Take for example:
console.log(null * 3); //0
Notice how the answer is now 0. It’s almost as if null converted into 0.
In fact, that’s exactly what happened!
And when you multiply 0 and 3, you get 0.
Check out the example below:
console.log("3" - 2); //1
Here, you see that “3” is technically a string instead of a number.
In this expression, you are subtracting a string (“3”) by a number (2).
3 – 2 = 1.
console.log("3" + 2); //32
To reiterate and prove what’s going on, this expression of code adds the number (2) to the string (“3”).
When you add two strings together, what you’re doing is called concatenation. That’s when you sort of glue two strings together.
But here’s a question for you. You were probably expecting the result to be 5 right? I mean, in the example above where “3” – 2 ended up being 1 means that “3” was converted to the number 3.
However, in this example, you can see that “3” was not converted to the number 3, while the second number 2 was converted to a string like “2” instead!
That’s why we see 32, the concatenation of the strings 3 and 2.
My guess is that in the case of the addition operator, concatenation takes precedence over the concept of adding two numbers.
This sounds a little confusing but here’s an example of what I’m talking about:
console.log("two" * 2); //NaN
As you can see here, this line of code has an error message called NaN which stands for Not a Number.
You can’t really change the letters two into 5. Also, you can’t multiply a string to a number either.
Since they didn’t know how to convert the value, it returned a NaN.
Let’s check out some true and false conversion concepts.
console.log(false == 0); //true
Do you understand why this results in truth?
It’s because the false eventually converts into 0, and when 0 == 0, then it’s good!
console.log(true == 32); //false
console.log(true == 1); //true
Here, it proves that true converts into 1 because that’s the only possible reason that the expression (true == 1) can evaluate as true.
If you continue to evaluate arithmetic operations using the result of NaN, you will continue to get the return of NaN in the next expression, leaving you with a series of errors. You may have to keep an eye out for that!
Are two NaN’s equal to each other?
console.log(NaN == NaN); //false
Apparently, two NaN’s are not actually equal to each other.
Are null and undefined combinations equal to each other?
console.log(undefined == undefined); //true
However, undefined and undefined do equal each other.
console.log(null == null); //true
And two nulls are also equal to each other.
Or how about when we test whether a null is equal to an undefined?
console.log(null == undefined); //true
So we see here that different arrangements of null and undefined are all equal to each other.
As a result, undefined and null should be considered interchangeable.
There’s one more thing I want to check…
Is null or defined equal to 0?
console.log(null == 0); //false
console.log(undefined == 0); //false
So undefined and null are not equal to 0.