JavaScript, as a dynamically-typed language, supports a variety of data types that form the building blocks of web development. In this article, we will embark on a unique journey to explore JavaScript's data types, diving deep into their characteristics and nuances.
1. Primitive Data Types
i. Undefined:
- Represents the absence of a value or an uninitialized variable.
let undefinedVariable;
ii. Null:
- Represents the intentional absence of any object value.
let nullValue = null;
iii. Boolean:
- Represents a logical entity and can have only two values:
true
orfalse
.
let isJavaScriptFun = true;
iv. Number:
- Represents numeric values, including integers and floating-point numbers.
let age = 25;
let pi = 3.14;
v. String:
- Represents textual data, enclosed in single or double quotes.
let greeting = 'Hello, World!';
let name = "Alice";
vi. Symbol:
- Introduced in ECMAScript 6, symbols are unique and immutable primitives, often used as object property identifiers.
const mySymbol = Symbol('unique');
2. Composite Data Types
i. Object:
- Represents a collection of key-value pairs, where keys are strings or symbols.
let person = {
name: 'John Doe',
age: 30,
isStudent: false
};
ii. Array:
- Represents an ordered list of values.
let fruits = ['apple', 'orange', 'banana'];
iii. Function:
- Functions are objects that can be invoked.
function add(a, b) {
return a + b;
}
iv. Date:
- Represents dates and times.
let today = new Date();
v. RegExp:
- Represents regular expressions for pattern matching.
let regex = /[a-z]+/;