4

I'm wondering what the core difference is between the conditional syntax below?

if (something) {
    // do something
}

vs.

if (something == true) {
    // do something
}

Are there any differences?

Edit: I apologize. I made a typo when the question was asked. I did not mean to put a triple equals sign. I know that triple equals is a strict operator. I was meaning to ask if '==' is the equivalent of if (something).

Codist
  • 1,198
  • 2
  • 11
  • 28
  • 6
    You've completely changed the meaning of the question by changing `===` to `==`. Edit: Now someone has changed it back... @W3Geek: What did you actually intend? – James Allardice Aug 07 '12 at 19:35
  • 2
    Yes, I made a typo. I meant to say double equals. I know what triple equals is... Its a strict operator. I already know there is a difference between them. – Codist Aug 07 '12 at 19:36
  • Personally, I write if (a === true) if I need to check a known boolean and if (a !== null && a !== undefined) to test if (a). I think if (a) could mean too many things.... For instance, if a is 0 then if(a) will evaluate to false.... – frenchie Aug 07 '12 at 19:38
  • I messed up this thread so bad by making a typo. :( – Codist Aug 07 '12 at 19:39
  • Yeah I just noticed that. `=]` Removed the previous comment. +1 @pst – Fabrício Matté Aug 07 '12 at 19:42
  • 4
    For those voting to close, he was not asking about `===`, but his question kept getting edited by others. – Alex W Aug 07 '12 at 19:44

6 Answers6

13

The difference is that in if(something), something is evaluated as boolean. It is basically

if(ToBoolean(something))

where ToBoolean is an internal function that is called to convert the argument to a boolean value. You can simulate ToBoolean with a double negation: !!something.

In the second case, both operands are (eventually) converted to numbers first, so you end up with

if(ToNumber(something) == ToNumber(true))

which can lead to very different results. Again, ToNumber is an internal function. It can be simulated (to some degree) using the unary plus operator: +something == +true. In the actual algorithm, something is first passed ToPrimitive if something is an object.


Example:

Assume that

var something = '0'; // or any other number (not 0) / numeric string != 1

if(something) will be true, since '0' is a non-empty string.

if(something == true) will be false, since ToNumber('0') is 0, ToNumber(true) is 1 and 0 == 1 is false.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    +1 This is the closest thing to a correct answer. The only thing I would add is that `ToBoolean` and `ToNumber` are not exposed directly as functions in JavaScript. Calling the `Number` constructor as a function, or using the unary `+` operator are equivalent though. – Matthew Crumley Aug 07 '12 at 19:54
  • @Matthew: Thanks, I clarified it. – Felix Kling Aug 07 '12 at 19:58
  • Great answer. Out of curiosity, in your answer, you say "where `ToBoolean` is an internal function that is called to convert the argument to a boolean value". Is there a reason that you used `ToBoolean` instead of the `Boolean()` function? – Jacob Stamm Aug 12 '16 at 14:26
  • 1
    @Jacob: That's just what the spec uses when evaluating the condition of an `if` statement. I could have used `Boolean` just as well. – Felix Kling Aug 12 '16 at 14:32
12

EDIT: Below only holds true for the original question, in which the === operator was used.

The first one will execute the body of the if-statement if something is "truthy" while the second will only execute it if it is equal in type and value to true.

So, what is "truthy"? To understand that, you need to know what is its opposite: falsey. All values in JavaScript will be coerced into a Boolean value if placed in a conditional expression. Here's a list of falsey values:

  • false
  • 0 (zero)
  • "" (empty string)
  • null
  • undefined
  • NaN

All other values are truthy, though I've probably missed some obscure corner case that someone will point out in the comments.

Here's my answer to the updated question:

The conditional if (something) and if (something == true) are equivalent, though the second is redundant. something will be type coerced in the same way in either case. This is wrong. See Felix Kling's answer.

Community
  • 1
  • 1
FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
3

if(something) is equivalent to if(someting == true).

The == operator checks for equality while === checks for sameness. In this case, any truthy value will cause the condition to be met for the first one, but only true will cause the condition to be met for the second one.

EDIT: Felix Kling's answer is correct. Please reference that instead.

josh shadowfax
  • 304
  • 1
  • 5
2

Now that the question has changed from === to ==, no, there is no practical difference.

Matt
  • 6,993
  • 4
  • 29
  • 50
1

There are a couple ways to look at it. The first example evaluates to see if "something" has a true-like or positive value. So as long as it is not 0, negative, null, or empty it should evaluate the contents of that if statement.

In your second statement you are testing to see if "something" is the equivalent of boolean true. Another words "TRUE" or 1. You could also do a "===" comparison so that it has to be identical to what your comparing it to. So in this case if you did something === true then "something" would have to be boolean true, the value of 1 would not suffice.

Patrick
  • 3,302
  • 4
  • 28
  • 47
0

There is no difference between the two conditions thing == true and just thing.

There is a large difference between thing === true and just thing, however.

In javascript, values are coerced to a boolean when they are put in a condition. This means all values of all types must be able to be coerced to either true or false. A value that coerces to true is generally called "truthy", and a value that coerces to false is called "falsey".

The "falsey" values are as follows:

  • NaN (not a number)
  • '' (empty string)
  • undefined
  • null
  • 0 (numeric 0)

Everything else evaluates to true when coerced. This jsFiddle will help you keep track of what is "truthy" and what is "falsey".

The identity operator (===) does NOT perform any type coercion: if the types are different, the comparison immediately fails.

jbabey
  • 45,965
  • 12
  • 71
  • 94