JS 8
JS 8
You can use a for...in loop to iterate through the object and print only the values.
javascript
Copy code
const obj = {
name: 'John',
age: 30,
city: 'New York'
};
NaN stands for "Not a Number" in JavaScript. It is a special value returned when a
mathematical operation produces an undefined or unrepresentable value.
Question 3: Explain pass by value and pass by reference? Give code example of how you
would pass by reference in JavaScript?
• Pass by Value: Primitive data types like numbers and strings are passed by value.
Changes made to the parameter inside the function do not affect the original value.
• Pass by Reference: Objects and arrays are passed by reference. Changes made to the
parameter inside the function affect the original object or array.
javascript
Copy code
const obj = { name: 'John' };
function updateObject(o) {
o.name = 'Jane'; // Changes the original object
}
updateObject(obj);
console.log(obj); // Output: { name: 'Jane' }
In JavaScript, this refers to the context in which a function is executed. It can have different
values depending on how the function is called:
• In a global context, this refers to the global object (window in a browser, global in
Node.js).
• In an object method, this refers to the object that owns the method.
• In a constructor function, this refers to the instance of the object being created.
Question 5: What is memoization in JavaScript? Can you give a code example
implementing the same?
Memoization is a technique used to cache the results of expensive function calls and return
the cached result when the same inputs occur again.
Example of memoization:
javascript
Copy code
function memoize(fn) {
const cache = {};
return function(...args) {
const key = JSON.stringify(args);
if (!cache[key]) {
cache[key] = fn.apply(this, args);
}
return cache[key];
};
}
function expensiveOperation(n) {
console.log('Calculating...');
return n * 2;
}
Question 7: What are escape characters? Why are they used? Give code example.
Escape characters are special characters preceded by a backslash () in strings. They are used
to represent characters that cannot be directly typed or have special meaning in JavaScript.
Example:
javascript
Copy code
console.log('This is a \'quoted\' text.'); // Output: This is a 'quoted'
text.
console.log("This is a \"quoted\" text."); // Output: This is a "quoted"
text.
console.log('This is a \nnew line.'); // Output: This is a
// new line.
Question 8: What are break and continue statements? How are they different?
Example:
javascript
Copy code
for (let i = 0; i < 5; i++) {
if (i === 3) {
break; // Exits the loop when i is 3
}
console.log(i); // Output: 0, 1, 2
}
The typeof operator returns 'object' for arrays because arrays are a type of object in
JavaScript.
javascript
Copy code
const arr = [1, 2, 3];
console.log(typeof arr); // Output: object
Anonymous functions are functions without a name. They are often used as callback
functions or immediately invoked function expressions (IIFE).
Example:
javascript
Copy code
const add = function(a, b) {
return a + b;
};
Question 12: What are higher order functions? Explain with a code example.
Higher order functions are functions that operate on other functions by taking them as
arguments or returning them.
Example:
javascript
Copy code
function multiplyBy(num) {
return function(x) {
return x * num;
};
}
In this example, multiplyBy is a higher order function that returns a function that multiplies
its argument by a specified number.