JavaScript Data types

Data types in JavaScript

Data types are the basic building blocks of any programming language. Like other computer languages, JavaScript has built in data types which are used to build your program. Data types specify what type of data can be stored in your program. The JavaScript data types can be categorised into three categories.

Primitive data types

Primitive data types contain String, Number and Boolean data types.

Composite data types

Composite data types include Object, Array and Functions.

Special data types

They include Undefined and Null data types.

The String data type

The string data types are required to hold characters sequentially. The strings are written within single or double quotes. The example is shown below.

var name='Rakesh';
var address="Jammu";

The Number data type

The Number data type is used to represent numbers which can be positive or negative numbers. The numbers can also be in decimal form. The Number data type also include some special values which are infinity, -infinity and NaN. Infinity is the result of dividing a non zero number by 0. NaN means Not a Number and it is a result of an invalid or undefined mathematical operation like 0 dividing 0.

var x=5;
var y=15.63;
var z=4.58e+5
alert(20/0)  //Infinity

The Boolean data type

The Boolean data type takes two value- true and false. It works like yes or no.

var isWorking=true;
var isWorking-false;

The Object data type in JavaScript

An object data type is used to store a collection of data which may include number, string etc. An object contains key-value pairs. A property or key is always a string but the value can be string, number, boolean, array, functions and objects. The example is given below to show how object is declared.

var myObject={};
var Employee={"name":"Rajesh", "Age":25, "Address":"Jammu"};
var Car={
         "Name":"Santro",
         "Year":2020,
         "Make":"Hyundai"};

The Array data type in JavaScript

An array is a type of object which is used to store data of multiple values in single variable. The values are also called as elements of an array. The element has a numerical position in an array which is called as index. The array may take data of any data type like numbers, strings, booleans, functions, objects etc. The array index starts from zero so that the first element of an array becomes arr[0]. The simplest way to create an array is by specifying the array elements as a comma-separated list enclosed by square brackets, as shown in the example below:

var student=["Rakesh", "Vijay", "Viraaj"];
alert(student[1]);     //Will give result as Vijay

Functions in JavaScript

A JavaScript function is a block of code designed to perform a particular task. It is executed when something calls it. Let us write a code for function.

function marks()
{
  return 70%;
}
marks();

We will learn more about functions in later topics.

The Undefined data type

The undefined data type can have only one value which is undefined. It is obtained when we declare a variable without assigning a value to it.

var x;
var name="Vijay";
alert(x);     //Will give Undefined
alert(name)   //Will give output Vijay

The Null data type

This is another special data type that can have only one value-the null value. A null value means that there is no value. It is not equivalent to an empty string (“”) or 0, it is simply nothing.

var x=null;
alert(x);    ///Output as null