MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Built-in Constants

NULL

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; // true
null === undefined; // false

CAREFUL: 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; // true

NaN

NaN 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'); // true
NaN; // NaN

Perhaps confusingly, NaN is still considered a number.


typeof NaN; // 'number'

Don't check for NaN using the equality operator. See isNaN instead.


NaN == NaN // false
NaN === NaN // false

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.


  • undefined is a global value that represents the absence of an assigned value.
    typeof undefined === 'undefined'

  • null is an object that indicates that a variable has been explicitly assigned "no Value".
    typeof null === 'object'

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:


  • A variable when it is declared but not assigned a value (i.e. defined)

  • let foo;
    console.log('is undefined?', foo === undefined);
    

  • Accessing the value of a property that doesn't exist

  • let foo = { a: 'a' };
    console.log('is undefined?', foo.b === undefined);

  • The return value of a function that doesn't return a value

  • function foo() { return; }
    console.log('is undefined?', foo() === undefined);

  • The value of a function argument that is declared but has been omitted from the function call

  • function foo(param) {
     console.log('is undefined?', param === undefined);
    }
    foo('a');
    foo();

    undefined is also a property of the global window object.


    console.log(window.undefined); 
    window.hasOwnProperty('undefined'); 

    Before ECMAScript 5 you could actually change the value of the window.undefined property to any other value potentially breaking everything.


    Number constants

    The Number constructor has some built in constants that can be useful


    Number.MAX_VALUE; // 1.7976931348623157e+308
    Number.MAX_SAFE_INTEGER; // 9007199254740991
    Number.MIN_VALUE; // 5e-324
    Number.MIN_SAFE_INTEGER; // -9007199254740991
    Number.EPSILON; // 0.0000000000000002220446049250313
    Number.POSITIVE_INFINITY; // Infinity
    Number.NEGATIVE_INFINITY; // -Infinity
    Number.NaN; // NaN

    In 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.EPSILON represents the different between one and the smallest Number greater than one, and thus the smallest possible difference between two different Number values. One reason to use this is due to the nature of how numbers are stored by JavaScript see Check the equality of two numbers


    Math library functions that return NaN

    Generally, Math functions that are given non-numeric arguments will return NaN.


    Math.floor("a")

    The square root of a negative number returns NaN, because Math.sqrt does not support imaginary or complex numbers.


    Math.sqrt(-1)


    Conclusion

    In this page (written and validated by ) you learned about Javascript Built-in Constants . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Arrays and Objects.



    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


    Share On:


    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.