There are five types of primitive data types.
- String
- Number
- Boolean
- Undefined
- Null
String
The string data type in JavaScript can be any group of characters enclosed by a single or double-quotes or by backticks.
let name = "Awais Butt"; // Using double quotes
let info = 'data'; // Using single quotes
Numbers
JavaScript has only one type of numbers. Numbers can be written with, or without decimals.
let number = 250; // integer value
Boolean
The boolean data type has only two values, true and false. It is mostly used to check a logical condition. Thus Booleans are logical data types which can be used for comparison of two variables or to check a condition.
let num1 = 5;
let num2 = 5;
(num1 == num2) // Returns true
Undefined
In JavaScript, a variable without a value, has the value undefined. The type is also undefined.
let name; //name is undefined and its type also undefined
Null
The null in JavaScript is a data type that is represented by only one value, the ‘null’ itself. A null value means no value.
let n = null;
console.log(n); // This returns null
If we check the data type of a using the typeof operator, we get:
typeof(n); // This returns object
This means the type of a null value is an object, not null.
Top comments (0)