JavaScript

What is the output of the following code: console.log([…”Hello”]);?

What is the output of the following code: console.log([…”Hello”]);? a) ["H", "e", "l", "l", "o"] b) "Hello" c) [["H", "e", "l", "l", "o"]] d) undefined Answer: a) ["H", "e", "l", "l", "o"] Explanation: The spread operator (…) spreads the elements of an iterable (like a string) into individual elements. When applied to the string "Hello",

What is the output of the following code: console.log([…”Hello”]);? Read More »

How do you create a generator function in JavaScript?

How do you create a generator function in JavaScript? a) By using the function* syntax b) By using function generator() c) By using the new Generator() keyword d) By using the Promise() constructor Answer: a) By using the function* syntax Explanation: A generator function in JavaScript is defined using the function* syntax. Generator functions return

How do you create a generator function in JavaScript? Read More »

How can you iterate over the properties of an object in JavaScript?

How can you iterate over the properties of an object in JavaScript? a) for…in loop b) Object.keys() c) Object.entries() d) All of the above Answer: d) All of the above Explanation: In JavaScript, you can iterate over the properties of an object using the for…in loop, Object.keys() to get an array of keys, or Object.entries()

How can you iterate over the properties of an object in JavaScript? Read More »

What is the difference between Object.freeze() and Object.seal() in JavaScript?

What is the difference between Object.freeze() and Object.seal() in JavaScript? a) Object.freeze() makes an object immutable, while Object.seal() prevents new properties from being added b) Object.freeze() allows new properties, while Object.seal() does not c) Object.seal() removes properties, while Object.freeze() adds properties d) They are identical Answer: a) Object.freeze() makes an object immutable, while Object.seal() prevents

What is the difference between Object.freeze() and Object.seal() in JavaScript? Read More »

What is the output of the following code: console.log([1, 2, 3] instanceof Array);?

What is the output of the following code: console.log([1, 2, 3] instanceof Array);? a) true b) false c) undefined d) null Answer: a) true Explanation: The instanceof operator checks if an object is an instance of a specific class. In this case, [1, 2, 3] is an array, so the result of [1, 2, 3]

What is the output of the following code: console.log([1, 2, 3] instanceof Array);? Read More »

How do you create a deep copy of an object in JavaScript?

How do you create a deep copy of an object in JavaScript? a) JSON.parse(JSON.stringify(object)) b) Object.assign() c) object.slice() d) Array.map() Answer: a) JSON.parse(JSON.stringify(object)) Explanation: To create a deep copy of an object in JavaScript, you can use JSON.parse(JSON.stringify(object)). This method works by first converting the object into a JSON string and then parsing it back

How do you create a deep copy of an object in JavaScript? Read More »

What is the output of the following code: console.log(typeof function(){});?

What is the output of the following code: console.log(typeof function(){});? a) "object" b) "function" c) "undefined" d) "object function" Answer: b) "function" Explanation: In JavaScript, the typeof operator returns "function" when applied to a function. Functions in JavaScript are a special type of object, but typeof identifies them as "function". Reference: JavaScript MCQ (Multiple-Choice Questions)

What is the output of the following code: console.log(typeof function(){});? Read More »

What is the difference between call() and apply() methods in JavaScript?

What is the difference between call() and apply() methods in JavaScript? a) call() takes individual arguments, while apply() takes an array of arguments b) call() is used for synchronous code, while apply() is used for asynchronous code c) call() executes a function immediately, while apply() creates a promise d) Both are identical Answer: a) call()

What is the difference between call() and apply() methods in JavaScript? Read More »

What is the purpose of the Object.assign() method in JavaScript?

What is the purpose of the Object.assign() method in JavaScript? a) To merge multiple objects into one b) To copy properties from one object to another c) To compare two objects d) To delete properties from an object Answer: b) To copy properties from one object to another Explanation: The Object.assign() method is used to

What is the purpose of the Object.assign() method in JavaScript? Read More »

What will the console.log([1, 2, 3] + [4, 5, 6]) statement output?

What will the console.log([1, 2, 3] + [4, 5, 6]) statement output? a) "1,2,34,5,6" b) "1,2,3,4,5,6" c) [1,2,3,4,5,6] d) NaN Answer: a) "1,2,34,5,6" Explanation: In JavaScript, when you use the + operator with arrays, it converts the arrays to strings and concatenates them. Therefore, the result is "1,2,34,5,6", a concatenated string of the two arrays.

What will the console.log([1, 2, 3] + [4, 5, 6]) statement output? Read More »

What will the console.log([] == false) statement output?

What will the console.log([] == false) statement output? a) true b) false c) undefined d) NaN Answer: a) true Explanation: When comparing an empty array [] with false using the loose equality operator (==), JavaScript converts the array to a boolean and compares it with false, resulting in true. Reference: JavaScript MCQ (Multiple-Choice Questions)

What will the console.log([] == false) statement output? Read More »

Scroll to Top