Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20560

Re: falsy and truthy

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1775736.VXPAqHmOfG@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Organization PointedEars Software (PES)
Date Sat, 06 Jul 2013 10:24:09 +0200
User-Agent KNode/4.10.4
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: falsy and truthy
Newsgroups comp.lang.javascript
References <f80d266d-dad8-4514-a617-773dec591a52@googlegroups.com>
MIME-Version 1.0
Lines 42
NNTP-Posting-Date 06 Jul 2013 10:24:12 CEST
NNTP-Posting-Host a96eb02e.newsspool4.arcor-online.net
X-Trace DXC=T1L8<Tn?WM5RadXUBHgFh34IUK<Cl32<14Fo<]lROoR18kF<OcfhCO;lY=Qggn==W?DZm8W4\YJN<b@mF9jNikd5BoKGPB=12A7KRCS92l?oA;
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:20560

Show key headers only | View raw


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


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