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")

Javascript Arrays and Objects Concept with code and Logic and sample example

Defining an array in JavaScript-
                  var    javascript_array_variable = [];


   An array is a set of variables and objects. For example:
         var   fav_Fruits = ["mango", "orange", "banana"];
         var  fav_cars = ["Toyota", "Ferrari", "Lexus"];
         var best_friend = ["Sandeep", "Shiv", "Amit"];
         var fav_no = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31];
         var randomVariables = [2, "any type works", undefined, null, true, 2.51];



To print array value we use-
   javascript_array_variable=["zero", "one", "two"];
    window.alert(javascript_array_variable [0]);

   Output Value- Zero
 
array in javascript work same as in other language like java, c.


An object is a group of values;
unlike arrays, we can do something better than them:
myObject = {};
sanddep = {
                       firstname: "Sandeep",
                       lastname: "Chauhan",
                       fullname: "Sandeep Chauhan"
           };
gurjar = {   
                 firstname: "amit",   
                 lastname: "gurjar", 
                 fullname: "amit gurjar"
             };
 
 window.alert(sandeep.fullname);   
//Output- John Doe
window.alert(gurjar.firstname);
// Billy
Rather than making an array ["Sanddep gurjar", "Sandeep"] and calling myArray[0],
we can just call sandeep.fullname and gurjar.fullname

JavaScript Variables Code Syntax uses and Demo Example

Syntax for defining of variable-

1-String Type
      var   javascript_String_variable="Value what we want to assign ?";

2-Integer
      var    javascript_Integer_variable=3

3-Long
      var    javascript_long_variable= 123123123;
        // 64-bit number (from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

4-Float
    var   javascript_float_variable= 10.6;
       // 32-bit floating-point number (decimal)

5-Double
    var javascript_double_variable= 9310141419482.22; // 64-bit floating-point number

6-Boolean
     var javascript_boolen_variable= true; // 1-bit true/false (0 or 1)

7-Boolean
     var javascript_boolen_variable= false;'

8-NAN
     var javascript_NAN_variable= NaN;

      var javascript_NAN_variable= 0/0; // NaN: Division by Zero is not possible

9- Not defined
      var javascript_notdefined_variable; // undefined: we didn't define it to anything ye

10- Null
      var javascript_null_variable= null;

Translate