Saturday, 19 September 2020

How to Determine javascript variable Data Types

 


Primitive Data Types :


boolean : true or false

null    : no value

undefined : a variable declared, but has no value

number    : integers,decimals,float, etc

string    : a series(array) of characters


Object Data Types :


new Array    : A collection of values

new Error    : contains a name and an error message

new Function : A block of code

new Object   : A wrapper around any type

new RegExp   : A regular expression

new Boolean  : An object that contains true or false

new Number   : An object that contains a numeric value

new String   : An object that contains a character(s)


typeof Operator :


1.Returns the data type of the passed in expression.

2.A string value is returned such as : 'string','number','object', etc.

  

ex : console.log(typeof "Hello"); // 'string'

     console.log(typeof 4);       // 'number'

     console.log(typeof (4*2));   // 'number'


   let _products =[

       {

     productID : 680,

name      : "HL Road Frame - Black, 58",

color     : "Black",

productNumber : "FR-R92B-8",

standardCost  : 1059.31,

listPrice     : 1431.50    

   },

       {

     productID : 707,

name      : "Sport-100 Helmet, Red",

color     : "Black",

productNumber : "UL-R92B-R",

standardCost  : 1059.31,

listPrice     : 1431.50    

   },

       {

     productID : 660,

name      : "HL Road Frame - Red, 56",

color     : "Red",

productNumber : "FR-R92B-8",

standardCost  : 1059.31,

listPrice     : 1431.50    

   }

      ];    


 function typeofSample(){

   let product =_products[0];

   let introDate=new Date();

   let strValue = new String();

   let isActive = false;

   let result;

   let value = null;

   

   console.log("_products = " + typeof _products);                          // object

   console.log("product = " + typeof product);                              // object

   console.log("product.productID = " + typeof product.productID);          // number

   console.log("product.productNumber = " + typeof product.productNumber);  // string

   console.log("strValue = " + typeof strValue);                            // object

   console.log("introDate = " + typeof introDate);                          // object

   console.log(" Result = " + typeof result);                               // undefined

   console.log("isActive = " + typeof isActive);                            // boolean

   console.log("value = " + typeof value);                                  // object

   console.log("typeofSample() = " + typeof typeofSample);                  // function

 

 }

 

 Object Data type / Constructor :

 

1.All object data types inherit from Object (not primitives).

2.Object has a constructor property, and this constructor property returns a reference to the object itself.

3.Object literals and primitives are cast to object for display.



function constructorSample(){


   let product =_products[0];

   let introDate=new Date();

   let strValue = new String();

   let isActive = false;   

   let value = null;

   

   console.log("_products = " + _products.constructor.toString());                          // function Array()         

   console.log("product = " +  product.constructor.toString());                             // function Object()

   console.log("product.productID = " +  product.productID.constructor.toString());         // function Number() 

   console.log("product.productNumber = " +  product.productNumber.constructor.toString()); // function String()

   console.log("strValue = " +  strValue.constructor.toString());                           // function String()

   console.log("introDate = " +  introDate.constructor.toString());                         // function Date()               

   console.log("isActive = " +  isActive.constructor.toString());                           // function Boolean()                           

   console.log("typeofSample() = " + typeofSample.constructor.toString());                  // function Function()

 

 }  


Helper functions for the Constructor property :


ex : 

   let introDate = new Date();

   let result;

   let value = null;

   // use helper functions that return true/false

   

   console.log("_products is Array? = " + isArray(_products));                           // true

   console.log("introDate is Date? = " + isDate(introDate));                             // true

   

   // Be sure to check if something is null prior to using

   console.log("result = " + isNullorUndefined(result)? 'null/undefined': result);       // null/undefined

   console.log("value = " + isNullorUndefined(value)?'null/undefined':value);            // null/undefined


function isArray(value){

  return value.constructor.toString().indexOf("Array")>-1;


function isDate(value){

  return value.constructor.toString().indexOf("Date") >-1;

}


function isNullorUndefined(value){

  return value ===null || value === undefined;

}


The instanceof Operator :


instanceof is different than the typeof because this instanceof 

tests if a specific object inherits from the Object data type (not a primitive).


instanceof tests for a specific type of object or for object itself.


 ex :

 

 // Using the instanceof operator

    function instanceofSample() {

      let prod = new Product(680, "HL Road Frame - Black, 58",

        "FR-R92B-58");

      let dt = new Date();

      let name = new String("Product Name");

      let value = "A simple string";


      console.log("prod instanceof Product = " + (prod instanceof Product).toString());    // prod instanceof Product = true

      console.log("prod instanceof Object = " + (prod instanceof Object).toString());      // prod instanceof Object = true

      console.log("dt instanceof Date = " + (dt instanceof Date).toString());              // dt instanceof Date = true

      console.log("dt instanceof Object = " + (dt instanceof Object).toString());          // dt instanceof Object = true

      console.log("name instanceof String = " + (name instanceof String).toString());      // name instanceof String = true

      console.log("name instanceof Object = " + (name instanceof Object).toString());      // name instanceof Object = true

      console.log("value instanceof String = " + (value instanceof String).toString());    // value instanceof String = false

      console.log("value instanceof Object = " + (value instanceof Object).toString());    // value instanceof Object = false

    }

 

 function Product(id, name, number) {

      this.productID = id;

      this.name = name;

      this.productNumber = number;

      this.color = "Black";

      this.standardCost = 10;

      this.listPrice = 30;

    }


Note : the instanceof only checks for object data types .primitives are not objects.


1. typeof for checking type.

2. instanceof for checking what type of object.  


Note : 

we can use constructor property on both objects and primitives.

Remember that with the primitives, it can actually cast it into an object,

and then we have that constructor property available to us.


No comments:

Post a Comment