i

JavaScript

Primitive Data Type

Data Types:

Data type is an attribute that tells what form of data a variable can hold. Data types can be integers, floating point numbers, strings, boolean etc.

Primitive Data Type:

Primitive Data types are data types wherein the variable can hold only one simple single data value. It cannot store a collection of data or other properties.

JavaScript has four primitive data types:

  • String

  • Number

  • Boolean

  • Undefined

String:

String is used to represent a sequence of characters. In JavaScript, a string must be surrounded by quotes. Single quotes (‘hi’), double quotes (“hi”) or backticks (`hi`) are used to represent strings. Practically, single quotes and double quotes are treated the same way in JavaScript, there is no difference between them. Backticks have enhanced functionality. They can be used to embed a variable into a string or concatenate two strings.

Example:

let string1 = “Hi”;

let string2 = ‘I am Ajay’;

let string3 = `${string1}, ${string2}`;

In the above example, string1 and string2 are two variables that hold two different strings. In string3, we will be concatenating the values of string1 and string2. ${string1} will give “Hi” and ${string2} will be ‘I am Ajay’. The value of string3 will be “Hi, I am Ajay”.

Backticks can also be used to evaluate an expression and use it in a string.

let sum = `The result of 7 + 9 is {7 + 9}`;

The value of sum will be “The result of 7 + 9 is 16”.

Number:

Number is used to represent numeric values. In JavaScript, Number data type is used to represent both integers and floating point numbers. Apart from regular numbers, there are two “special numeric values” that come under Numbers data type in JavaScript. They are Infinity and NaN. When a number is divided by zero, we get infinity and when there is any computational error, we get Nan.

Example:

let a = 100;

let b = 90;

let c = 45 / 0;

let d = “abc” / 100;

In the above examples, the value of c evaluates to ‘infinity’ and the value of d evaluates to ‘NaN’ because arithmetic operations cannot be performed on strings. Though the values of c and d are not numbers in real world, in JavaScript they will be considered as numeric data type.

Boolean:

Boolean data type is used to represent a Boolean value - true or false. This data type is generally used to store yes/no values.

Example:

let isWeekday = true;

let isGreater = a > b;

In the above examples, ‘isWeekday’ will have the value true if the day of the week is between Monday – Friday. This variable can be set to false on Saturdays and Sundays. The value ‘isGreater’ is computed based on the expression a > b, (i.e.), if a = 7 and b = 5, then ‘isGreater’ will evaluate to true whereas if a = 5 and b = 7, then the value of ‘isGreater’ will be false.

Undefined:

Undefined will be used to represent any undefined value. Undefined means that no value is assigned to that variable. The value of a variable will be undefined when it is declared but not assigned any value.

Example:

let x;

In this case, the value of x will be undefined, since it is not assigned with any value.

let x = undefined;

The above way of assigning the value ‘undefined’ to a variable is also valid; however this is not recommended. ‘Null’ must be used to assign an empty value to a variable and ‘undefined’ is used for checks to verify if the variable is assigned a value or not.

Difference between Null and Undefined:

In JavaScript, null refers to an empty variable. It does not mean referring to a non-existing object or a null pointer as in other languages. Undefined also means that no value is assigned to that variable. Though both look similar, they have different data types. The data type of a variable that is undefined is ‘Undefined’ whereas the data type of a variable that is null is ‘Object’.