What are the different data types in JavaScript?
JavaScript has several built-in data types that are used to represent different kinds of values. Here are the different data types in JavaScript:
Number:
Represents numeric values. It can be an integer or a floating-point number. For example:
var age = 25;
var temperature = 98.6;
String:
Represents a sequence of characters enclosed in single or double quotes. For example:
var name = “John Doe”;
var message = ‘Hello, world!’;
Boolean:
Represents a logical value, either true or false. For example:
var isTrue = true;
var isFalse = false;
Undefined:
Represents a variable that has been declared but not assigned a value. It is the default value for uninitialized variables. For example:
var x;
console.log(x); // Output: undefined
Null:
Represents the intentional absence of any object value. It is assigned to a variable to explicitly indicate that it has no value. For example:
var person = null;
Object:
Represents a collection of key-value pairs, where each value can be of any data type, including other objects. For example:
var person = {
name: “John Doe”,
age: 25,
isAdmin: true
};
Array:
Represents an ordered list of values enclosed in square brackets. Array elements can be of any data type, including other arrays and objects. For example:
var fruits = [“apple”, “banana”, “orange”];
var numbers = [1, 2, 3, 4, 5];
Symbol:
Represents a unique identifier. Symbols are often used as property keys in objects to avoid naming conflicts. For example:
var id = Symbol(“uniqueId”);
var obj = {
[id]: “12345”
};
These are the primary data types in JavaScript. It’s worth noting that JavaScript is a dynamically typed language, which means variables can hold values of any data type, and the data type can change dynamically during runtime.
