If null, NaN, false and undefined in Javascript

10

I want to know how to if to check any of these possible states.

    
asked by anonymous 25.11.2016 / 20:42

6 answers

8

Because JavaScript is dynamic, you can try to "coerce" the conversion of these values to boolean , this will cause all of the items quoted ( null , undefined , false , NaN , "" ), equals false .

If you make a comparison of one of the values with == false , the answer will be true , however, if you use === the answer will be false . This is because == ignores the types and tries to force the conversion to validate the values.

The if and the negation operator ( ! ) have the same behavior as == .

Example with if :

var v1 = null;
var v2 = undefined;
var v3 = NaN;
var v4 = 0;
var v5 = false; // Não precisava dessa, né? :p
var v6 = "";

valida(v1);
valida(v2);
valida(v3);
valida(v4);
valida(v5);
valida(v6);

function valida(variavel){
  if(variavel)
    console.log("true");
  else
    console.log("false");
}

Example with ===

I'm not going to do all the values to not get too long.

var v1 = "";

valida(v1);

function valida(variavel){
  if(variavel == false)
    console.log("true");
  else
    console.log("false");
  
  if(variavel === false)
    console.log("true");
  else
    console.log("false");
}  
    
25.11.2016 / 20:49
8

Normally a simple if (variable) is used to determine any of the situations in code, but to detect each situation individually, a summary follows:

Testing if it is null :

if( minhaVariavel === null ) ...

If testing with only == is equal to undefined


Testing if it is NaN :

if( isNaN( minhaVariavel ) ) ...

or

minhaVariavel !== minhaVariavel 


Testing is false

if( minhaVariavel === false ) ...


Testing if it is undefined :

For general use:

if( typeof minhaVariavel == 'undefined' )

If you want to test if the value of an existing variable is undefined:

if( minhaVariavel === undefined ) ...
    
26.11.2016 / 03:48
2

It does:

 var x = false;

 if(x === false || x === undefined || isNaN(x)) {

 }

Any questions, check out these links: link link

    
25.11.2016 / 20:53
1

Explanation

As reported in the Rodrigo Branas video ( here ), Javascript identifies as false some cases, these being:

  • 0;
  • NaN;
  • "(empty string);
  • false;
  • null;
  • undefined.

That is, if I just wanted to check if a variable contains a value, without being afraid of it being null or undefined , I should just do:

if(variavel) {
  //Seu código
}

Extra

To test what I said, here's the code snippet:

var a = 0;
var b = false;
var c = "";
var d = "abc" * 2;
var e = null;
var f;

if(!a) {
  alert("A false");
}

if(!b) {
  alert("B false");
}

if(!c) {
  alert("C false");
}

if(!d) {
  alert("D false");
}

if(!e) {
  alert("E false");
}

if(!f) {
  alert("F false");
}
    
25.11.2016 / 20:53
0
if ( ! variavel ) {
    console.log("Variável inválida");
}
    
25.11.2016 / 21:01
-1

Hello, use typeof to check for undefined. For example:

var variavel; 

if (typeof variavel === "undefined"){
  console.log("sem valor atribuido");
}
    
26.11.2016 / 03:30