JavaScript provides different data types to hold different types of values. These data types are categorized into two main types:
1. primitive data types
2. non-primitive (reference) data types.
var name = "Rahul";
var age = 40;
var isStudent = true;
var x; console.log(x);
var person = null;
| Data Type | Description | Example | Code Example |
|---|---|---|---|
| Object | Represents an instance through which we can access members (properties and methods). | { firstName: "John", lastName: "Doe", age: 25 } |
var person = { firstName: "John", lastName: "Doe", age: 25 };
|
| Array | Represents a group of similar values. | ["Apple", "Banana", "Cherry"] |
var fruits = ["Apple", "Banana", "Cherry"];
|
| RegExp | Represents a regular expression used for pattern matching. | /ab+c/ |
var pattern = /ab+c/;
|
JavaScript is a dynamically typed language, meaning you don't need to specify the type of a variable when you declare it. The type of the variable is determined dynamically based on the value assigned to it. This allows a variable to hold any type of value, such as numbers, strings, etc.