JavaScript Data Types

In JavaScript, a data type is the type of data that is being used. In JS there are various data types like numbers, strings, and many more. The data type is an important aspect of programming, as it is essential to know about the type of data to operate on it.

What if I ask you to add 2 to “mango”? Does it make any sense?

2 is a number and “mango” is a string. However, doing so will produce a result something like 2Mango.

The point here is that you should have knowledge about the data types to justify the expressions.

In this topic, we will learn about the data types in JavaScript.

To be more specific, JavaScript has the following data types-

  • Numbers (Integers and floating-point numbers)
  • Strings.
  • Boolean
  • Undefined.
  • null
  • Array
  • Object

The first five data types mentioned above are called primitive data types and the latter are called non-primitive data types.

Numbers

In JS, the numbers can be with or without decimal.

For example-

var x = 12;
var y = 14.5;

Note: We can change the type of value a variable is already holding. This is called type conversion which we will see in a later tutorial.

For the representation of very small or very large numbers, we can also use scientific or exponential notation.

For example-

var a = 12e4; //4836

Strings

A string is simply a collection of characters. For example “apple”.

A String is always written inside quotes (Single or double).

For example-

var fruit = "apple";
var vegetable = "potato";

Even a number can also be represented as a string.

var numstr = "1234";

Boolean

A Boolean type variable can have only one of the two values – true or false.

 var b = true;

A comparison expression always returns a Boolean value.

var x = 1;
var y = 2;
(x < y) //returns true
(x == y) //return false
(x > y) // return false

Because the value inside x is less than that of y, the expression will return true. And so, as the value of x is neither equal to nor greater than the value of y, the latter two expressions will return false.

Booleans are usually used in conditional statements and loops.

Undefined

This data type represents that no value has been assigned to a variable.

For example-

var num;
console.log(num);

Output:

undefined

It is also possible to explicitly assign a value of undefined to a variable, however it is not recommended to do so.

var num = undefined;
console.log(num);

Output:

undefined

null

In JavaScript, null is a data type that represents an empty value. null means nothing.

var num = null; //num is empty

Array

In programming and JavaScript, an array can store multiple items at a time.

In JavaScript, an array can be declared by using square brackets.

For example-

var myarr = [];

In the above statement, we have declared an empty array named ‘myarr’.

An array can be defined at the time of declaration. That means you can assign the values at the time of array creation.

var myarr = ["apple", "mango"];

In the above expression, we have created an array named myarr containing two string values “apple” and “mango”.

The values of the array can be fetched with the help of indexes. You will learn more about arrays in a separate tutorial.

Object

A JavaScript object ( just like an array ) is also a collection type, except that you need to specify pairs of indexes and values.

In JavaScript, an object can be created by using curly braces.

var obj = {"name" : "Aman", "age" : 21};

A value from an object can be fetched by using the dot operator.

var obj = {"name" : "Aman", "age" : 21};
document.write(obj.name); 

Output:

Aman