Data Types is used to mention that the variable holds specific type data to handle and process without an issue.
There are 3 common variables in JavaScript.
Primitive data types are the most basic building blocks of JavaScript. They represent individual piece of values, not objects.
The list of primitive data types are numbers, strings, booleans, null, undefined, symbols.
It represents numerical values, including integers and floating-point numbers.
let age = 42; // Number Integer
let pi = 3.14; // Decimals Floating-point number
It represents sequences of characters.
let name = "Alice";
let message = "Welcome!";
It represents true or false values.
let isAdult = true;
let isRaining = false;
It represents the absence of a value.
let user = null; // Indicates that the user is not logged in
It represents a variable that has not been assigned a value.
let result;
console.log(result); // Output: undefined
It represents a unique identifier.
Syntax: let uniqueSymbol = Symbol("mySymbol");
const PRIVATE_KEY = Symbol("private");
Non-primitive data types in JavaScript are complex data structures that can hold multiple values or properties. They are mutable and compared by reference.
The list of non-primitive data types are arrays and objects.
It is a ordered collections of elements.
Syntax: let array = [1, "hello", true];
let fruits = ["apple", "banana", "orange"];
let numbers = [1, 2, 3, 4, 5];
See More on this Reference
It is a collections of key-value pairs
let object = {
name: "Alice",
age: 30,
isStudent: true
};
See More on this Reference
- Data type:
- Primitive Data Types: It represent individual values and single piece of data.
- Non-Primitive Data Types: It represent collections of data.
- Mutability:
- Primitive Data Types: immutable (their values cannot be changed directly).
- Non-Primitive Data Types: objects are mutable (their properties can be modified).
- Comparison:
- Primitive Data Types: It compared by value.
- Non-Primitive Data Types: It compared by reference.