0

I am making a mistake or something strange is going on but I am not seeing it.

localStorage value = 9

if (localStorage.getItem('TotalProducts') >= '10') {
  alert('total products'+localStorage.getItem('TotalProducts'));
}

Why am I receiving the alert?

Alert content is total products9

Thanks everyone for helping.

For others read the comments below there is some really useful information in there.

purple11111
  • 709
  • 7
  • 20
  • 8
    because you're comparing strings, and in this case `'9' >= '10'` is `true` – Nick Parsons Apr 26 '19 at 12:44
  • 1
    Try comparing them as int not as a String – Jalil Apr 26 '19 at 12:44
  • 1
    because you have `'10'` instead of `10` – sassy_rog Apr 26 '19 at 12:45
  • @Jalil could you clarify how, please? – purple11111 Apr 26 '19 at 12:45
  • 1
    if (localStorage.getItem('TotalProducts') >= 10) { alert('total products'+localStorage.getItem('TotalProducts')); } – Jalil Apr 26 '19 at 12:45
  • 1
    @Jalil —`localStorage.getItem('TotalProducts') ` will be a string, that won't work for many values. – Quentin Apr 26 '19 at 12:46
  • @Quentin parseInt(localStorage.getItem('TotalProducts')) – Jalil Apr 26 '19 at 12:47
  • `+localStorage.getItem('TotalProducts')` – Scott Marcus Apr 26 '19 at 12:48
  • @Jalil —Don't use `parseInt` without a radix. – Quentin Apr 26 '19 at 12:49
  • @ScottMarcus That + parses it automatically to int? – Jalil Apr 26 '19 at 12:49
  • 1
    @Jalil There is no such thing as an `int` in JavaScript. But, prepending `+` in front of something that holds a value that could successfully be converted to a number, will do so. See the docs on [unary +](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_()). – Scott Marcus Apr 26 '19 at 12:50
  • @Quentin what do you mean by that? Can you explain it a little bit more? – Jalil Apr 26 '19 at 12:50
  • @ScottMarcus is your suggestion with `>= 10` or `>= '10'` – purple11111 Apr 26 '19 at 12:55
  • 1
    @purple11111 `+localStorage.getItem('TotalProducts') >= 10` because the `+` converts the `localStorage` value to a number so you can then compare it against a number. – Scott Marcus Apr 26 '19 at 12:56
  • @ScottMarcus I like your solution a lot. I also like Sudhir Ojha's solution but I am going for yours could you post it as an answer, please? – purple11111 Apr 26 '19 at 12:58
  • 1
    @purple11111 Can't do that because the question has been closed as a duplicate. But, Sudhir's answer isn't really a good solution for two reasons. 1) It's more code than necessary and 2) he didn't supply the second argument for `parseInt()` as I describe in the comments below his answer. `+` in front of the string is the best approach when you know the value will convert. – Scott Marcus Apr 26 '19 at 12:58
  • Don't take your question being closed as a duplicate personally. We do that so that we don't have the same question answered all over the place here. This way, we can have one definitive answer that everyone can refer to. – Scott Marcus Apr 26 '19 at 13:02
  • @ScottMarcus I thought I simply explain it. But in case such as duplicates should I delete my question? Because too many unanswered questions and I will get banned. – purple11111 Apr 26 '19 at 13:03
  • @ScottMarcus I see that the below answer is modified and reflects your changes. Going to set that as accepted but want to especially thank you for your help! Much appreciated!! – purple11111 Apr 26 '19 at 13:05
  • 1
    You won't get banned for too many unanswered questions and, no, don't delete it because, as you can see, a lot of good information came out in the comments and others may find that useful. – Scott Marcus Apr 26 '19 at 13:07
  • 1
    @ScottMarcus Okey, I will not delete it and you are right it would be a shame as there is a lot of useful information in the comments and the question's answer is also correct so I am leaving it alone. But going to use the code though ;-).. Thanks once more and have a great day! – purple11111 Apr 26 '19 at 13:11

1 Answers1

1
if (+localStorage.getItem('TotalProducts') >= 10) {
  alert('total products'+localStorage.getItem('TotalProducts'));
}

Convert your TotalProducts value of localStorage to a number and then compare it to 10.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
  • 5
    If you are going to use `parseInt()`, always supply the second optional argument for the radix. Otherwise, you could get data treated as Hex or Octals by mistake. But, the better solution here is to just prepend `+` to `localStorage.getItem('TotalProducts')`. – Scott Marcus Apr 26 '19 at 12:48
  • 3
    Alternatively you can use `localStorage.getItem('TotalProducts') * 1 >= 10` instead of using *parseInt* – n1md7 Apr 26 '19 at 12:52
  • @ScottMarcus so what is radix in this case 2,10 or 16 ? – Sudhir Ojha Apr 26 '19 at 12:53
  • The radix is the numeral base system you want to work with. If you want to work in base 10, then the radix is 10. If you want to work in hex, it's 16 and if you want to work in octal it's 8. – Scott Marcus Apr 26 '19 at 12:55
  • 2
    @harry That's overkill, just use: `+localStorage.getItem('TotalProducts')` – Scott Marcus Apr 26 '19 at 12:56
  • 2
    Thanks @ScottMarcus and harry for valuable information. – Sudhir Ojha Apr 26 '19 at 13:01
  • 1
    @SudhirOjha Thank you for your help and I did test your (first )solution and it also worked which made me immediately happy. Thank you for your contribution and discussion. Have a great day! – purple11111 Apr 26 '19 at 13:09