Datatypes
In JavaScript, the type of the data can be classified into 2 types. They are
- Primitive
- Non-Primitive
Primitive data types
There are 6 primitive data types in JavaScript:
- Number
- String
- Boolean
- Null
- Undefined
- Symbol
Number
In JavaScript, the number data type can represent both integer as well as decimal numbers.
Example
let avg = 34.84; // decimal number
let age = 35; // integer number
We can also represent large or small numbrs with scientific notation.
Example
let x = 123e4; // 1230000
let y = 123e-4; // 0.0123
String
In JavaScript, string is the series of characters enclosed in double quotes.
Example
let name = "ReadersBuddy";
Boolean
Boolean variable can have one of the two values: true or false. This type is used to store only true or false values.
Example
let isEmployee = true;
Null
In JavaScript, null means nothing or empty. If we need to set a value to empty, then we can use the null value.
The data type of null is an object.
Example
let salary = null;
console.log(typeof salary); // object
Undefined
In JavaScript, a variable without a value is always undefined. The value undefined means ???value is not assigned???.
Example
let salary;
console.log(salary); // undefined
Symbol
The JavaScript ES6 introduced a new primitive data type called Symbol. Symbols are immutable (cannot be changed) and are unique.
Example
// two symbols with the same description
const item1 = Symbol('hi');
const item2 = Symbol('hi');
console.log(item1 === item2); // false
Though item1 and item2 both contains the same value, they are considered different.
Accessing Symbol
To access the symbol description, we use description property with (.) operator.
Example
const name = Symbol('ReadersBuddy');
console.log(name.description); // ReadersBuddy