JavaScript Data Types
JavaScript has two categories of data types:
1. Primitive Data Types
2. Non-Primitive (Reference) Data Types
1. Primitive Data Types
Primitive types are immutable (cannot be changed) and store single values.
Data Type Example Description
String "Hello" Represents text.
Number 42, 3.14 Stores integers and floating-
point numbers.
Boolean true, false Stores logical values
(yes/no, on/off).
Undefined let x; A variable that has been
declared but not assigned a
value.
Null let y = null; Represents "no value" or
"empty value."
BigInt BigInt(9007199254740991 Used for very large
) numbers.
Symbol Symbol('id') Unique and immutable
value used as object keys.
Example of Primitive Data Types:
```js
let name = "John"; // String
let age = 25; // Number
let isStudent = true; // Boolean
let notDefined; // Undefined
let emptyValue = null; // Null
let bigNumber = BigInt(9007199254740991); // BigInt
let uniqueID = Symbol('id'); // Symbol
[Link](name, age, isStudent, notDefined, emptyValue, bigNumber, uniqueID);
```
2. Non-Primitive (Reference) Data Types
These store multiple values and are mutable (can be modified).
Data Type Example Description
Object {name: "John", age: 25} A collection of key-value
pairs.
Array [1, 2, 3, 4] A list of values.
Function function greet() A block of reusable code.
{ [Link]('Hello'); }
Example of Non-Primitive Data Types:
```js
// Object
let person = {
name: "John",
age: 25
};
// Array
let numbers = [1, 2, 3, 4, 5];
// Function
function greet() {
[Link]("Hello, World!");
}
[Link]([Link], numbers[2]); // Output: John 3
greet(); // Output: Hello, World!
```
3. Special Notes
• JavaScript is dynamically typed, meaning you don’t need to declare a variable type.
• Type Conversion: JavaScript automatically converts data types in some cases.
```js
[Link]("5" + 5); // Output: "55" (string concatenation)
[Link]("5" - 2); // Output: 3 (automatic number conversion)
```
Conclusion
JavaScript has 7 primitive types and 3 non-primitive types. Understanding these helps in
efficient programming and debugging.