JavaScript Data Types
In JavaScript, data types serve as the building blocks for variable assignments and manipulations within a program. These data types can be broadly categorized into two groups: primitive data types and object data types.
Primitive Data Types:
-
String:
- The string data type is employed to represent textual information within the program.
let name = "John";
-
Number:
- The number data type encompasses both integers and floating-point numbers.
let age = 25;
let height = 1.75; -
Boolean:
- Booleans represent logical values, taking on either
true
orfalse
.
let isStudent = true;
- Booleans represent logical values, taking on either
-
Undefined:
- The undefined data type represents variables that have been declared but not assigned any value.
let city;
-
Null:
- Null signifies the intentional absence of any object value.
let result = null;
-
Symbol:
- Symbols, introduced in ECMAScript 6, are unique and immutable primitive values.
let id = Symbol("uniqueId");
Object Data Types:
-
Object:
- Objects represent a collection of key-value pairs, allowing the grouping of related information.
let person = { name: "Alice", age: 30 };
-
Array:
- Arrays are ordered collections of values, enabling the storage and manipulation of multiple items.
let numbers = [1, 2, 3, 4, 5];
-
Function:
- Functions serve as reusable blocks of code, encapsulating specific logic for execution.
function greet(name) {
console.log("Hello, " + name + "!");
} -
Date:
- The Date object is used for representing date and time values within the program.
let today = new Date();
-
RegExp:
- Regular expressions, represented by the RegExp object, enable pattern matching within strings.
let pattern = /[a-z]+/;