JavaScript Quiz Questions and Answers 1

Question 1

What is the output of the following code?

console.log(typeof NaN);

a) “number”
b) “string”
c) “undefined”
d) “NaN”

Answer: a) “number”

Explanation: In JavaScript, NaN (Not-a-Number) is actually of the type number. This is a special value that results from operations that don’t yield a valid number.

Question 2

What does the following code snippet do?

let x = 5;

let y = x++;

console.log(x, y);

a) 4, 5
b) 6, 5
c) 5, 6
d) 5, 5

Answer: b) 6, 5

Explanation: The x++ is the post-increment operator. It increases the value of x by 1 but returns the original value before incrementing. Thus, y gets the original value (5), and x is incremented to 6.

Question 3

Which of the following will NOT create a new array?

const arr = [1, 2, 3];

const newArr = => x * 2);

a) Array.from(arr)
b) arr.slice()
c) arr.push(4)
d) […arr]

Answer: c) arr.push(4)

Explanation: arr.push(4) modifies the original array by adding a new element to it and returns the new length of the array. The other methods create a new array.

Question 4

What will be the result of the following code?

const person = { name: “Alice” };


person.age = 25;


a) { name: “Alice”, age: 25 }
b) { name: “Alice” }
c) Error
d) undefined

Answer: b) { name: “Alice” }

Explanation: Object.freeze makes an object immutable, meaning properties cannot be added, removed, or changed. Therefore, person.age = 25 will not have any effect.

Question 5

What will be the output of the following code?

let a = [1, 2, 3];

let b = a;



a) [1, 2, 3]
b) [1, 2, 3, 4]
c) [4]
d) undefined

Answer: b) [1, 2, 3, 4]

Explanation: In JavaScript, arrays are reference types. When b is assigned a, both variables point to the same array in memory. Modifying b also modifies a.

Question 6

Which method can be used to combine the elements of an array into a single string?

const arr = [“Hello”, “World”];

a) arr.split(” “)
b) arr.join(” “)
c) arr.concat(” “)
d)” “)

Answer: b) arr.join(” “)

Explanation: The join method combines all elements of an array into a single string, with a specified separator. In this case, it will join the elements with a space.

Question 7

What is the purpose of the Promise.all method in JavaScript? a) To execute multiple promises in parallel and wait for all of them to resolve
b) To execute multiple promises in sequence
c) To execute a promise after a specified delay
d) To handle errors in promises

Answer: a) To execute multiple promises in parallel and wait for all of them to resolve

Explanation: Promise.all takes an array of promises and returns a single promise that resolves when all of the promises in the array have resolved, or rejects if any promise in the array rejects.

Question 8

What will the following code output?

(function() {

    var a = b = 3;


console.log(typeof a, typeof b);

a) undefined, undefined
b) number, number
c) undefined, number
d) number, undefined

Answer: c) undefined, number

Explanation: The statement var a = b = 3 is equivalent to b = 3; var a = b;. b is not declared with var, let, or const, making it a global variable. a is scoped to the immediately invoked function expression (IIFE). Therefore, a is undefined outside the function, while b is a global number.

Question 9

What is the output of the following code?

console.log(0.1 + 0.2 == 0.3);

a) true
b) false
c) undefined
d) NaN

Answer: b) false

Explanation: Due to floating-point precision issues in JavaScript, 0.1 + 0.2 does not exactly equal 0.3. The result is a number very close to 0.3, but not exactly, thus 0.1 + 0.2 == 0.3 evaluates to false.

Question 10

What will be the result of the following code?

let x = [1, 2, 3];

let y = [1, 2, 3];

console.log(x == y, x === y);

a) true true
b) false false
c) true false
d) false true

Answer: b) false false

Explanation: In JavaScript, arrays are compared by reference, not by value. Even though x and y contain the same elements, they are different objects in memory, so both == and === comparisons return false.