Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #20560
| Message-ID | <1775736.VXPAqHmOfG@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-07-06 10:24 +0200 |
| Subject | Re: falsy and truthy |
| Newsgroups | comp.lang.javascript |
| References | <f80d266d-dad8-4514-a617-773dec591a52@googlegroups.com> |
tlbaxter99@yahoo.com wrote:
> I thought I understood falsy and truthy until I ran this code:
>
> var falsyValues = [false, 0, null, "", undefined, NaN];
>
> for (var index in falsyValues)
> if (!falsyValues[index])
> console.log("FALSE");
> else
> console.log("TRUE");
>
> As I expected, "FALSE" was printed for all 6 values. No problem.
>
> Now, when I modify the if() statement to this:
> if (false == falsyValues[index])
>
> I see that null, undefined and NaN do NOT compare as being equal to false,
> yet when using the not operator(!) a shown earlier, they ARE considered
> false.
>
> Can someone explain this? Using the not operator (!) is apparently not the
> same as comparing directly against false.
That is true :) The “!” operator internally converts its operand, and the
“if” statement internally converts its expression to a primitive Boolean
value before evaluating that. The “==” operator acts as type-converting
operator as well, but follows different, much more complex rules.
See the ECMAScript Language Specification, 5.1 Edition, §§12.5 (“The if
Statement”), 11.4.9 (“Logical NOT Operator ( ! )”), and 11.9.1 (“The Equals
Operator ( == )”) for details. (AFAIK this has not changed since the first
Edition.)
Get a real name. Do not use Google Groups for posting, at least not until
they fixed the many bugs.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
falsy and truthy tlbaxter99@yahoo.com - 2013-07-05 20:01 -0700
Re: falsy and truthy Denis McMahon <denismfmcmahon@gmail.com> - 2013-07-06 07:20 +0000
Re: falsy and truthy Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-07-06 10:24 +0200
Re: falsy and truthy "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-07-06 11:22 +0200
Re: falsy and truthy "Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br> - 2013-08-10 09:03 -0300
csiph-web