Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

JavaScript Arrays and Objects

Lesson 21 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

JavaScript Arrays and Objects

JavaScript Arrays

// Create
const fruits = ["apple", "banana", "cherry"];
const mixed = [1, "two", true, null, { id: 1 }];

// Access
console.log(fruits[0]);    // apple
console.log(fruits.length); // 3

// Modify
fruits.push("date");        // add at end
fruits.unshift("avocado");  // add at start
const last = fruits.pop();  // remove from end
const first = fruits.shift(); // remove from start
fruits.splice(1, 1, "mango"); // remove/insert at index

Array Methods

const nums = [1, 2, 3, 4, 5];

// map — transform elements
const doubled = nums.map(n => n * 2);   // [2,4,6,8,10]

// filter — select elements
const evens = nums.filter(n => n % 2 === 0);  // [2,4]

// reduce — accumulate
const sum = nums.reduce((acc, n) => acc + n, 0);  // 15

// find / findIndex
const found = nums.find(n => n > 3);       // 4
const idx = nums.findIndex(n => n > 3);   // 3

// some / every
nums.some(n => n > 4);   // true
nums.every(n => n > 0);  // true

// includes
nums.includes(3);  // true

// sort
["banana","apple","cherry"].sort(); // alphabetical
nums.sort((a, b) => a - b);         // ascending numeric

// flat / flatMap
[[1,2],[3,4]].flat();               // [1,2,3,4]

// forEach (no return value)
nums.forEach(n => console.log(n));

// Spread operator
const copy = [...nums];
const combined = [...nums, 6, 7];

JavaScript Objects

// Object literal
const person = {
    name: "Alice",
    age: 25,
    address: {
        city: "Delhi",
        pin: "110001"
    },
    greet() {
        return `Hi, I'm ${this.name}`;
    }
};

// Access
console.log(person.name);           // dot notation
console.log(person["age"]);         // bracket notation
console.log(person.address.city);   // nested access
console.log(person.greet());        // method call

// Modify
person.age = 26;
person.email = "alice@email.com";   // add property
delete person.email;                // delete property

Object Methods

Object.keys(person);    // ["name","age","address"]
Object.values(person);  // ["Alice", 25, {...}]
Object.entries(person); // [["name","Alice"], ...]

// Spread and merge
const updated = { ...person, age: 27 };

// Destructuring
const { name, age } = person;
const { address: { city } } = person;

Array vs Object

FeatureArrayObject
KeysNumeric indicesString keys
OrderedYesNo (mostly)
Use caseListsRecords/entities
Iterationfor...offor...in
Key Takeaway: Arrays (ordered lists) and Objects (key-value maps) are the core data structures in JavaScript. Master array methods like map, filter, reduce and object destructuring for clean, functional code.