Datatypes in Javascript

Javascript supports various type of datatypes such as number, string, object etc. To declare a variable we use the keyword “var”

variable declaration in Javascript:

Syntax:

var num=5;

Unlike c, c++ or java, in javascript we don’t require the type of data to declare. Browser engine can understand from the assigned value the type of data.

var val=5; // integer value assigned to the variable val

var val=”javascript”; //Javascript string is assigned to the variable val

var val=5.2; // a decimal value 5.2 is assigned to the variable val

var val=true; // a boolean value true is assigned to the variable val

Note: Browser can understand from the assigned value the type of data assigned to the variable.

To better understand the above point, lets take one example.

<script>

var a=12;

var b=5;

var c=a/b;

alert(c);

</script>

The above code would print 2.4

In C,C++ or Java, we would have declared the type the variable that would have stored the result of the division operation. That is float. But in javascript, we are free from this because browser itself going to understand from the result that it is decimal value and it assigns the value to the variable c accordingly.