1. JavaScript Objects
Objects in JavaScript are used to store collections of data using key-value pairs. Keys are strings (or Symbols), and values can be of any type including strings, numbers, arrays, functions, or even other objects.
1.1 Creating Objects
// Using object literal
const person = {
name: "Alice",
age: 30,
isStudent: false
};
// Using constructor
const car = new Object();
car.brand = "Toyota";
car.model = "Camry";
car.year = 2022;
1.2 Accessing Object Properties
console.log(person.name); // Dot notation → "Alice"
console.log(person["age"]); // Bracket notation → 30
1.3 Modifying Properties
person.age = 31; // Update value
person.city = "New York"; // Add new property
delete person.isStudent; // Remove property
1.4 Nested Objects
const employee = {
name: "Bob",
address: {
street: "123 Main St",
city: "Los Angeles"
}
};
console.log(employee.address.city); // Output: Los Angeles
1.5 Object Methods
Functions inside objects are called methods.
const user = {
name: "John",
greet: function() {
return "Hello, " + this.name;
}
};
console.log(user.greet()); // Output: Hello, John
1.6 Common Object Methods
Object.keys(obj)
– Returns an array of keysObject.values(obj)
– Returns an array of valuesObject.entries(obj)
– Returns an array of key-value pairsObject.hasOwnProperty("key")
– Checks if a property exists
const user = {name: "Sam", age: 25};
console.log(Object.keys(user)); // ["name", "age"]
console.log(Object.values(user)); // ["Sam", 25]
console.log(user.hasOwnProperty("name")); // true
2. JavaScript Arrays
Arrays are ordered collections used to store multiple values in a single variable. Each item in an array has an index, starting at 0.
2.1 Creating Arrays
// Using array literal
const fruits = ["apple", "banana", "cherry"];
// Using constructor
const scores = new Array(90, 80, 85);
2.2 Accessing & Modifying Elements
console.log(fruits[0]); // Output: "apple"
fruits[1] = "mango"; // Modify element
fruits.push("orange"); // Add element to end
fruits.pop(); // Remove last element
2.3 Common Array Methods
push()
– Add to endpop()
– Remove from endshift()
– Remove from beginningunshift()
– Add to beginningslice(start, end)
– Extracts a sectionsplice(start, deleteCount, item1, item2...)
– Adds/removes itemsincludes()
– Checks if value existsindexOf()
– Finds index of value
const nums = [1, 2, 3, 4];
nums.push(5); // [1, 2, 3, 4, 5]
nums.pop(); // [1, 2, 3, 4]
nums.splice(1, 2); // Removes 2 items from index 1 → [1, 4]
2.4 Looping Through Arrays
const colors = ["red", "green", "blue"];
// for loop
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
// for...of loop
for (let color of colors) {
console.log(color);
}
// forEach
colors.forEach(function(color) {
console.log(color);
});
2.5 Higher-Order Array Methods
map()
– Creates a new array by transforming elementsfilter()
– Returns a new array with elements that pass a testreduce()
– Reduces array to a single value
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(num => num * 2); // [2, 4, 6, 8, 10]
const evens = numbers.filter(num => num % 2 === 0); // [2, 4]
const sum = numbers.reduce((acc, curr) => acc + curr, 0); // 15
3. Arrays of Objects
Often used in real-world applications like databases, APIs, etc.
const users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 22 }
];
// Accessing
console.log(users[1].name); // Output: Bob
// Filtering
const adults = users.filter(user => user.age >= 25);
JavaScript Function
1. What will be the output of the following code?
const arr1 = [1, 2];
const arr2 = [3, 4];
const combined = [...arr1, ...arr2];
console.log(combined);
2. What will be the output of the following code?
const numbers = [1, 2, 3, 4];
const lastNumber = numbers.pop();
console.log(lastNumber);
3. What will be the result of destructuring the following object?
const person = { name: 'Alice', age: 25 };
const { name, age, gender } = person;
console.log(gender);
4. What will be the output of the following code?
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled);
5. Can the spread operator (...) be used to create a deep copy of an object?
6. What will be the output of the following code?
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum);
7. Which of the following is a valid way to create an object in JavaScript?
8. Which loop is best used to iterate over the properties of an object?
9. What will be the output of the following code?
const fruits = ['apple', 'banana', 'cherry'];
console.log(fruits.length);
10. What will be the output of the following code?
const numbers = [1, 2, 3, 4];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers);
11. What will be the output of the following code?
const fruits = ['apple', 'banana', 'orange'];
const lastFruit = fruits.pop();
console.log(lastFruit);
12. What will be the output of the following code?
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1, c: 3 };
console.log(obj2);
13. What will be the output of the following code?
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled);
14. What will be the output of the following code?
const person = { name: 'Alice', age: 25 };
const keys = Object.keys(person);
console.log(keys);
15. What will be the output of the following code?
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers);
16. What will be the output of the following code?
const arr = [1, 2, 3];
const [x, y] = arr;
console.log(x);
17. What will be the output of the following code?
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum);
18. What will be the output of the following code?
const numbers = [1, 2, 3];
const firstNumber = numbers.shift();
console.log(firstNumber);
19. What will be the output of the following code?
const arr = [1, 2, 3];
arr.splice(1, 1, 4);
console.log(arr);
20. What will be the output of the following code?
const user = { name: 'John', age: 30 };
const { name } = user;
console.log(name);