JavaScript dynamic data types are explained as follows:
var x; // Now x is undefined var x = 5; // Now x is a Number var x = "Bill"; // Now x is a String
For elaboration of javascript data types, explained as follows:
- String: A string can be any text inside quotes. You can use single or double quotes
var str="It's alright"; var str ="He is called 'Akon'"; var str ='He is called "Akon"';
- Number:
var num=33.00; // Written with decimals var num=33; // Written without decimals var exp=333e5; // 33300000 var exp=333e-5; // 0.00333
- Boolean:
var x=true; var y=false;
- Array:
//First Way of Declaration var friendsName=new Array(); friendsName[0]="John"; friendsName[1]="Sachin"; friendsName[2]="Naren"; //Second Way of Declaration var friendsName =new Array("John","Sachin","Naren"); //Third Way of Declaration var friendsName =["John","Sachin","Naren"];
- Object:
var friends={ firstname : "Sachin", lastname : "Tendulkar" }; //Access the object value as follows: document.write(friends.lastname + "<br>"); document.write(friends["lastname"] + "<br>");
- Undefined and Null
Var frineds; //undefined variable Var frinds=null; // null variable
When you declare a new variable, you can declare its type using the “new” keyword:
var carname=new String; var x= new Number; var y= new Boolean; var cars= new Array; var person= new Object;