Category: javascript
Array in JavaScript
Published on 16 Feb 2026
Explanation
Array in JavaScript
An array is an ordered collection of values.
Each value can be of any type (number, string, object, etc.).
Array elements are indexed starting from 0.
Arrays are mutable, meaning you can change their elements.
Code:
Explanation
Creating an Array
Code:
// Using array literal let fruits = ["Apple", "Banana", "Mango"]; console.log(fruits); // ["Apple", "Banana", "Mango"] // Using Array constructor let numbers = new Array(1, 2, 3, 4); console.log(numbers); // [1, 2, 3, 4] //Accessing Elements console.log(fruits[0]); // Apple console.log(fruits[2]); // Mango
Explanation
Common Array Methods
Code:
let colors = ["Red", "Green", "Blue"];
// Add element
colors.push("Yellow"); // Add at end
colors.unshift("Purple"); // Add at start
// Remove element
colors.pop(); // Remove last
colors.shift(); // Remove first
// Iterate array
colors.forEach(color => console.log(color));
cont..
Explanation
cont..
Common Array Methods
Code:
// Transform array
let upperColors = colors.map(color => color.toUpperCase());
console.log(upperColors);
// Filter array
let filtered = colors.filter(color => color.startsWith("G"));
console.log(filtered); // ["Green"]
Explanation
Object in JavaScript
1. An object is a collection of key-value pairs.
2. Keys are strings , values can be any type.
3. Objects are unordered.
4. Objects are useful to represent real-world entities.
Code:
let person = {
name: "John",
age: 25,
isStudent: true
};
console.log(person);
// {name: "John", age: 25, isStudent: true}
cont..
Explanation
cont..
Code:
//Accessing Object Properties
console.log(person.name); // John
console.log(person["age"]); // 25
//Modifying Object Properties
person.age = 26;
person.country = "USA"; // Add new property
console.log(person);
// {name: "John", age: 26, isStudent: true, country: "USA"}
cont..
Explanation
cont..
Code:
//Iterating Over Object Using for...in loop
for (let key in person) {
console.log(key + " => " + person[key]);
}
// Using Object.keys() and forEach
Object.keys(person).forEach(key => {
console.log(key + " : " + person[key]);
});
Explanation
Array of Objects Example
Code:
let students = [
{ name: "Alice", age: 20 },
{ name: "Bob", age: 22 },
{ name: "Charlie", age: 21 }
];
// Access first student
console.log(students[0].name); // Alice
cont..
Explanation
cont..
Code:
// Filter students older than 20
let older = students.filter(student => student.age > 20);
console.log(older);
// [{name: "Bob", age: 22}, {name: "Charlie", age: 21}]
//Object with Array Property
let classroom = {
teacher: "Mr. Smith",
students: ["Alice", "Bob", "Charlie"]
};
console.log(classroom.students[1]); // Bob
Explanation
Key Points:
1. Arrays are best for ordered lists.
2. Objects are best for structured data with named properties.
3. You can combine both (array of objects or object with array).
Code:
Explanation
How do you access the age of this object?
let person = { name: "John", age: 25, isStudent: true };
Code:
A) person[0]
B) person.age
C) person("age")
D) person->age