DuduAlul
DuduAlul

Reputation: 6390

Javascript if-statement vs. comparsion

Is it true that in if-statement JavaScript wrap the condition into a Boolean?

if(x) => if(Boolean(x))

Is it true that in comparison JavaScript wrap the compare elements into a Number?

a == b => Number(a) == Number(b)

Upvotes: 3

Views: 142

Answers (3)

Adriano Repetti
Adriano Repetti

Reputation: 67148

Is it true that in if-statement JavaScript wrap the condition into a Boolean?

Usually yes.

Is it true that in comparison JavaScript wrap the compare elements into a Number?

Absolutely no.

Explanation

From JavaScript Language Specifications.

The if statement is defined at § 12.5 as:

if ( Expression ) Statement else Statement

It says that the Expression will be evaluated, converted with GetValue() and then tested after the ToBoolean() conversion.

Then the first assertion is true (but see later), the condition for the if statement is evaluated like is passed as parameter to the Boolean function. Please recall how JavaScript handles type conversion to boolean (§ 9.2):

  • undefined and null values are converted to false.
  • numbers are converted to false if ±0 or NaN otherwise they're converted to true.
  • strings are converted to false if empty otherwise always to true regardless their content.
  • objects are always converted to true.

Because of the call to GetValue() strictly speaking this assertion is not always true, take a look to § 8.7.1 where the standard describes how GetValue() works, here can happen some magic conversion before ToBoolean() is called.

The == operator is defined as in § 11.9.3.
As you can see it doesn't specify that operands must be (or will be treated as) numbers, the behavior of the operator is different and regulated by a series of rules based on the type of the operands. Then your second assertion is false. The case they're numbers (or one of them is a number) is just a special case in the algorithm, please note that at point 4 of the algorithm it says that if one of them is a number and the other one is a string then it'll be converted with ToNumber(), only in this case (with all the implications that this conversion has).

It's intuitive if you think that you can compare functions, strings or numbers, not every type can be converted to a numeric value.

Upvotes: 1

Jeff
Jeff

Reputation: 5043

Yes, and No.

For the first part, yes, that is essentially what the javascript does.

But for the latter, no. Not everything in JavaScript can be converted to a number. For example:

Number('abc') // => NaN

And Not-A-Numbers are not equal:

NaN == NaN // => false

So something like this:

Number('abc') == Number('abc') // => false!

But that's actually true with equality comparison.

'abc' == 'abc' // => true

As a side note, it's probably better to use === in JavaScript, which also checks the type of the values being compared:

0 == '0' // => true
0 === '0' // => false, because integer is not a string

More details about === can be read over here.

Upvotes: 3

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263147

  1. Yes, that's true, x is evaluated in a boolean context in this situation, so the equivalent of Boolean(x) is applied.

  2. No, that's not true. It only looks that way because the coercitive equality operator == tries to convert a and b to the same type. Number() is only applied if either a or b is already a Number. For instance:


>>> 0x2A == 42
true   // both 'a' and 'b' are numbers.

>>> "0x2A" == 42
true   // 'a' is a string whose number coercion is equal to 'b'.

>>> "0x2A" == "42"
false  // 'a' and 'b' are different strings.

Upvotes: 1

Related Questions