In this lesson, we will learn about the different JavaScript data types available with the help of examples. Basically, data types define what kind of data can be stored and manipulated within a program. There are eight different types of javascript data types divided into two categories.
Primitive Data Type
- Boolean type
- Null type
- Undefined type
- Number type
- BigInt type
- String type
- Symbol type
Non-primitive Data Type
- Object type
Primitive Data Type
Predefined data types provided by JavaScript are known as primitive data types. They are also known as in-built data types.
- Boolean Type
This data type represents logical entities. There are only two possible values for Boolean
: true
or false
. For example,
const firstValue = true;
const secondValue = false;
- Null Type
Null
is a special value in JavaScript that represents an empty or unknown value. For example,
const number = null;
- Undefined Type
The undefined
datatype represents the value that is not assigned. A variable whose value has not been assigned has the value undefined. For example,
let name;
let name = undefined;
Here, in both cases the value of the name is undefined.
- Number Type
JavaScript uses the number
datatype to represent both integer and floating-point numbers. It can represent numbers less than (253 – 1) and more than -(253 – 1). For example,
const numb = 100; // Integer number
const numb = 100.05; //Floating number
- BigInt Type
The BigInt
type is a numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit for Numbers. A BigInt
number is created by appending n to the end of an integer. For example,
// BigInt value
const value1 = 900719925124740998n;
// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "900719925124740999n"
// Adding big integers and number
const value2 = 900719925124740998n;
const result2 = value2 + 1;
console.log(result2); // Error! BitInt and number cannot be added
- String Type
String
datatype is used to store textual data. In JavaScript, the strings can be represented as follow:
- Single quotes:
'Hello'
- Double quotes:
"Hello"
- Backticks:
`Hello`
For examples,
const str = 'Hello'
const str2 = "World"
const str3 = `Hello ${str2}`
Backticks
are generally used if we need to include dynamic value into a string.
- Symbol Type
A value having the data type Symbol
can be referred to as a symbol value. Symbol
is an immutable primitive value that is unique. Two Symbols created from the same key are not equal. For example,
const value1 = Symbol('hello');
const value2 = Symbol('hello');
value1 === value2 //False
Non-primitive Data Type
The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types.
- Object Type
The object
datatype is a complex type of data that allows us to manage collections of data. For example,
const person = {
firstName: 'ram',
lastName: null,
age: 25
};
Conclusion
In this lesson, you have learned how about JavaScript data types with some examples.