Data Types in JavaScript
JavaScript is a dynamically-typed language, which means that the data type of a variable can change during the execution of a program. JavaScript has several built-in data types, which can be classified into two main categories: primitive data types and non-primitive data types.
Primitive Data Types
Primitive data types are the basic building blocks of JavaScript. They are immutable, which means that their values cannot be changed once they are created. JavaScript has the following primitive data types:
- Number: This data type represents both integers and floating-point numbers. It can also represent special numeric values like
Infinity
andNaN
(Not a Number).
let age = 30;
let pi = 3.14;
let negativeNumber = -10;
let specialNumber = Infinity;
let notANumber = NaN;
- String: This data type represents a sequence of characters. Strings can be enclosed in single quotes (
'
), double quotes ("
), or backticks (`
).
let name = "John Doe";
let message = 'Hello, world!';
let multilineString = `This is a
multi-line
string.`;
- Boolean: This data type represents a logical value, either
true
orfalse
.
let isStudent = true;
let isEmployed = false;
- Undefined: This data type represents a variable that has been declared but not assigned a value, or an object property that does not exist.
let myVariable;
console.log(myVariable); // Output: undefined
- Null: This data type represents the intentional absence of any object value.
let myObject = null;
- Symbol: This data type represents a unique and immutable identifier. Symbols are primarily used as object property keys.
let id = Symbol('id');
let obj = {
[id]: 1234
};
console.log(obj[id]); // Output: 1234
Non-Primitive Data Types
Non-primitive data types are complex data structures that can hold multiple values. They are mutable, which means that their values can be changed. JavaScript has the following non-primitive data types:
- Object: This data type represents a collection of key-value pairs. Objects can have properties and methods.
let person = {
name: "John Doe",
age: 30,
occupation: "Software Engineer"
};
- Array: This data type represents an ordered collection of values. Arrays can hold values of different data types.
let fruits = ["apple", "banana", "orange"];
let mixedArray = [1, "hello", true];
- Function: This data type represents a reusable block of code that can be called with arguments and can return a value.
function greet(name) {
return "Hello, " + name + "!";
}
let greeting = greet("John");
console.log(greeting); // Output: "Hello, John!"
- Date: This data type represents a specific date and time.
let today = new Date();
console.log(today); // Output: "Fri Apr 14 2023 11:30:00 GMT+0000 (Coordinated Universal Time)"
- RegExp: This data type represents a regular expression, which is a pattern used for matching and manipulating text.
let emailRegex = /\b[\w.-]+@[\w.-]+\.\w+\b/;
let email = "[email protected]";
console.log(emailRegex.test(email)); // Output: true
Understanding the different data types in JavaScript is crucial for writing efficient and maintainable code. By knowing the characteristics and behaviors of each data type, you can choose the appropriate data structure for your specific use case and perform the necessary operations on your data.