Register

JavaScript for Beginners

0 of 24 notes read 0%

Reading progress is saved to your INFIRALS account. Sign in to keep this note marked as read.

Practice questions: control flow and functions

Control flow and functions18 min readLesson 4 of 4

Answer each question before reading the solution. Every snippet can be pasted into the browser console or a Node file, so you can compare the printed value with your prediction.

4.1 Q1 What is the difference between if/else and a switch statement?

Answer (English): if/else evaluates conditions in order, so each branch can test a different expression. switch compares one value against fixed cases using strict equality and jumps to the matching case. Use if/else for ranges and combined logic, and switch for a known set of option values.

Hinglish: if/else conditions ko order mein check karta hai, isliye har branch alag expression test kar sakti hai. switch ek value ko fixed cases se strict equality se compare karta hai. Ranges aur combined logic ke liye if/else, aur fixed options ke liye switch use karo.

4.2 Q2 Why does a switch statement need break?

Answer (English): Without break or return, execution falls through to the next case, so the code that belonged to one option also runs for the previous option. Fallthrough is occasionally useful when several cases share the same body, but it must be intentional.

Hinglish: break ya return na likho to switch agle case mein gir jata hai, isliye ek option ka code pichle option ke liye bhi chal jata hai. Kabhi-kabhi ek hi body ke liye fallthrough jaan-boojh kar use hota hai, par woh clear hona chahiye.

4.3 Q3 When would you choose a while loop over a for loop?

Answer (English): Use while when the number of passes is not known in advance and the loop ends when a condition changes, such as reading until a value is found. Use for when the start, condition and update are known and fit on one line. Both run the same body; the choice is about which shape states the intent more clearly.

Hinglish: while tab use karo jab passes ki ginti pehle se pata na ho aur loop kisi condition ke badalne par ruke, jaise value milne tak padhna. for tab jab start, condition aur update ek line mein saaf likhe ja sakein. Dono ka body same hota hai, choice sirf intent clear karne ke liye hai.

4.4 Q4 What is the difference between for of and for in?

Answer (English): for of visits the values of an iterable such as an array, string or Map. for in visits enumerable property names of an object, including inherited ones, and on an array it yields index strings. Use for of for values and Object.keys or Object.entries for object properties.

Hinglish: for of array, string ya Map jaise iterable ki values par chalta hai. for in object ke property names deta hai, including inherited properties, aur array par index strings deta hai. Values ke liye for of, aur object properties ke liye Object.keys ya Object.entries use karo.

4.5 Q5 How do break and continue differ inside a loop?

Answer (English): break leaves the loop immediately and continues with the statement after it. continue skips the rest of the current pass and starts the next pass. break is for stopping a search, and continue is for skipping an item without wrapping the whole body in an if block.

Hinglish: break loop se turant bahar nikalta hai aur loop ke baad wala statement chalata hai. continue current pass ka baaki code skip karke agla pass shuru karta hai. break search rokne ke liye, aur continue item skip karne ke liye use hota hai.

4.6 Q6 What happens when a function is called with fewer arguments than parameters?

Answer (English): The missing parameters receive undefined. A default parameter replaces undefined with its default value, but a parameter without a default stays undefined, and arithmetic on it produces NaN. JavaScript does not throw for a missing argument, so the function should guard or default the value.

Hinglish: Missing parameters ko undefined milta hai. Default parameter undefined ki jagah default value deta hai, par default ke bina parameter undefined hi rehta hai aur us par arithmetic NaN deta hai. JavaScript missing argument par error nahi deta, isliye function ko guard ya default rakhna chahiye.

4.7 Q7 What is an arrow function, and how does it differ from a regular function?

Answer (English): An arrow function is a compact function expression with implicit return for a single expression. It does not create its own this, arguments or prototype, so it inherits this from the surrounding scope. That makes it a good callback and a poor choice for an object method that needs this to refer to the object.

Hinglish: Arrow function compact function expression hai; single expression par return implicit hota hai. Iska apna this, arguments ya prototype nahi hota, woh bahar ka this leta hai. Isliye callback ke liye achha hai, par object method jise this se object chahiye uske liye galat hai.

4.8 Q8 What does it mean that functions are values?

Answer (English): It means a function can be stored in a variable, passed as an argument, returned from another function and placed in an object or array. A function that takes or returns a function is called higher-order. Callbacks, array methods and event listeners all rely on this property.

Hinglish: Functions ko variable mein rakh sakte ho, argument ki tarah pass kar sakte ho, doosre function se return kar sakte ho, aur object ya array mein rakh sakte ho. Jo function doosra function leta ya return karta hai usse higher-order function kehte hain. Callbacks, array methods aur event listeners isi par chalte hain.

4.9 Q9 Why does a function that changes its array parameter surprise the caller?

Answer (English): Arrays are objects, so a function receives a reference to the caller's array. Pushing or sorting inside the function changes the same array the caller holds. If the function should not mutate its input, copy it with spread or map first and return a new array.

Hinglish: Arrays objects hote hain, isliye function ko caller ke array ka reference milta hai. Function ke andar push ya sort karne se caller ka wahi array badal jata hai. Agar input ko badalna nahi hai to spread ya map se copy bana kar naya array return karo.

4.10 Q10 How do you decide when a function should return early?

Answer (English): Return early when the answer or the invalid case is known before the main work. A guard clause at the top removes bad input, and a return at the first match removes the rest of the branch. Early returns keep the main path unindented and make the function easier to scan and test.

Hinglish: Jab answer ya invalid case main kaam se pehle pata ho to early return karo. Shuru mein guard clause galat input hata deta hai, aur pehla match milte hi return baaki branch hata deta hai. Early return se main path unindented rehta hai aur function padhna aasan hota hai.

javascript
function sumEven(numbers) {
  return numbers
    .filter((number) => number % 2 === 0)
    .reduce((sum, number) => sum + number, 0);
}

console.log(sumEven([4, 7, 10, 13, 16]));

Output

30

The quiz for this module is separate. Take it without scrolling back, then revisit any question that felt uncertain. If an output still looks wrong, run the snippet and compare the printed value line by line.