What is a Data Type?
A data type tells us what kind of value we are working with in a program. It helps JavaScript understand whether a value is text, a number, a true/false value, or something else.
For example:
let name = "Abishek";
let age = 25;
let isStudent = true;
Here, "Abishek" is a String, 25 is a Number, and true is a Boolean.
Types of Data Types in JavaScript
JavaScript data types are mainly divided into two categories:
- Primitive Data Types
- Non-Primitive Data Types
Primitive data types represent a single value, while non-primitive data types are used to store more complex data.
What are Primitive Data Types?
Primitive data types are the basic data types in JavaScript. They represent a single value and are not objects.
JavaScript has 7 primitive data types:
- String
- Number
- BigInt
- Boolean
- Undefined
- Null
- Symbol
Primitive data types are used to store simple values such as text, numbers, and true/false values.
Primitive Data Types are Immutable
All primitive data types in JavaScript are immutable. This means their value cannot be changed after it is created.
For example:
let name = "Abishek";
name[0] = "X";
console.log(name);
Output:
Abishek
The string cannot be changed directly. However, we can assign a completely new value to the variable:
let name = "Abishek";
name = "Kumar";
console.log(name);
Here, the original "Abishek" value was not changed. The variable was simply assigned a new value.
1. String
A String is used to store text or a sequence of characters.
let name = "Abishek";
let message = "Hello World";
let msg = `Hi`;
Strings can be written using single quotes, double quotes, or backticks.
2. Number
The Number data type is used to store numerical values, including integers and decimal numbers.
let age = 25;
let price = 99.50;
3. BigInt
BigInt is used to store very large integer values that cannot be safely represented by the normal Number type.
let bigNumber = 12345678901234567890n;
The n at the end indicates that the value is a BigInt.
4. Boolean
A Boolean has only two possible values: true or false. It is commonly used for conditions and decision-making.
let isLoggedIn = true;
let isCompleted = false;
5. Undefined
Undefined means a variable has been declared but no value has been assigned to it.
let username;
console.log(username);
Output:
undefined
6. Null
Null represents the intentional absence of a value. It is used when we want to indicate that a variable currently has no value.
let selectedUser = null;
Here, null means there is currently no selected user.
7. Symbol
Symbol is used to create unique values. It is mainly useful when we need unique property keys in objects.
let id = Symbol("id");
Every Symbol is unique, even if two Symbols have the same description.
Primitive data types are the basic building blocks of JavaScript. JavaScript has seven primitive data types: String, Number, BigInt, Boolean, Undefined, Null, and Symbol.
All primitive values are immutable, which means their values cannot be modified after they are created. Understanding these data types is important because they are used throughout JavaScript programs to store and work with different kinds of values.
Top comments (0)