Built-in Constants
null is used for representing the intentional absence of an object value and is a primitive value. Unlike undefined, it is not a property of the global object.
It is equal to undefined but not identical to it.
null == undefined; // truenull === undefined; // falseCAREFUL: The typeof null is 'object'.
typeof null; // 'object';To properly check if a value is null, compare it with the strict equality operator
var a = null;
a === null; // trueTesting for NaN using isNaN()
Section titled “Testing for NaN using isNaN()”window.isNaN()
Section titled “window.isNaN()”The global function isNaN() can be used to check if a certain value or expression evaluates to NaN. This function (in short) first checks if the value is a number, if not tries to convert it (*), and then checks if the resulting value is NaN. For this reason, this testing method may cause confusion.
(*) The “conversion” method is not that simple, see ECMA-262 18.2.3 for a detailed explanation of the algorithm.
These examples will help you better understand the isNaN() behavior:
isNaN(NaN); // trueisNaN(1); // false: 1 is a numberisNaN(-2e-4); // false: -2e-4 is a number (-0.0002) in scientific notationisNaN(Infinity); // false: Infinity is a numberisNaN(true); // false: converted to 1, which is a numberisNaN(false); // false: converted to 0, which is a numberisNaN(null); // false: converted to 0, which is a numberisNaN(""); // false: converted to 0, which is a numberisNaN(" "); // false: converted to 0, which is a numberisNaN("45.3"); // false: string representing a number, converted to 45.3isNaN("1.2e3"); // false: string representing a number, converted to 1.2e3isNaN("Infinity"); // false: string representing a number, converted to InfinityisNaN(new Date); // false: Date object, converted to milliseconds since epochisNaN("10$"); // true : conversion fails, the dollar sign is not a digitisNaN("hello"); // true : conversion fails, no digits at allisNaN(undefined); // true : converted to NaNisNaN(); // true : converted to NaN (implicitly undefined)isNaN(function(){}); // true : conversion failsisNaN({}); // true : conversion failsisNaN([1, 2]); // true : converted to "1, 2", which can't be converted to a numberThis last one is a bit tricky: checking if an Array is NaN. To do this, the Number() constructor first converts the array to a string, then to a number; this is the reason why isNaN([]) and isNaN([34]) both return false, but isNaN([1, 2]) and isNaN([true]) both return true: because they get converted to "", "34", "1,2" and "true" respectively. In general, an array is considered NaN by isNaN() unless it only holds one element whose string representation can be converted to a valid number.
Number.isNaN()
Section titled “Number.isNaN()”In ECMAScript 6, the Number.isNaN() function has been implemented primarily to avoid the problem of window.isNaN() of forcefully converting the parameter to a number. Number.isNaN(), indeed, doesn’t try to convert the value to a number before testing. This also means that only values of the type number, that are also NaN, return true (which basically means only Number.isNaN(NaN)).
From ECMA-262 20.1.2.4:
When the Number.isNaN is called with one argument number, the following steps are taken:
-
- If Type(number) is not Number, return `false`.
- If number is `NaN`, return `true`.
- Otherwise, return `false`.
Some examples:
// The one and onlyNumber.isNaN(NaN); // true
// NumbersNumber.isNaN(1); // falseNumber.isNaN(-2e-4); // falseNumber.isNaN(Infinity); // false
// Values not of type numberNumber.isNaN(true); // falseNumber.isNaN(false); // falseNumber.isNaN(null); // falseNumber.isNaN(""); // falseNumber.isNaN(" "); // falseNumber.isNaN("45.3"); // falseNumber.isNaN("1.2e3"); // falseNumber.isNaN("Infinity"); // falseNumber.isNaN(new Date); // falseNumber.isNaN("10$"); // falseNumber.isNaN("hello"); // falseNumber.isNaN(undefined); // falseNumber.isNaN(); // falseNumber.isNaN(function(){}); // falseNumber.isNaN({}); // falseNumber.isNaN([]); // falseNumber.isNaN([1]); // falseNumber.isNaN([1, 2]); // falseNumber.isNaN([true]); // falseNaN stands for “Not a Number.” When a mathematical function or operation in JavaScript cannot return a specific number, it returns the value NaN instead.
It is a property of the global object, and a reference to Number.NaN
window.hasOwnProperty('NaN'); // trueNaN; // NaNPerhaps confusingly, NaN is still considered a number.
typeof NaN; // 'number'Don’t check for NaN using the equality operator. See isNaN instead.
NaN == NaN // falseNaN === NaN // falseundefined and null
Section titled “undefined and null”At first glance it may appear that null and undefined are basically the same, however there are subtle but important differences.
undefined is the absence of a value in the compiler, because where it should be a value, there hasn’t been put one, like the case of an unassigned variable.
-
- `typeof undefined === 'undefined'`
typeof null === 'object'- A variable when it is declared but not assigned a value (i.e. defined)
-
let foo;console.log('is undefined?', foo === undefined);// is undefined? true
-
let foo = { a: 'a' };console.log('is undefined?', foo.b === undefined);// is undefined? true
-
function foo() { return; }console.log('is undefined?', foo() === undefined);// is undefined? true
-
function foo(param) {console.log('is undefined?', param === undefined);}foo('a');foo();// is undefined? false// is undefined? true
undefinedis also a property of the globalwindowobject.// Only in browsersconsole.log(window.undefined); // undefinedwindow.hasOwnProperty('undefined'); // trueBefore ECMAScript 5 you could actually change the value of the
window.undefinedproperty to any other value potentially breaking everything.Infinity and -Infinity
Section titled “Infinity and -Infinity”1 / 0; // Infinity// Wait! WHAAAT?Infinityis a property of the global object (therefore a global variable) that represents mathematical infinity. It is a reference toNumber.POSITIVE_INFINITYIt is greater than any other value, and you can get it by dividing by 0 or by evaluating the expression of a number that’s so big that overflows. This actually means there is no division by 0 errors in JavaScript, there is Infinity!
There is also
-Infinitywhich is mathematical negative infinity, and it’s lower than any other value.To get
-Infinityyou negateInfinity, or get a reference to it inNumber.NEGATIVE_INFINITY.- (Infinity); // -InfinityNow let’s have some fun with examples:
Infinity > 123192310293; // true-Infinity < -123192310293; // true1 / 0; // InfinityMath.pow(123123123, 9123192391023); // InfinityNumber.MAX_VALUE * 2; // Infinity23 / Infinity; // 0-Infinity; // -Infinity-Infinity === Number.NEGATIVE_INFINITY; // true-0; // -0 , yes there is a negative 0 in the language0 === -0; // true1 / -0; // -Infinity1 / 0 === 1 / -0; // falseInfinity + Infinity; // Infinityvar a = 0, b = -0;a === b; // true1 / a === 1 / b; // false// Try your own!Operations that return NaN
Section titled “Operations that return NaN”Mathematical operations on values other than numbers return NaN.
"a" + 1"b" * 3"cde" - "e"[1, 2, 3] * 2An exception: Single-number arrays.
[2] * [3] // Returns 6Also, remember that the
+operator concatenates strings."a" + "b" // Returns "ab"Dividing zero by zero returns
NaN.0 / 0 // NaNNote: In mathematics generally (unlike in JavaScript programming), dividing by zero is not possible.
Math library functions that return NaN
Section titled “Math library functions that return NaN”Generally,
Mathfunctions that are given non-numeric arguments will return NaN.Math.floor("a")The square root of a negative number returns NaN, because
Math.sqrtdoes not support imaginary or complex numbers.Math.sqrt(-1)Number constants
Section titled “Number constants”The
Numberconstructor has some built in constants that can be usefulIn many cases the various operators in Javascript will break with values outside the range of (
Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER)Note that
Number.EPSILONrepresents the different between one and the smallestNumbergreater than one, and thus the smallest possible difference between two differentNumbervalues. One reason to use this is due to the nature of how numbers are stored by JavaScript see Check the equality of two numbers -
Setting a variable to undefined means the variable effectively does not exist. Some processes, such as JSON serialization, may strip undefined properties from objects. In contrast, null properties indicate will be preserved so you can explicitly convey the concept of an “empty” property.
The following evaluate to undefined: