In a Javascript L1 & L2 round the following questions can be asked from interviewer.

JS DSA Interview Question

JavaScript Tricky Output Questions - Interview Notes

1. What is the difference between 'Pass by Value' and 'Pass by Reference'?

Pass by Value: A copy of the actual value is passed. Changes to the parameter inside the function don't affect the original variable.

Pass by Reference: A reference to the memory location is passed. Changes inside the function affect the original variable.

In JavaScript:

javascript

// Pass by Value (Primitives)
let num = 10;
function changeValue(x) {
    x = 20;
}
changeValue(num);
console.log(num); // 10 (unchanged)

// Pass by Reference (Objects)
let obj = { name: "John" };
function changeName(object) {
    object.name = "Jane";
}
changeName(obj);
console.log(obj.name); // "Jane" (changed)

2. What is the difference between map and filter?

map(): Transforms each element and returns a new array of the same length.

filter(): Returns a new array containing only elements that pass a test condition.

javascript

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

// map - transforms each element
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8, 10]

// filter - selects elements based on condition
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4]

3. What is the difference between map() and forEach()?