What are Primitive Data Types?
- Undefined: Represents an uninitialized variable.
- Null: Represents the absence of any object value.
- Boolean: Represents a logical entity that can have two values: true or false.
- Number: Represents numeric values.
- String: Represents textual data enclosed within single quotes (”) or double quotes (“”).
- Symbol: Introduced in ECMAScript 6, represents a unique identifier.
The Characteristics of Primitive Data Types
All primitive data types have the following characteristics:
Primitive values are immutable, which means their values cannot be changed once they are created. Any operation on a primitive value returns a new value rather than modifying the original value.
- Pass By Value:
When you assign a primitive value to a variable or pass it as an argument to a function, the variable or function parameter holds a copy of the original value. Changes made to the copy do not affect the original value.
- Comparison by Value:
Primitive values are compared by their value. If two primitive values have the same value, they are considered equal. For example, 5 is equal to 5, and ‘hello’ is equal to ‘hello’.
Working with Primitive Data Types
let age = 25; // number
const name = ‘John’; // string
var isStudent = true; // boolean
You can perform various operations on primitive data types such as arithmetic operations on numbers, string concatenation, boolean logic operations, and more.
Casting to Primitive Types
- String(value): Converts a value to a string.
- Number(value): Converts a value to a number.
- Boolean(value): Converts a value to a boolean.
You can use these functions when you want to perform specific operations that require values of specific types.