0

I'm really trying to get a firm understanding of what it means when something "evaluates to false" Like:

Null evaluates to false by default

NaN evaluates to false by default

I would think it would be the opposite, like Null means "empty" so therefore it should evalute to true, if something is Null it is empty, true. If it is not empty, then null is false. So whenever I write a condition for say: **somedata** == null, does that mean that somedata is not empty because null evaluates to false? But if **somedata** IS empty null is true.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Audrey
  • 1
  • 1
  • 1
  • 2

4 Answers4

2

it means that if you do

if(null)
{
  //this will never be hit
}

the block will never be executed.

Femaref
  • 60,705
  • 7
  • 138
  • 176
0

Ouch, you're twisting my brain around!

A "Null" value all by itself is false, by definition. If you ask whether something is == Null, then the expression is true if the thing is == Null, and false if it's not.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

Sometimes, if I'm confused about a situation, I set up test cases:

   var param1;
    var param2 = "";
    var param3 = "I'm a string";
    
    if (param1)
    {
         alert(param1);   //fails
    }
    
    if (!param1)
    {
        //alert(param1);   //success : undefined
    }
    
    if (param1 == undefined)
    {
        //alert(param1);   //success : undefined
    }
    
    if (param1 == null)
    {
        //alert(param1);   //success : undefined
    }
    
    if (param2)
    {
        //alert(param2); //fails
    }
    
    if (!param2)
    {
        //alert(param2);   //success : no value
    }
    
    if (param2 == undefined)
    {
        //alert(param2);   //fails
    }
    
    if (param2 == null)
    {
        //alert(param2);   //fails
    }
    
    if (param3)
    {
        //alert(param3); //success : im a string
    }
    
    if (!param3)
    {
        //alert(param3);   //fails
    }
    
    if (param3 == undefined)
    {
        //alert(param3);   //fails
    }
    
    if (param3 == null)
    {
        //alert(param2);   //fails
    }

http://jsfiddle.net/HbML8/1/

Maybe this will help illustrate the above, correct, answers ;)

cheers!

Community
  • 1
  • 1
Bosworth99
  • 4,206
  • 5
  • 37
  • 52
  • Bosworth99, this is great illustration. I've gotten the best answers from this forum. I am printing out this page to use as a reference. I appreciate the time of all of you! Thank you! – Audrey Mar 23 '11 at 20:58
0

To make sense of what "evaluates to false" means, consider that, in a conditional expression like

if( condition ) { ... }

it is expected that condition is a boolean, something that evaluates to true or false. Something like

if( x == 3 ) { ... }

is fine because you can take the expression x == 3 and decide if it is true or false. Something like

if( null ) { ... }

in principle, isn't ok because null is not boolean. Some languages don't allow this, some languages decide that whenever they see null in a condition, it will have the same effect as false. This is what is meant by "Null evaluates to false".

This is the only situation in which you need to worry about interpreting null as false. In other example you posted:

if(**somedata** == null) { ... }

the code between brackets will be executed if **somedata** is null, and it won't if **somedata** is not null. null won't magically change its value to true or false. No conversion is needed, since **somedata** == null is already a valid boolean expression.

Jong Bor Lee
  • 3,805
  • 1
  • 24
  • 27
  • Jong Bor, Ok, I'm beginning to understand now. You are right, I have seen in javascript that null is false by default. I thank you for taking the time to explain in detail the difference and/or comparison of Null being boolean (sometimes "understood) when in reality, it isn't boolean. – Audrey Mar 23 '11 at 19:50