1

Consider the following:

var comp = 2+3;
console.log(comp == "5"); // true
console.log(comp === "5"); // false
console.log(comp == "2+3"); // false 

Why isn't the 3rd comparison true? JS can convert a string to integer only if it's a single number? What happens in the 3rd comparison? Is it unable to convert "2+3" into a number so it just assumes it's different?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Try out in Chrome's developer tool what's the difference between `console.log("2+3")` and `console.log(2+3)`. You will see the first is resolved as `"2+3"` and the second one is `5`. – norbitrial May 18 '20 at 13:35
  • 2
    `"2+3"` can't be coerced to a number, it's a *calculation* and won't be magically evaluated. – jonrsharpe May 18 '20 at 13:36
  • @jonrsharpe `"2+3"` is not a _calculation_ to JavaScript. It's just a string. That's like saying `"Hello"` is a _greeting_ -- but to JavaScript, it's not ... it's just a string. I'm sure you understand this, but what you said could be misleading to someone who doesn't understand. – Wyck May 18 '20 at 13:59
  • @Wyck sorry, I meant it *represents* a calculation. `"5"` is also just a string! – jonrsharpe May 18 '20 at 14:01
  • You could evaluate the expression: 2+3===eval("2+3"), [p.s. many disapprove of eval] – QuentinUK May 18 '20 at 14:02
  • Since your question has already been answered, here's something to also be aware of. _Most_ arithmetic operators will automatically coerc numbers from strings when possible: `"3" * "2" === 6` and `"3" - "2" === 1`, *but* the plus operator will still just concatenate the strings: `"3" + "2" === "32"` – Gary May 18 '20 at 14:24

2 Answers2

3

comp, which is 5 is a number type in JS and "5" is a string. A string and a number are not the same and thus console.log(comp === "5") will return false.

When you use == instead of === it returns true, because of a concept called "type coercion". This just means that JS assumes that you are trying to compare two of the same types—in this case numbers—because they seem to be identical, and returns true. So it converted the string to a number to help you out.

In the third case there are other types of characters in the string, in addition to numbers, and none of them are similar to the number 5, so type coercion does not apply and so it returns false.

Read more about type coercion here.

Ludolfyn
  • 1,806
  • 14
  • 20
1

Because "2+3" with quotation marks signals javascript that this is an string and it cant be coerced to an number.

And 5 is not the same like "2+3".

Just test it: console.log(typeof '2+3') and console.log(typeof 2 + 3)

bill.gates
  • 14,145
  • 3
  • 19
  • 47
  • Double equal doesnot check types. Tripple equal does. The reason it is `false` it is because `"2+3"` is not `5`. – Adam Orłowski May 18 '20 at 14:00
  • 1
    @AdamOrlov actually, double equals does check types, but tries to perform coercion if types are not the same. Maybe a formulation detail, but it definitely helped me better understand that `==` does actually _more work_ than `===`. – Pac0 May 26 '20 at 19:21