Search the Blog

Thursday, August 8, 2019

JavaScript Built-in Constants syntax with example

Javascript currently have 8 built-in constants-


1- null 
   null is used for representing the intentional absence of an object value and is a primitive value.
Note:- It is not a property of the global object.
Note:- Type of null is object.
Note:- == is used for check equality  while === is used for check identity.

2-  NaN stands for Not a number

isNaN(NaN);          // true
isNaN(1);            // false: 1
isNaN(-2e-4);        // false: -2e-4 is a number (-0.0002) in scientific notation
isNaN(Infinity);     // false: Infinity is a number
isNaN(true);         // false: converted to 1, which is a number
isNaN(false);        // false: converted to 0, which is a number
isNaN(null);         // false: converted to 0, which is a number
isNaN("");           // false: converted to 0, which is a number
isNaN(" ");          // false: converted to 0, which is a number
isNaN("45.3");       // false: string representing a number, converted to 45.3
isNaN("1.2e3");      // false: string representing a number, converted to 1.2e3
isNaN("Infinity");   // false: string representing a number, converted to Infinity
isNaN(new Date);     // false: Date object, converted to milliseconds since epoch
isNaN("10$");        // true : conversion fails, the dollar sign is not a digit
isNaN("hello");      // true : conversion fails, no digits at all
isNaN(undefined);    // true : converted to NaN
isNaN();             // true : converted to NaN (implicitly undefined)
isNaN(function(){}); // true : conversion fails
isNaN({});           // true : conversion fails
isNaN([1, 2]);       // true : converted to "1, 2", which can't be converted to a number


3- Number NaN results
Number.isNaN(NaN);          // true// Numbers
Number.isNaN(1);            // false
Number.isNaN(-2e-4);        // false
Number.isNaN(Infinity);     // false
Number.isNaN(true);         // false
Number.isNaN(false);        // false
Number.isNaN(null);         // false
Number.isNaN("");           // false
Number.isNaN(" ");          // false
Number.isNaN("46.3");       // false
Number.isNaN("1.2e3");      // false
Number.isNaN("Infinity");   // false
Number.isNaN(new Date);     // false
Number.isNaN("100$");        // false
Number.isNaN("hello");      // false
Number.isNaN(undefined);    // false
Number.isNaN();             // false
Number.isNaN(function(){}); // false
Number.isNaN({});           // false
Number.isNaN([]);           // false
Number.isNaN([3);          // false
Number.isNaN([4, 6]);       // false
Number.isNaN([true]);       // false

4-undefined 
undefined is a global value that represents the absence of an assigned value.


5- Infinity and -Infinity
Infinity is a property of the global object (therefore a global variable) that represents mathematical infinity. It is a reference to Number.POSITIVE_INFINIT
To get -Infinity you negate Infinity, or get a reference to it in Number.NEGATIVE_INFINITY

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

Number.MAX_VALUE;          // 1.79769313423157e+308
Number.MAX_SAFE_INTEGER;   // 9007194740991
Number.MIN_VALUE;          // 5e-324
Number.MIN_SAFE_INTEGER;   // -900719925440991
Number.EPSILON;            // 0.000000000000000222044604
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)

7-  Operations that return NaN
0 / 0

8- Math library functions that return value NaN
Math.floor("a")

No comments:

Post a Comment

Translate