In JavaScript,
data types can be categorized into two main groups: primitive and non-primitive (also known as reference types) data types. Here's an explanation of each:
1. Primitive Data Types:
These are immutable data types that store a single value.
a. Boolean: Represents a logical value, either true or false. It is commonly used for conditions and branching in JavaScript.
let isTrue = true;
let isFalse = false;
console.log(isTrue); // Output: true
console.log(isFalse); // Output: false
b. Number: Represents numeric values, including integers and floating-point numbers.
let count = 10;
let price = 4.99;
console.log(count); // Output: 10
console.log(price); // Output: 4.99
c. String: Represents a sequence of characters enclosed in single or double quotes. Strings are used to represent textual data.
let message = "Hello, world!";
console.log(message); // Output: Hello, world!
d. Null: Represents the intentional absence of any
object value. It is often assigned to a variable to indicate that it has no value or that the value is unknown.
let value = null;
console.log(value); // Output: null
e. Undefined: Represents an uninitialized or undeclared variable. If a variable is declared but not assigned a value, it will have the value of undefined.
let variable;
console.log(variable); // Output: undefined
f. Symbol: Represents a unique identifier. Symbols are typically used as keys in objects to avoid naming conflicts.
let id = Symbol("unique");
console.log(id); // Output: Symbol(unique)
2. Non-Primitive (Reference) Data Types:
These are mutable data types that store references to memory locations rather than the actual values.
a. Object: Represents a collection of key-value pairs and provides a way to group related data and functionality together. Objects can be created using object literals {}, constructor functions, or the class syntax introduced in ECMAScript 2015.
let person = {
name: "John",
age: 30,
isAdmin: false
};
console.log(person); // Output: { name: 'John', age: 30, isAdmin: false }
b. Array: Represents an ordered list of values. Arrays can hold values of any type, and their elements are accessed using numeric indices starting from 0.
let numbers = [1, 2, 3, 4, 5];
console.log(numbers); // Output: [1, 2, 3, 4, 5]
c. Function: Represents executable code that can be invoked and performs a specific task. Functions are one of the fundamental building blocks in JavaScript and can be defined using function declarations or function expressions.
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Alice"); // Output: Hello, Alice!
Non-primitive data types, such as objects, arrays, and functions, are passed by reference, meaning that when you assign them to a variable or pass them as arguments to functions, you are working with a reference to the original value stored in memory. Primitive data types, on the other hand, are passed by value, meaning that when you assign them to a variable or pass them as arguments, a copy of the value is created.