javaScript Data Types

javascript support number, string, boolean, array, object, null, undefined data types.

javascript is a loosely typed language.what this means is that you can use the same variable for different types of information.


             var     x;         //now x is undefined 

             x = 'ram';       //now x is  a string
             x = 30;         //now x is a number

primitive data types

  • number
  • string
  • boolean

complex types

  • array
  • object

special data types

  • null
  • undefined

javaScript numbers

you can use integer and double types on your variables, but they are treated as a numeric type.

       var   a  =  20;            //number without decimals
       var   b  =  40.35;        //number  with decimals
       var   c  =  2022e2; //scientific notation
       var   d  =  3044e-4; 
       var   e  =  303e-6;

in javascript all numbers are represented in ieee 754-1985 double-precision floating-point format.this representation permits exact representation of integers in the range -253 to 253 and floating-point magnitudes as large as 1.7976*10308 as small as 2.2250*10-308

numeric literals in javascript can be written in a wide variety of ways, including scientific notation. when using scientific notation(3434e2), the exponent is specified with the letter e or E.

javaScript String data Type

a String value is a sequence of zero or more characters.

a string value always enclosed within single quotes ('') or double quotes ("") .

                  var        name   ='sandeep';

                  var     lastname ="kumar nehra";

you can use quotes inside a string, as long as they don't match the quotes surrounding the string:

                var    name1  ="java script string ";
                var    name2  ="java script'data'type";
                var    name3  ="java\"script\"type";
                var    name4  ='java"script"type';
                var    name5  ='java\'script\'type';

in javascript strings are associated with a string object, which provides methods for manipulation and examination. for example the string object charat() function is used to extract character from strings.

  var  value = "this is javastring";   document.write("character ="+value.charat()) ;

javaScript Boolean Data Types

boolean variables can only have two possible values, true or false.

                  var    equal     = true;
                  var    unequal  = false;

comparisons you make in your scripts always have a boolean outcome.look this javascript code

                  var   y  = (10<=100);      //y value is true, (because 10 less then 100)

javaScript arrays

the array object is used to store multiple values in a single variable.

in javascript an array can be created in three ways:


       var    bike = ["white","honda","1000"];


       var    bike = new  array("white","honda","1000");


       var     bike=new array();


javaScript Objects

the object within javascript is created using the new operator.

         var    a  = new   object();

objects can also be created with the object notation, which uses curly braces.

         var   aa = {};

inside the braces the object's properties are defined as name and value pairs(name:value).the properties are separated by commas:

         var  person = {
                                 name :"javascript datatypes",
                                 lastname:"datatypes in js",

javascript undefined or null

the javascript undefined value is returned when you use an object property that does not exist, or a variable that has been declared, but has never had a value assigned to it.

the javascript null value means the value has been set to be empty.


        var      name ;
        var      empty = null;