JavaScript Special Keywords

Javascript has a few pre-defined variables with special meaning.

NaN

Not a Number (Generated when an arithmetic operation returns an invalid result).

NaN is a weird construct. For one, it is NEVER equal to itself so you can't simply check to see if 3/'dog' == 'NaN'.

You must use the construct isNaN(3/dog) to determine if the operation failed.

In boolean operations NaN evaluates to false, however 0 also evaluates to false so use isNaN. Since NaN is never equal to itself you can use this simple trick as well:


let result = 80;

if (result !== result){ 
  console.log("Not a Number");
}

Example


let result = 80;

if (result !== result){ 
  console.log("Not a Number");
}

Infinity

Infinity is a keyword which is returned when an arithmetic operation overflows Javascript's precision which is in the order of 300 digits.

You can find the exact minimum and maximum range for your Javascript implementation using Number.MAX_VALUEand Number.MIN_VALUE.

null

null is a reserved word that means "empty". When used in boolean operation it evaluates to false. Javascript supports true and false as boolean values.

If a variable hasn't been declared or assigned yet (an argument to a function which never received a value, an object property that hasn't been assigned a value) then that variable will be given a special undefinedvalue. In boolean operations undefinedevaluates as false. Here's an example


let result = 80;
function doAlert(sayThis){ 
  if (sayThis === undefined){ // Check to see if sayThis was passed.
    sayThis = ("default value");// It wasn't so give it a default value
  }// End check
  alert(sayThis);// Toss up the alert.
}
doAlert();

Example


let result = 80;
function doAlert(sayThis){ 
  if (sayThis === undefined){ // Check to see if sayThis was passed.
    sayThis = ("default value");// It wasn't so give it a default value
  }// End check
  alert(sayThis);// Toss up the alert.
}
doAlert();