Data types in JS


Data types are the type of data value a variable stores. They are basically types of data that can be used and manipulated in a program.

For example, numbers (2,4,6), strings ("Hello", "welcome") etc.


Data types in JS are mainly divided into two 2 categories-


1.Primitive types


Integers- They represent numeric value such as 7, 9, 65 etc.


//JS program to demonstrate integer data types


<DOCTYPE! html>

<html>

<body>

<script>

var a = 54;

document.write (typeof a);

</script>

</body>

</html>


OUTPUT


number


In the example above, the datatype of variable a is printed as output i.e. number.


Note: typeof is used to get the datatype of any variable.


String- They represent sequences of characters such as "Hello", "names". Strings are written in quotation (single/double) marks.


Note: Any data type value written as quotes are considered as strings like "75", 'true', 'world' etc.

//js program to demonstrate string data type


<DOCTYPE! html>

<html>

<body>

<script>

var a = 'str';

document.write (typeof a);

document.write (" ");

a = '56';

document.write (typeof a);

document.write (" ");

a = 'false';

document.write (typeof a);

</script>

</body>

</html>


OUTPUT


string string string


Here, in the example above, in each case, the output is string. It is because data values of any datatype when written in quotes are considered as string data types.


Boolean- Represents boolean values either true or false. They are not written in quotes.

Example 1


//js program to demonstrate boolean datatype.

<DOCTYPE! html>

<html>

<body>

<script>

var a = true;

document.write (typeof a);

</script>

</body>

</html>


OUTPUT


boolean

In this example, variable a is assigned true without quotes. Hence it is a boolean datatype.


Example 2

//js program to test conditions

<DOCTYPE! html>

<html>

<body>

<script>

var a = 5 > 4;

document.write (a);

</script>

</body>

</html>



OUTPUT


true


In the example above, 5 > 4 is true. So, the output is true, which is a boolean datatype.


Null- represents null i.e. no value at all. They return a null value.

//js program to demonstrate null values

<DOCTYPE! html>

<html>

<body>

<script>

var a = null;

document.write (typeof a);

</script>

</body>

</html>


OUTPUT


null


undefined (also known as trivial or other data types). They represent variables which are not defined or not assigned any data value.


//js program to demonstrate undefined data types

<DOCTYPE! html>

<html>

<body>

<script>

var a;

document.write (typeof a);

</script>

</body>

</html>


OUTPUT


As, in the example above, a is declared without assigning it any value, hence it is undefined data type.


2. Composite/Non-primitive types


Objects- Represent instances through which we can access members. They store collections of data written under curly braces, {}. It contains properties and values.


//js program to demonstrate objects type

<DOCTYPE! html>

<html>

<body>

<script>

var a = {'x' : 10, 'y' : 15};

document.write (typeof a);

</script>

</body>

</html>


OUTPUT


object

 

In the above program, variable 'a' has a collection of properties and their respective values. It is an object data type.


Arrays- Represent a group of similar values. JavaScript considers arrays as objects. So they return object data types.


// js program to demonstrate array data type

<DOCTYPE! html>

<html>

<body>

<script>

var a = ['x', 'y'];

document.write (typeof a);

</script>

</body>

</html>

OUTPUT

object


In JS, arrays are of object type.