Sunday, October 29, 2017

Javascript Data Types

There are two basic data type in javascript:

Note:

  1. Javascript variable are dynamic type. It means when we assign value to javascript variable then data type of that variable will depend on the type of value the variable holds.
  2. typeOf operator is used to find the data type of the variable.

Primitives
  1. string
  2. boolean
  3. number
  4. undefined
Non Primitives
  1. object
  2. function
string: When we declare a variable by assigning the value to the variable in single quotes i.e. ' ' or in double quotes "". then that variable become of string type.

Example: 

var name="abc";
or
var name='abc';


boolean: When we declare a variable by assigning the value to the variable true/false then that variable becomes of boolean type.

Example:

var flag = true;
or
var flag = false;

number: When we declare a variable by assigning the integer or floating value to the variable then that variable becomes of number type.

Example:

var salary=5000;

var salary=5000.543;

var salary=5.324e+2;

var salary= 5.324e-4;

undefined: When we declare a variable and does not assign any value to it then then that variable becomes of undefined type.

Example:

var name;

Non Primitive:

object: Object data type variables are reference type.

Example:

var name=null; //null is a keyword and its type is object.

var myArr=[1,2,"5"]; // data type of the array is object

var obj={ firstName : 'shshi' , lastName: 'sharma'};

 Note: 

  1.   In javascript we declare object with key-value pair. 
  2.   Key name can be any value. We can use key name as valid string value.
  3.   Value can be any valid javascript supported data type.
function: When we declare a function and assign function into a variable then data type of the variable becomes function.

Example:

var myFunc=function (){
      alert("hello");
}

Hope it helpful to know about javascript data type.

Thanks for reading.