Arrays and Objects are the most commonly used data structures in JavaScript.
Almost every JavaScript application uses them extensively.
Examples: User data, Product lists, API responses, Dashboard data, Shopping carts
๐ฆ PART 1: JavaScript Arrays
๐ง 1. What is an Array?
An array is used to store multiple values in a single variable.
Example:
const fruits = ["Apple", "Mango", "Banana"];
Access Elements:
console.log(fruits[0]);Output: Apple
Important: Array indexing starts from 0.
โก 2. Creating Arrays
Method 1:
const numbers = [10, 20, 30];Method 2:
const numbers = new Array(10, 20, 30);๐ฅ 3. Array Methods
push() Adds element at the end.
const arr = [1, 2];
arr.push(3);
console.log(arr);
Output: [1, 2, 3]
pop() Removes last element.
arr.pop();unshift() Adds element at beginning.
arr.unshift(0);shift() Removes first element.
arr.shift();๐ 4. Loop Through Arrays
for Loop
const nums = [1,2,3];
for(let i=0; i<nums.length; i++){
console.log(nums[i]);
}
for...of
for(let num of nums){
console.log(num);
}๐ฏ 5. map()
Creates a new array by transforming elements.
Example:
const nums = [1,2,3];
const doubled = nums.map(num => num * 2);
console.log(doubled);
Output: [2, 4, 6]
๐ฅ 6. filter()
Returns elements matching condition.
Example:
const nums = [1,2,3,4,5];
const even = nums.filter(num => num % 2 === 0);
console.log(even);
Output: [2, 4]
๐ 7. reduce()
Reduces array to single value.
Example:
const nums = [1,2,3,4];
const sum = nums.reduce((total,num) => total + num, 0);
console.log(sum);
Output: 10
๐งฉ 8. find()
Returns first matching element.
const users = [10,20,30,40];
const result = users.find(num => num > 20);
console.log(result);
Output: 30
๐ 9. includes()
Checks if value exists.
const fruits = ["Apple","Mango"];
console.log(fruits.includes("Apple"));
Output: true
๐ฅ 10. Remove Duplicates
const nums = [1,2,2,3,3];
const unique = [...new Set(nums)];
console.log(unique);
Output: [1, 2, 3]
๐ฆ PART 2: JavaScript Objects
๐ง 11. What is an Object?
An object stores data in key-value pairs.
Example:
const person = {
name: "Deepak",
age: 25,
city: "Bengalore"
};โก 12. Access Object Properties
Dot Notation:
console.log(person.name);Bracket Notation:
console.log(person["name"]);๐ฅ 13. Add New Property
person.country = "India";
console.log(person);
โ 14. Delete Property
delete person.city;๐ 15. Loop Through Objects
for...in
for(let key in person){
console.log(key, person[key]);
}