Data Types in Javscript


There are following data types in javascript:

1. String
2. Number
3. Boolean
4. Null
5. Undefined

Description with Example(s):

      String Data Type:

When we assign some string value in a variable then that time variable data type is string.

Example:
var str="my name is mac";

Number Data Type:

When we assign following value in a variable then that variable data type is Number.

Example:
var num1 = 10; // integer value including positive and negative values
var num2 = 10.10; // float , decimal value
var num3 = 10e5; // exponential notation

Note: NAN (Not a Number) is used to check that variable is the number or not in javascript.
 
3.       Boolean Data Type:

When we assign a variable as value “true” or “false” then that variable data type is Boolean

Example:
var flag = true;
var flag2 = false;

We use this data type to check condition.


4.       null data type:

When we have a variable and we want to erase the existing value of a variable then that time we assign that variable to “null”.

Example:
var str="albert";
       str=null; //this erase the value of string to null, now str has null data type                (actually it is object data type that hold nothing)

5.       undefined data type:

When we declare a variable but never assign any value to it or use an object property that does not exists then that time variable return “undefined”.

Example:
var str; //when we use this variable then  it show undefined
alert(str);

var str = "test value";
alert(str.count); //it will show undefined because there no property count for the string data type

Comments