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


Groups > comp.lang.javascript > #20558 > unrolled thread

falsy and truthy

Started bytlbaxter99@yahoo.com
First post2013-07-05 20:01 -0700
Last post2013-08-10 09:03 -0300
Articles 5 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#20558 — falsy and truthy

Fromtlbaxter99@yahoo.com
Date2013-07-05 20:01 -0700
Subjectfalsy and truthy
Message-ID<f80d266d-dad8-4514-a617-773dec591a52@googlegroups.com>
Hi everyone,

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.

Thanks

[toc] | [next] | [standalone]


#20559

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-07-06 07:20 +0000
Message-ID<kr8gfp$61j$1@dont-email.me>
In reply to#20558
On Fri, 05 Jul 2013 20:01:38 -0700, tlbaxter99 wrote:

> Hi everyone,
> 
> 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.

falsy and false are two different things.

falsy values are never truthy, but that doesn't mean they're always false.

Languages may have pseudo-values or named constants or whatever else you 
want to call them which are never == anything else, even themselves, but 
are still considered falsy.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#20560

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-07-06 10:24 +0200
Message-ID<1775736.VXPAqHmOfG@PointedEars.de>
In reply to#20558
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. 

[toc] | [prev] | [next] | [standalone]


#20561

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-07-06 11:22 +0200
Message-ID<XnsA1F573C4A7D54eejj99@194.109.133.133>
In reply to#20560
Thomas 'PointedEars' Lahn wrote on 06 jul 2013 in comp.lang.javascript:

> That is true :)  The ƒ o!ƒ   operator internally converts its operand,
> and the ƒ oifƒ   statement internally converts its expression to a
> primitive Boolean value before evaluating that.  The ƒ o==ƒ   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 (ƒ
> oThe if Statementƒ  ), 11.4.9 (ƒ oLogical NOT Operator ( ! )ƒ  ), and
> 11.9.1 (ƒ oThe Equals Operator ( == )ƒ  ) for details.  (AFAIK this
> has not changed since the first Edition.)

A character-efficient way to convert a value to true or false is:

result = !!myValue;

This a nonnotgood idea?

> Get a real name.  Do not use Google Groups for posting, at least not
> until they fixed the many bugs.

Even the minutest of bugs can byte a bit:

<http://www.oed.com/view/Entry/128424>


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#20909

From"Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br>
Date2013-08-10 09:03 -0300
Message-ID<ku5a5r$h4j$1@speranza.aioe.org>
In reply to#20558
On 06-07-2013 00:01, tlbaxter99@yahoo.com wrote:
> Hi everyone,
>
> 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.

As described in ES 5.1, §9.2 (ToBoolean), there are seven falsy values: 
false, 0, -0, "", NaN, null, and undefined. All other values are truthy.

Logical NOT (!)	(!expr)	returns false if its single operand can be 
converted to true; otherwise, returns true.

>
> 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.

In this particular case, we should use the strict Equals Operator (===)
than just the Equals Operator (==), because undefined == null, for
instance, produces true, whereas undefined === null yields false.

These are some interesting cases:

'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
' \t\r\n ' == 0 // true

Note: Douglas Crockford's advice is to never use [what he calls] the
evil twins (== and !=). Instead, we should always use === and !==,
although Crockford's advice is a tad exaggerated when dealing with the
typeof operator which always returns a string value.


--
Joao Rodrigues (J.R.)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web