Javascript Datatypes

Value store in a variable is of some datatype. There are the five primitive datatypes in Javascript.

Boolean

Boolean has only two possible values either TRUE or FALSE. Boolean is used in many condition code statements. Comparison operator always returns a boolean value. We can also check the boolean value of other data types by using the boolean casting function. Like All non zero integers and floating values are TRUE, a blank string boolean value is false.

<script>
var str = '';
var str1 = 'Hello World';
var boostr = Boolean(str); 
alert(boostr); // it alerts false
var boostr1 = Boolean(str1); 
alert(boostr1); // it alerts true
</script>

Number

Javascript support all integers and floating points as number data types.

<script>
var a1 = 10;
var a2 = 10.00;    
</script>

All are valid number data types.

Javascript also support octal and hexadecimal numbers.

<script>
var a3 = 062 // Octal value of a number 50
var a4 = 0xA // Hexadecimal for 10   
</script>

Null

Null data type is an empty object pointer and has only one value 'null'. The below code alert object as output.

<script>
var tree = null;
alert(typeof tree);
</script>

String

The string is a sequence of characters enclosed within double and single quotes.

<script>
var str1 = 'This is string datatype';
var str2 = 'This is string datatype';    
</script>

Undefined

When a variable is declared using var, but not defined, then by default it is initialized as undefined.

<script>
var tree;
alert(typeof tree);
</script>

Object

Object is a complicated datatype. This data type is created by using an instance of object.

var obj = new Object();




Read more articles


General Knowledge



Learn Popular Language