JS Arrays
In this page:
const arrayName = [item1, item2, item3];
arrayName[index];
arrayName.length;
Creating Arrays
Arrays are created with square brackets [ ], containing a comma-separated list of values, which can be numbers, strings, objects, or even other arrays, all inside the same array.
उदाहरण: Creating Arrays
// Declare the constant `numbers`, set to `[1, 2, 3]`
// Declare the constant `numbers`, set to `[1, 2, 3]`
const numbers = [1, 2, 3];
// Declare the constant `mixed`, set to `["text", 5, true]`
// Declare the constant `mixed`, set to `["text", 5, true]`
const mixed = ["text", 5, true];
// Print `numbers, mixed` to the console
// Print `numbers, mixed` to the console
console.log(numbers, mixed);
Accessing Elements
Array elements are accessed using square brackets with a zero-based index, arrayName[0] retrieves the first element, arrayName[1] the second, and so on. Trying to read an index that doesn't exist returns undefined instead of throwing an error.
उदाहरण: Accessing Elements
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]);
console.log(fruits[5]); // undefined, index doesn't exist
Array Length
Every array has a length property that reports how many elements it currently contains, automatically updating whenever elements are added or removed. length is also writable directly, and shrinking it manually will actually delete elements from the end of the array.
उदाहरण: Array Length
const items = ["a", "b", "c"];
console.log(items.length);
items.length = 1;
console.log(items); // ["a"], shrinking deletes elements
Changing Elements
An individual element can be changed by assigning a new value directly to its index, arrayName[index] = newValue, which replaces just that one slot without affecting the rest of the array.
उदाहरण: Changing Elements
// Declare the constant `colors`, set to `["red", "green", "blue"]`
// Declare the constant `colors`, set to `["red", "green", "blue"]`
const colors = ["red", "green", "blue"];
colors[1] = "yellow";
// Print `colors` to the console
// Print `colors` to the console
console.log(colors);
The Spread Operator With Arrays
The spread operator ... expands an array's elements individually, making it easy to copy an entire array or combine multiple arrays together into one new array.
It creates a new array rather than modifying the original, which is why it's a common tool for avoiding accidental mutation.
उदाहरण: The Spread Operator With Arrays
// Declare the constant `original`, set to `[1, 2, 3]`
// Declare the constant `original`, set to `[1, 2, 3]`
const original = [1, 2, 3];
// Declare the constant `copy`, set to `[...original]`
// Declare the constant `copy`, set to `[...original]`
const copy = [...original];
// Declare the constant `combined`, set to `[...original, 4, 5]`
// Declare the constant `combined`, set to `[...original, 4, 5]`
const combined = [...original, 4, 5];
// Print `copy, combined` to the console
// Print `copy, combined` to the console
console.log(copy, combined);
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: