Practice questions: objects, arrays and data
Objects, arrays and data18 min readLesson 4 of 4
Try each question before reading the answer. The snippets are self-contained and print their results, so you can run them in Node or the browser console and compare your prediction.
4.1 Q1 What is the difference between dot access and bracket access on an object?
Answer (English): Dot access reads a property with a fixed identifier, such as user.name. Bracket access evaluates a string expression, so it works when the key is stored in a variable or contains spaces. Both read the same property; dot access is shorter when the key is known at author time.
Hinglish: Dot access fixed naam se property padhta hai, jaise user.name. Bracket access string expression leta hai, isliye tab kaam aata hai jab key variable mein ho ya space ho. Dono wahi property padhte hain; key pehle se pata ho to dot access chhota hai.
4.2 Q2 How does this behave inside an object method?
Answer (English): When a method is called through its object, this refers to that object, so this.name reads the property. If the method is detached and called as a plain function, this is lost and the property read fails in strict mode. Pass a wrapper that calls the method through the object when a callback needs it.
Hinglish: Method ko object ke through call karo to this usi object ko point karta hai, aur this.name property padh leta hai. Method ko object se alag karke simple function ki tarah call karo to this chala jata hai aur strict mode mein property read error deta hai. Callback mein wrapper se object ke through call karo.
4.3 Q3 What does map return, and how does it differ from forEach?
Answer (English): map returns a new array with one result for every input, using the callback's return value. forEach runs the callback for its side effect and returns undefined. Use map to build a transformed list and forEach only for work such as logging or rendering.
Hinglish: map har input ke liye ek result wala naya array deta hai, jo callback ka return value hota hai. forEach callback ko side effect ke liye chalata hai aur undefined deta hai. Transformed list banane ke liye map, aur log ya render jaise kaam ke liye forEach use karo.
4.4 Q4 When should you use filter, find, some and every?
Answer (English): filter returns all items whose predicate is truthy, find returns the first match or undefined, some returns true when any item matches, and every returns true only when all items match. Each method answers a different question, so pick the one whose return shape matches the need.
Hinglish: filter saare matching items ka array deta hai, find pehla match ya undefined deta hai, some koi match mile to true deta hai, aur every tabhi true deta hai jab sab match karein. Har method alag sawal ka jawab deta hai, isliye return shape dekh kar chuno.
4.5 Q5 Why does reduce need an initial value?
Answer (English): The initial value fixes the accumulator's type and makes reduce safe on an empty array. Without it, reduce uses the first item and starts from the second, which produces wrong results for typed accumulators and throws when the array has no items.
Hinglish: Initial value accumulator ka type fix karti hai aur empty array par reduce ko safe banati hai. Iske bina reduce pehle item ko accumulator maan leta hai, typed accumulator ke liye galat result deta hai, aur empty array par error deta hai.
4.6 Q6 Why does [10, 2, 1].sort() return a surprising order, and how do you fix it?
Answer (English): sort converts items to strings before comparing by default, so the strings "10", "2" and "1" sort in dictionary order. Pass a comparator such as (a, b) => a - b for numeric ascending order. Use toSorted when the original array must stay unchanged.
Hinglish: sort default mein items ko string bana kar compare karta hai, isliye "10", "2", "1" dictionary order mein lagte hain. Numeric order ke liye comparator do, jaise (a, b) => a - b. Original array ko bachana ho to toSorted use karo.
4.7 Q7 What does the spread operator do, and is its copy deep?
Answer (English): Spread expands an array's items or an object's properties into a new literal, which makes a shallow copy. Nested objects and arrays inside the copy still point at the same data. Use structuredClone or a JSON round trip when nested data must be independent.
Hinglish: Spread array ke items ya object ke properties ko naye literal mein failata hai, jisse shallow copy banti hai. Andar ke nested objects aur arrays abhi bhi same data ko point karte hain. Nested data independent chahiye to structuredClone ya JSON round trip use karo.
4.8 Q8 What is the difference between spread and rest?
Answer (English): They use the same ... token but do opposite things. Spread expands a collection where values are being built or passed. Rest collects remaining values into a new array or object and appears in a binding or parameter list, always last.
Hinglish: Dono ... use karte hain par kaam ulta hai. Spread collection ko expand karta hai jahan value ban rahi ya pass ho rahi ho. Rest bachi hui values ko naye array ya object mein collect karta hai, binding ya parameter list mein aata hai, aur hamesha last hota hai.
4.9 Q9 What kinds of values does JSON support, and what is lost in a round trip?
Answer (English): JSON supports objects, arrays, strings, numbers, booleans and null. It does not support undefined, functions, dates or cycles. A stringify and parse round trip drops undefined and function properties and turns a Date into a string.
Hinglish: JSON objects, arrays, strings, numbers, booleans aur null support karta hai. undefined, functions, dates aur cycles support nahi karta. Stringify aur parse ke round trip mein undefined aur functions nikal jate hain, aur Date string ban jati hai.
4.10 Q10 Why does changing a copied array of objects sometimes change the original?
Answer (English): Spread copies the array's slots but not the objects inside them. Both arrays point at the same objects, so changing a property through the copy is visible through the original. Copy the objects as well, for example with users.map((user) => ({ ...user })), when the data must be independent.
Hinglish: Spread array ke slots copy karta hai par andar ke objects nahi. Dono arrays same objects ko point karte hain, isliye copy se property badlo to original mein bhi dikhta hai. Independent data chahiye to objects bhi copy karo, jaise users.map((user) => ({ ...user })).
const users = [
{ name: "Aarav", active: true },
{ name: "Meera", active: false },
];
const activeNames = users.filter((user) => user.active).map((user) => user.name);
const { name } = users[0];
console.log(JSON.stringify(activeNames));
console.log(name);Output
["Aarav"] Aarav
The five-question quiz for this module is separate. Take it without scrolling back, then revisit the questions you missed. For any answer that surprised you, run the snippet and print the intermediate value after each step.
