0% found this document useful (0 votes)
12 views4 pages

JS 8

The document contains a series of JavaScript-related questions and answers covering topics such as object iteration, NaN, pass by value vs. pass by reference, the 'this' keyword, memoization, data types, escape characters, loop control statements, typeof operator, anonymous functions, event methods, and higher order functions. Each question is accompanied by explanations and code examples to illustrate the concepts. Overall, it serves as a comprehensive guide to fundamental JavaScript concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

JS 8

The document contains a series of JavaScript-related questions and answers covering topics such as object iteration, NaN, pass by value vs. pass by reference, the 'this' keyword, memoization, data types, escape characters, loop control statements, typeof operator, anonymous functions, event methods, and higher order functions. Each question is accompanied by explanations and code examples to illustrate the concepts. Overall, it serves as a comprehensive guide to fundamental JavaScript concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Question 1: How to iterate inside an object and print all the values inside it without the

keys? Give a code example.

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'
};

for (let key in obj) {


console.log(obj[key]);
}

Question 2: What is NaN property in JavaScript?

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.

Example of pass by reference:

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' }

Question 4: Explain “this” keyword?

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;
}

const memoizedOperation = memoize(expensiveOperation);

console.log(memoizedOperation(5)); // Output: Calculating... 10


console.log(memoizedOperation(5)); // Output: 10 (cached result, no
calculation)

Question 6: What is the data type of variables in JavaScript?

JavaScript variables can have several data types:

• Primitive types: Number, String, Boolean, Null, Undefined, Symbol


• Object type: Objects, Arrays, Functions

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?

• break: Used to exit a loop or switch statement.


• continue: Skips the current iteration of a loop and continues with the next iteration.

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
}

for (let i = 0; i < 5; i++) {


if (i === 3) {
continue; // Skips the iteration when i is 3
}
console.log(i); // Output: 0, 1, 2, 4
}

Question 9: What is the typeof an array in JavaScript?

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

Question 10: What are anonymous functions in JavaScript? Give an example?

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;
};

console.log(add(5, 3)); // Output: 8

Question 11: What's the difference between event.preventDefault() and


event.stopPropagation() methods in JavaScript?

• event.preventDefault(): Prevents the default behavior of an event, such as


submitting a form or following a link.
• event.stopPropagation(): Stops the propagation of an event through the DOM
tree, preventing parent elements from receiving the event.

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;
};
}

const multiplyByTwo = multiplyBy(2);


console.log(multiplyByTwo(5)); // Output: 10

In this example, multiplyBy is a higher order function that returns a function that multiplies
its argument by a specified number.

You might also like