Are You Ready to Challenge Your JavaScript Skills 250 Question Quiz 140 Page PDF guide with explanations


🌟 Are You Ready to Challenge Your JavaScript Skills? 🌟

πŸš€ Introducing the Ultimate JavaScript Quiz: 250 Questions to Test Your Knowledge! 🧠

πŸ“š Dive into a comprehensive set of questions covering:

  1. Variables and Scoping: #LetKeyword #JavaScriptArrays #CodingConstants
  2. Functions and Arrow Functions: #ArrowFunctions #CallbackFunctions #FunctionDeclarations
  3. Array Methods and Operations: #ArrayUnshift #ArrayReduce #JavaScriptMap #ArrayFilter
  4. Object Manipulation and JSON: #ReadOnlyProperties #JSONData #ObjectSerialization
  5. Asynchronous Programming and Promises: #AsyncJavaScript #Promises #ErrorHandling
  6. Event Handling and DOM Interaction: #EventBubbling #DOMManipulation #EventDelegation
  7. JavaScript Syntax and Operators: #CodingSyntax #JavaScriptOperators #LoopControl
  8. Advanced Concepts and Miscellaneous: #JavaScriptClosures #ThisKeyword #ExceptionHandling #PackageManager

πŸ‘¨β€πŸ’» Whether you’re a beginner looking to learn or a seasoned pro wanting to test your skills, this quiz has something for everyone!

Test Your Knowledge Today!

Question 1 What does let keyword in JavaScript signify?

A. A constant variable

B. A block-scoped variable

C. A global variable

D. A function-scoped variable

Answer: B. A block-scoped variable

Explanation: The let keyword allows you to declare variables that are limited in scope to the block, statement, or expression in which they are used. Unlike var, which declares a function-scoped or globally-scoped variable, let ensures that the variable is only accessible within its declared context.

if (true) {

 let a = 40;

 console.log(a); // outputs 40

}

console.log(a); // Uncaught ReferenceError: a is not defined

Question 2 Which method converts a JSON string into a JavaScript object?

A. JSON.parse()

B. JSON.stringify()

C. Object.toJSON()

D. String.parseJSON()

Answer: A. JSON.parse()

Explanation: JSON.parse() is used to parse a JSON string and construct the JavaScript value or object described by the string.

const jsonString = ‘{“name”: “John”, “age”: 30}’;

const obj = JSON.parse(jsonString);

console.log(obj.name); // John

Question 3 Which operator is used to check both the value and the type?

A. ==

B. ===

C. =

D. !=

Answer: B. ===

Explanation: The === operator is known as the strict equality operator and it compares both the value and the type of two operands. In contrast, the == operator only compares the values, performing type coercion if necessary.

console.log(1 === ‘1’); // false

console.log(1 == ‘1’); // true

Question 4 What will be the output of the following code?

console.log(typeof null);

A. “null”

B. “undefined”

C. “object”

D. “number”

Answer: C. “object”

Explanation: In JavaScript, typeof null returns “object”. This is actually a bug in the language and one of its peculiarities, as null is not really an object.

Question 5 What is a closure in JavaScript?

A. A function along with its lexical environment.

B. An inner function that has access to the outer function’s variables.

C. Both A and B.

D. A syntax error.

Answer: C. Both A and B.

Explanation: A closure is a function combined with its lexical environment (the environment in which it was declared). This means it has access to outer function’s variables even after the outer function has returned.

function outerFunction() {

 let outerVariable = 100;

 function innerFunction() {

 console.log(outerVariable);

 }

 return innerFunction;

}

const inner = outerFunction();

inner(); // Outputs: 100

Question 6 Which of the following is not a reserved word in JavaScript?

A. interface

B. throws

C. program

D. short

Answer: C. program

Explanation: In JavaScript, interface, throws, and short are reserved words, but program is not. Reserved words have a specific meaning to the language or are reserved for future use.

Question 7 What is the purpose of the Array.prototype.map() method?

A. To execute a function on each element in the array.

B. To modify each element in the array and create a new array.

C. To combine all elements of an array into a single value.

D. To check if at least one element in the array passes a test.

Answer: B. To modify each element in the array and create a new array.

Explanation: Array.prototype.map() creates a new array populated with the results of calling a provided function on every element in the calling array.

const numbers = [1, 2, 3];

const doubled = numbers.map(number => number * 2);

console.log(doubled); // [2, 4, 6]

Question 8 What is the use of the bind method in JavaScript?

A. To bind the value of this in a method to an object.

B. To combine two strings.

C. To link a JavaScript file to an HTML document.

D. To repeat a string multiple times.

Answer: A. To bind the value of this in a method to an object.

Explanation: The bind method creates a new function that, when called, has its this keyword set to the provided value.

const person = {

 name: ‘John’,

 greet: function() { console.log(`Hello, ${this.name}`); }

};

const greet = person.greet.bind({name: ‘Jane’});

greet(); // Hello, Jane

Question 9 Which statement is true about JavaScript’s arrow functions?

A. They have their own this context.

B. They cannot return values.

C. They are shorter syntax for writing function expressions.

D. They can be used as constructors.

Answer: C. They are shorter syntax for writing function expressions.

Explanation: Arrow functions provide a concise syntax for writing function expressions. They don’t have their own bindings to this, arguments, super, or new.target, and are not suitable for use as constructors.

const add = (a, b) => a + b;

console.log(add(2, 3)); // 5

Question 10 What does the unshift method do in an array?

A. Adds new elements to the end of an array.

B. Removes the first element from an array.

C. Adds new elements to the beginning of an array.

D. Removes the last element from an array.

Answer: C. Adds new elements to the beginning of an array.

Explanation: The unshift method adds one or more elements to the beginning of an array and returns the new length of the array.

const array = [1, 2, 3];

array.unshift(0);

console.log(array); // [0, 1, 2, 3]

Question 11 What will the following code output?

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

A. true
B. false
C. TypeError
D. undefined

Answer: B. false

Explanation: This is a classic floating point math problem in many programming languages. The result of 0.1 + 0.2 is not exactly 0.3 due to the way numbers are represented in JavaScript (IEEE 754 standard for floating-point arithmetic).

Question 12 What is event delegation in JavaScript?

A. A way to handle events on parent elements rather than individual child elements.

B. A method to delegate event handling to a different element than the one that received the event.

C. A technique to speed up event handling.

D. A process to bind multiple events to a single event listener.

Answer: A. A way to handle events on parent elements rather than individual child elements.

Explanation: Event delegation is a technique where you use a single event listener on a parent element to manage all of its child events, taking advantage of event bubbling.

Question 13 Which method is used to remove the last element from an array and returns that element?

A. pop()
B. push()
C. shift()
D. unshift()

Answer: A. pop()

Explanation: pop() is used to remove the last element from an array and returns that element. This method changes the length of the array.

Question 14 How can you get the total number of arguments passed to a function?

A. Using arguments.length
B. Using arguments.size()
C. Using this.length
D. Using function.length

Answer: A. Using arguments.length

Explanation: The arguments object is an array-like object accessible inside functions that contains the values of the arguments passed to that function. arguments.length provides the number of arguments actually passed to the function.

Question 15 Which statement about asynchronous requests in JavaScript is true?

A. They pause the execution of the script until the request is completed.

B. They allow the script to continue running while the request is being processed.

C. They are only possible using the XMLHttpRequest object.

D. They cannot handle HTTP GET requests.

Answer: B. They allow the script to continue running while the request is being processed.

Explanation: Asynchronous requests in JavaScript, such as those made using XMLHttpRequest or the fetch API, allow the script to continue running while the request is processed in the background.

Question 16 What is the purpose of the Array.prototype.reduce() method?

A. To iterate over each element of an array.

B. To execute a reducer function on each element of the array, resulting in a single output value.

C. To filter out certain elements of an array.

D. To transform an array into a string.

Answer: B. To execute a reducer function on each element of the array, resulting in a single output value.

Explanation: The reduce() method executes a reducer function on each element of the array, resulting in a single output value. It is often used for accumulating values or concatenating strings.

Question 17 In JavaScript, what will this code typeof NaN return?

A. “number”
B. “NaN”
C. “undefined”
D. “object”

Answer: A. “number”

Explanation: NaN stands for “Not-a-Number” but is ironically of type “number” in JavaScript. This is one of the language’s quirks.

Question 18 Which is a correct method to create a new object in JavaScript?

A. let obj = new Object();
B. let obj = Object.create();
C. let obj = {};
D. All of the above

Answer: D. All of the above

Explanation: All three methods are correct ways to create a new object in JavaScript. new Object() and {} (object literal syntax) are more common, whereas Object.create() is used for more complex cases like setting the prototype chain.

Question 19 How can you add a comment in a JavaScript file?

A. <!– This is a comment –>
B. /* This is a comment */
C. // This is a comment
D. Both B and C

Answer: D. Both B and C

Explanation: In JavaScript, you can create a single-line comment using // and a multi-line comment using /* */.

Question 20 Which method is used to randomly shuffle an array in JavaScript?

A. There is no built-in method, but it can be achieved by custom code.

B. Array.shuffle()

C. Math.random(array)

D. array.randomize()

Answer: A. There is no built-in method, but it can be achieved by custom code.

Explanation: JavaScript does not provide a built-in method to shuffle an array. However, you can achieve this by implementing a function, commonly using the Fisher-Yates (or Knuth) shuffle algorithm.

function shuffle(array) {

 for (let i = array.length – 1; i > 0; i–) {

 const j = Math.floor(Math.random() * (i + 1));

 [array[i], array[j]] = [array[j], array[i]];

 }

 return array;

}

Question 21 What is the correct syntax for referring to an external script called script.js?

A. <script href=”script.js”></script>

B. <script name=”script.js”></script>

C. <script src=”script.js”></script>

D. <script file=”script.js”></script>

Answer: C. <script src=”script.js”></script>

Explanation: The src attribute in the <script> tag is used to include external JavaScript files.

Question 22 How do you write an if statement in JavaScript?

A. if (i == 5)

B. if i = 5 then

C. if i == 5 then

D. if (i = 5)

Answer: A. if (i == 5)

Explanation: The correct syntax for an if statement in JavaScript is if (condition).

Question 23 How can you detect the client’s browser name in JavaScript?

A. navigator.appName

B. browser.name

C. client.browser

D. window.browserName

Answer: A. navigator.appName

Explanation: The navigator.appName property returns the name of the browser. However, detecting the browser using this method is generally discouraged in favor of feature detection.

Question 24 Which JavaScript label catches all the values, except for the ones specified?

A. catch

B. label

C. default

D. except

Answer: C. default

Explanation: In a switch statement, the default keyword is used to specify the code to run if there is no case match.

Question 25 What will the following code return: Boolean(10 > 9)

A. true

B. false

C. NaN

D. undefined

Answer: A. true

Explanation: Boolean(10 > 9) evaluates the expression 10 > 9, which is true.

Question 26 Which event occurs when the user clicks on an HTML element?

A. onchange

B. onclick

C. onmouseover

D. onmouseclick

Answer: B. onclick

Explanation: The onclick event occurs when the user clicks on an element.

Question 27 How do you declare a JavaScript variable?

A. v carName;

B. variable carName;

C. var carName;

D. let carName;

Answer: C. var carName; and D. let carName;

Explanation: Both var and let are used to declare a variable in JavaScript, but they have different scopes and hoisting behavior.

Question 28 Which operator is used to assign a value to a variable?

A. *

B. –

C. =

D. x

Answer: C. =

Explanation: The = operator is used to assign values to variables in JavaScript.

Question 29 What is the correct way to write a JavaScript array?

A. var colors = (1:”red”, 2:”green”, 3:”blue”)

B. var colors = “red”, “green”, “blue”

C. var colors = [“red”, “green”, “blue”]

D. var colors = 1 = (“red”), 2 = (“green”), 3 = (“blue”)

Answer: C. var colors = [“red”, “green”, “blue”]

Explanation: In JavaScript, an array is written as a list of values, each separated by a comma, and enclosed in square brackets.

Question 30 What will the following code output?

console.log(typeof undefined === typeof NULL);

A. true

B. false

C. TypeError

D. undefined

Answer: B. false

Explanation: typeof undefined is ‘undefined’, but typeof NULL will be ‘undefined’ only if NULL is not declared. Since JavaScript is case-sensitive, NULL is not the same as null. If NULL isn’t declared, this will result in a ReferenceError.

Question 31 What is the method for rounding a number to the nearest integer in JavaScript?

A. Math.round()

B. Math.floor()

C. Math.ceil()

D. Number.round()

Answer: A. Math.round()

Explanation: Math.round() is used to round a number to the nearest integer.

Question 32 Which of the following will check if a variable v is an array in JavaScript?

A. v.isArray()

B. Array.isArray(v)

C. v instanceof Array

D. Both B and C are correct

Answer: D. Both B and C are correct

Explanation: Array.isArray(v) is a standard method to check if v is an array. v instanceof Array also works but it might not be as reliable in all contexts, especially when dealing with different JavaScript contexts (like iframes).

Question 33 How do you find the number with the highest value of x and y?

A. Math.ceil(x, y)

B. Math.max(x, y)

C. Math.top(x, y)

D. Math.highest(x, y)

Answer: B. Math.max(x, y)

Explanation: Math.max(x, y) returns the highest value among its arguments.

Question 34 What will the following code output?

var x = “1”;

console.log(x === 1);

A. true

B. false

C. TypeError

D. undefined

Answer: B. false

Explanation: The === operator tests for both value and type equality. Since x is a string and 1 is a number, the comparison returns false.

Question 35 What is the correct JavaScript syntax to change the content of the HTML element below?

<p id=”demo”>This is a demonstration.</p>

A. document.getElement(“p”).innerHTML = “Hello World!”;

B. document.getElementById(“demo”).innerHTML = “Hello World!”;

C. #demo.innerHTML = “Hello World!”;

D. document.getElementByName(“p”).innerHTML = “Hello World!”;

Answer: B. document.getElementById(“demo”).innerHTML = “Hello World!”;

Explanation: document.getElementById(“demo”) is the correct method to select the element, and .innerHTML is used to change its content.

Question 36 What is the correct syntax for adding comments in JavaScript?

A. <!– This is a comment –>

B. // This is a comment

C. /* This is a comment */

D. Both B and C

Answer: D. Both B and C

Explanation: In JavaScript, // is used for single-line comments and /* */ for multi-line comments.

Question 37 How can a value be appended to an array in JavaScript?

A. arr[arr.length] = value;

B. arr.push(value);

C. arr += value;

D. Both A and B are correct

Answer: D. Both A and B are correct

Explanation: arr.push(value) adds a new element to the end of an array. Alternatively, setting arr[arr.length] = value; also appends the value to the end.

Question 38 How do you create a function in JavaScript?

A. function myFunction()

B. function:myFunction()

C. function = myFunction()

D. new Function()

Answer: A. function myFunction()

Explanation: function myFunction() is the standard syntax for declaring a function in JavaScript.

Question 39 What method is used to serialize an object into a JSON string in JavaScript?

A. JSON.stringify()

B. JSON.parse()

C. JSON.toObject()

D. Object.toJSON()

Answer: A. JSON.stringify()

Explanation: JSON.stringify() is used to convert an object into a JSON string.

Question 40 What is the purpose of the this keyword in JavaScript?

A. It refers to the current object

B. It refers to the previously called object

C. It is a variable that stores data

D. It is a constant value

Answer: A. It refers to the current object

Explanation: In JavaScript, this is a reference to the current object the code is being written inside. Its value depends on where it is used.

Question 41 Which of the following is true about the forEach method in JavaScript?

A. It changes the original array.

B. It returns a new array.

C. It can break out of the loop using a break statement.

D. It executes a provided function once for each array element.

Answer: D. It executes a provided function once for each array element.

Explanation: forEach() method executes a provided function once for each array element. Unlike map or filter, it does not return a new array and cannot be broken out of using break or return.

Question 42 What does the following expression return?

typeof “x” === “string”

A. true

B. false

C. TypeError

D. undefined

Answer: A. true

Explanation: The typeof operator returns a string indicating the type of the unevaluated operand. In this case, “x” is a string, so typeof “x” returns “string”.

Question 43 How do you create a Promise in JavaScript?

A. let promise = Promise(value);

B. let promise = new Promise((resolve, reject) => { … });

C. let promise = Promise.create((resolve, reject) => { … });

D. let promise = create Promise((resolve, reject) => { … });

Answer: B. let promise = new Promise((resolve, reject) => { … });

Explanation: Promises in JavaScript are created using the new Promise() constructor, which takes a function with resolve and reject arguments.

Question 44 Which method is used to sort the elements of an array in place and returns the sorted array?

A. Array.sort()

B. Array.order()

C. Array.arrange()

D. Array.sequence()

Answer: A. Array.sort()

Explanation: Array.sort() is a method that sorts the elements of an array in place and returns the sorted array.

Question 45 What is the output of the following code?

let x = 1;

function func() {

 console.log(x);

 let x = 2;

}

func();

A. 1

B. 2

C. undefined

D. ReferenceError

Answer: D. ReferenceError

Explanation: Due to JavaScript’s hoisting, the local variable x is hoisted to the top of the function, but it’s not initialized until let x = 2; is executed. Accessing x before the declaration results in a ReferenceError.

Question 46 In JavaScript, what will the following return?

isNaN(“hello”)

A. true

B. false

C. TypeError

D. undefined

Answer: A. true

Explanation: The isNaN() function returns true if the value is NaN or not a number. The string “hello” is not a number, so the function returns true.

Question 47 What does the !! (double exclamation point) operator do in JavaScript?

A. It inverts the boolean value of an expression twice.

B. It converts a value to a boolean with the same truthiness.

C. It is a syntax error.

D. It multiplies the value by -1 twice.

Answer: B. It converts a value to a boolean with the same truthiness.

Explanation: The !! operator converts a value to a boolean while preserving its truthiness. The first ! negates the value (converting it to a boolean), and the second ! negates it again.

Question 48 What is the correct syntax for importing a module in modern JavaScript?

A. import name from “module-name”;

B. require(“module-name”);

C. module.import(“module-name”);

D. include “module-name”;

Answer: A. import name from “module-name”;

Explanation: In modern JavaScript (ES6 and later), the import statement is used to import functions, objects, or primitives from another module or file.

Question 49 What will be the output of the following code snippet?

console.log(1 + “2” + “2”);

A. 5

B. 122

C. 14

D. Error

Answer: B. 122

Explanation: In JavaScript, the + operator is also used for string concatenation. When used with a string, numbers are converted to strings. Hence, 1 + “2” becomes “12”, and “12” + “2” becomes “122”.

Question 50 Which statement about setTimeout() function in JavaScript is true?

A. It pauses the execution of the function for a specified time.

B. It executes a function once after a specified time.

C. It repeatedly calls a function with a fixed time delay between each call.

D. It is a synchronous function.

Answer: B. It executes a function once after a specified time.

Explanation: The setTimeout() function is used to execute a function once after a specified period of time in milliseconds. It is asynchronous, meaning the rest of the code will continue to execute while the timer counts down.

Question 51 What does the following JavaScript code return?

[1, 2, 3].map(num => {

 if (typeof num === ‘number’) return;

 return num * 2;

});

A. [undefined, undefined, undefined]

B. [null, null, null]

C. [2, 4, 6]

D. []

Answer: A. [undefined, undefined, undefined]

Explanation: The map function here checks if each element is a number and if so, it returns undefined (since there’s no explicit return value). If the element is not a number, it would return num * 2, but since all elements are numbers, the array will be filled with undefined.

Question 52 What does the following code output?

let a = [1, 2, 3];

let b = a;

b.push(4);

console.log(a);

A. [1, 2, 3]

B. [1, 2, 3, 4]

C. TypeError

D. undefined

Answer: B. [1, 2, 3, 4]

Explanation: Since b is a reference to the same array as a, modifying b will also modify a. Therefore, when you push 4 into b, a is also updated.

Question 53 Which of the following is a way to define an object in JavaScript?

A. var person = {firstName:”John”, lastName:”Doe”};

B. var person = Object(“firstName: John, lastName: Doe”);

C. var person = new Object(firstName: “John”, lastName: “Doe”);

D. var person = {Person(“firstName: John, lastName: Doe”)};

Answer: A. var person = {firstName:”John”, lastName:”Doe”};

Explanation: Option A uses object literal syntax, which is a common and concise way to create objects in JavaScript.

Question 54 How can you detect if a variable is an array in JavaScript?

A. variable.isArray()

B. Array.isArray(variable)

C. variable instanceof Array

D. Both B and C are correct

Answer: D. Both B and C are correct

Explanation: Array.isArray(variable) is a standard method for determining if a variable is an array. variable instanceof Array also works for this purpose.

Question 55 What does the following JavaScript code do?

let x = “5”;

let y = x++;

A. Increments x and then assigns the incremented value to y.

B. Assigns the value of x to y and then increments x.

C. Results in a runtime error.

D. None of the above.

Answer: B. Assigns the value of x to y and then increments x.

Explanation: The x++ operator first returns the value of x and then increments it. So, y gets the value of x before incrementation.

Question 56 In JavaScript, which method is used to remove the first element from an array and return that element?

A. shift()

B. unshift()

C. pop()

D. push()

Answer: A. shift()

Explanation: The shift() method removes the first element from an array and returns that removed element.

Question 57 What is the main difference between let and const in JavaScript?

A. let declarations are block-scoped, const declarations are not.

B. let allows reassignment, const does not.

C. let is function-scoped, const is block-scoped.

D. let can store objects and arrays, const cannot.

Answer: B. let allows reassignment, const does not.

Explanation: Both let and const are block-scoped. However, variables declared with let can be reassigned, while variables declared with const cannot be reassigned.

Question 58 What is the output of the following code?

console.log(typeof null);

A. “null”

B. “undefined”

C. “object”

D. “number”

Answer: C. “object”

Explanation: In JavaScript, typeof null returns “object”. This is actually a long-standing bug in JavaScript, as null is not really an object.

Question 59 What will be logged in the console in the following code?

let num = 8;

let newNum = num++;

console.log(num, newNum);

A. 8, 8

B. 8, 9

C. 9, 8

D. 9, 9

Answer: C. 9, 8

Explanation: num++ is a post-increment operator, so it returns num (which is 8) before incrementing it. Hence, newNum is 8, and num becomes 9.

Question 60 Which statement will correctly call a function named myFunction?

A. call myFunction();

B. myFunction();

C. call function myFunction();

D. execute myFunction();

Answer: B. myFunction();

Explanation: In JavaScript, you call a function by writing the function’s name followed by parentheses. So, myFunction(); is the correct syntax.

Question 61 What is the output of this code snippet?

let x = “Hello”;

let y = new String(“Hello”);

console.log(x === y);

A. true

B. false

C. TypeError

D. undefined

Answer: B. false

Explanation: x is a string literal, while y is an object created by the String constructor. In JavaScript, the === operator checks for both value and type, so these two are not considered the same.

Question 62 Which method can be used to combine two or more arrays in JavaScript?

A. Array.join()

B. Array.concat()

C. Array.merge()

D. Array.combine()

Answer: B. Array.concat()

Explanation: Array.concat() is used to merge two or more arrays into a new array without modifying the original arrays.

Question 63 What will the following code log to the console?

console.log(typeof (() => {}));

A. “function”

B. “object”

C. “undefined”

D. “string”

Answer: A. “function”

Explanation: An arrow function () => {} is a type of function, so typeof for an arrow function will return “function”.

Question 64 Which of the following is the correct way to create an object in JavaScript?

A. var obj = {};

B. var obj = new Object();

C. var obj = Object.create(null);

D. All of the above

Answer: D. All of the above

Explanation: All three methods are correct for creating an object in JavaScript. The first uses object literal syntax, the second uses the Object constructor, and the third uses Object.create().

Question 65 What does the following JavaScript code return?

“Cat” && “Dog”

A. “Cat”

B. “Dog”

C. true

D. false

Answer: B. “Dog”

Explanation: The logical AND (&&) operator returns the value of the second operand if the first is truthy. Since “Cat” is truthy, it returns “Dog”.

Question 66 What does the delete operator in JavaScript do?

A. It deletes variables.

B. It removes properties from objects.

C. It deletes elements from an array.

D. It removes a file from the server.

Answer: B. It removes properties from objects.

Explanation: The delete operator is used to remove properties from objects. It does not delete variables or array elements (it will leave a hole in the array).

Question 67 What does the following code output?

let x = 1;

let y = x !== 2;

console.log(y);

A. 1

B. false

C. true

D. undefined

Answer: C. true

Explanation: The expression x !== 2 is true because x (which is 1) is not equal to 2.

Question 68 What is the output of this code snippet?

console.log(typeof “Hello” === “string”);

A. true

B. false

C. TypeError

D. undefined

Answer: A. true

Explanation: typeof “Hello” returns “string”, and so the comparison “string” === “string” is true.

Question 69 What is the purpose of template literals in JavaScript?

A. They allow you to perform complex calculations.

B. They enable secure data transmission.

C. They provide an easy way to interpolate variables and expressions into strings.

D. They improve the performance of string operations.

Answer: C. They provide an easy way to interpolate variables and expressions into strings.

Explanation: Template literals (denoted by backticks `) allow embedded expressions and variables, making it easier to create complex strings.

Question 70 What does the ?. operator signify in JavaScript?

A. Nullish Coalescing

B. Logical AND

C. Optional Chaining

D. Exclusive OR

Answer: C. Optional Chaining

Explanation: The optional chaining operator ?. allows you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

Question 71 What is the purpose of the Array.prototype.filter() method in JavaScript?

A. To update every element in an array based on a condition.

B. To create a new array with elements that pass a test provided by a function.

C. To return the first element that satisfies a provided testing function.

D. To execute a function once for each array element.

Answer: B. To create a new array with elements that pass a test provided by a function.

Explanation: Array.prototype.filter() creates a new array with all elements that pass the test implemented by the provided function.

Question 72 What is the output of this JavaScript code?

let a = 3;

let b = new Number(3);

console.log(a == b);

console.log(a === b);

A. true, true

B. false, false

C. true, false

D. false, true

Answer: C. true, false

Explanation: a == b is true because JavaScript performs type coercion and compares the values. However, a === b is false because they are of different types (number vs object).

Question 73 In JavaScript, which of these statements will create a copy of an object?

A. let objCopy = Object.assign({}, obj);

B. let objCopy = {…obj};

C. let objCopy = JSON.parse(JSON.stringify(obj));

D. All of the above

Answer: D. All of the above

Explanation: All three methods can be used to create a shallow copy of an object in JavaScript.

Question 74 What will the following JavaScript code log to the console?

function test() {

 return

 {

 message: “hello”

 };

}

console.log(test());

A. { message: “hello” }

B. undefined

C. SyntaxError

D. null

Answer: B. undefined

Explanation: Due to JavaScript’s automatic semicolon insertion, the return statement is considered to end after return. Therefore, the function returns undefined.

Question 75 Which JavaScript method is used for parsing a string to an integer?

A. Number.parse()

B. Math.parseInt()

C. parseInt()

D. String.toInt()

Answer: C. parseInt()

Explanation: parseInt() is a function used to parse a string and return an integer.

Question 76 What does the map() method do in JavaScript?

A. It changes the original array.

B. It performs a redirect to a new URL.

C. It calls a provided function on every element in an array and creates a new array.

D. It concatenates all elements of an array into a string.

Answer: C. It calls a provided function on every element in an array and creates a new array.

Explanation: map() creates a new array with the results of calling a provided function on every element in the calling array.

Question 77 How can you add a key-value pair to a JavaScript object after it has been created?

A. obj[key] = value;

B. obj.add(key, value);

C. obj.push({key: value});

D. obj.concat({key: value});

Answer: A. obj[key] = value;

Explanation: You can add a new key-value pair to an object by using the syntax obj[key] = value; where key is the name of the property and value is the value you want to assign.

Question 78 Which statement about JavaScript’s switch statement is correct?

A. The switch statement is used to select one of many code blocks to be executed.

B. The switch statement can only test for equality with string values.

C. The switch statement tests expressions strictly (using ===).

D. The switch statement can only have one case for each value.

Answer: A. The switch statement is used to select one of many code blocks to be executed.

Explanation: A switch statement evaluates an expression and executes code blocks based on the case that matches the expression’s value. It can test for equality with any data type, and multiple cases can match the same value.

Question 79 What will the following code output?

console.log(“5” + 2);

console.log(“5” – 2);

A. 52 and 3

B. 7 and 3

C. 52 and NaN

D. 7 and NaN

Answer: A. 52 and 3

Explanation: In JavaScript, the + operator is used for string concatenation as well as addition. So “5” + 2 results in “52”. The – operator, however, is only for subtraction, so “5” – 2 results in 3.

Question 80 In JavaScript, how do you check if a variable a is an instance of an object A?

A. a.instanceof(A)

B. A.instanceof(a)

C. a instanceof A

D. instanceof a(A)

Answer: C. a instanceof A

Explanation: The instanceof operator checks whether an object in its left-hand operand is an instance of an object in its right-hand operand.

Question 81 What is a typical use case for an anonymous function in JavaScript?

A. As a callback function

B. For recursive function calls

C. To define a class

D. To initialize global variables

Answer: A. As a callback function

Explanation: Anonymous functions are commonly used as callback functions in JavaScript. They are functions that don’t have a name and can be defined in the place where they are used, often in response to events or as arguments to other functions.

Question 82 What will the following code output?

let num = 10;

console.log(++num);

A. 10

B. 11

C. 9

D. TypeError

Answer: B. 11

Explanation: The ++ operator before the variable (++num) is a pre-increment operator, which increments num by 1 before returning its value. So, it outputs 11.

Question 83 Which of the following is a correct method to clone an array in JavaScript?

A. let newArray = […oldArray];

B. let newArray = Array.from(oldArray);

C. let newArray = oldArray.slice();

D. All of the above

Answer: D. All of the above

Explanation: All three methods (spread syntax, Array.from(), and slice()) are correct for cloning an array in JavaScript. They create a new array instance with the same elements.

Question 84 What does the finally block do in a try-catch statement?

A. It runs only if an error occurs in the try block.

B. It runs regardless of the result of the try-catch blocks.

C. It replaces the catch block if no errors occur.

D. It is used to finalize object cleanup.

Answer: B. It runs regardless of the result of the try-catch blocks.

Explanation: The finally block executes after the try and catch blocks but before statements following the try-catch statement. It always executes, regardless of whether an exception was thrown or caught.

Question 85 What is the output of the following JavaScript code?

let x = “0”;

console.log(Boolean(x));

A. true

B. false

C. 0

D. “0”

Answer: A. true

Explanation: In JavaScript, the string “0” is truthy, so Boolean(“0”) returns true.

Question 86 How do you find the character at a specific position in a string in JavaScript?

A. string.charAt(index)

B. string.at(index)

C. string.charCodeAt(index)

D. string.characterAt(index)

Answer: A. string.charAt(index)

Explanation: charAt(index) method returns the character at the specified index in a string.

Question 87 What is the use of the new keyword in JavaScript?

A. To create a new object instance from a class or constructor function.

B. To create a new variable.

C. To declare a new function.

D. To initialize a new array.

Answer: A. To create a new object instance from a class or constructor function.

Explanation: The new keyword is used to create an instance of an object that has a constructor function.

Question 88 Which method is used to add an event listener in modern JavaScript?

A. object.addEventListener(event, function)

B. object.attachEvent(event, function)

C. object.onEvent = function

D. object.captureEvent(event, function)

Answer: A. object.addEventListener(event, function)

Explanation: addEventListener() is the modern method to attach an event listener to an element. It allows you to specify the event to listen for and the callback function to execute when the event is triggered.

Question 89 What is the result of the following expression?

“Hello” – “World”

A. NaN

B. 0

C. “HelloWorld”

D. TypeError

Answer: A. NaN

Explanation: Attempting arithmetic subtraction on non-numeric strings results in NaN (Not a Number).

Question 90 How can you convert a JSON object to a string in JavaScript?

A. JSON.stringify(jsonObject)

B. jsonObject.toString()

C. JSON.parse(jsonObject)

D. String(jsonObject)

Answer: A. JSON.stringify(jsonObject)

Explanation: JSON.stringify(jsonObject) is used to convert a JSON object to a string.

Question 91 What does the static keyword do in JavaScript classes?

A. Makes a method or property belong to the class, rather than an instance of the class.

B. Declares a static type for variables.

C. Prevents a class from being instantiated.

D. Indicates that a property or method can’t be changed.

Answer: A. Makes a method or property belong to the class, rather than an instance of the class.

Explanation: The static keyword in JavaScript classes is used to define methods or properties on the class itself, rather than on instances of the class.

Question 92 Which of the following is not a valid JavaScript variable name?

A. 2names

B. _name

C. $name

D. name2

Answer: A. 2names

Explanation: In JavaScript, a variable name cannot start with a number. It can start with a letter, $, or _.

Question 93 How do you stop an interval timer in JavaScript?

A. clearInterval(timerID)

B. stopInterval(timerID)

C. removeInterval(timerID)

D. endInterval(timerID)

Answer: A. clearInterval(timerID)

Explanation: clearInterval(timerID) is used to stop a timer set with setInterval().

Question 94 What is the main difference between the == and === operators in JavaScript?

A. == checks for equality and === checks for data type.

B. == performs type coercion while === does not.

C. == is used to compare strings and === is used to compare numbers.

D. There is no difference.

Answer: B. == performs type coercion while === does not.

Explanation: The == operator compares two values after converting them to a common type (type coercion), whereas === compares both value and type without coercion.

Question 95 Which of these methods can be used to send a request to a server in JavaScript?

A. XMLHttpRequest

B. fetch

C. axios

D. All of the above

Answer: D. All of the above

Explanation: All three, XMLHttpRequest, fetch, and axios (a third-party library), can be used to make HTTP requests to a server in JavaScript.

Question 96 What will the following code output?

console.log(!!null);

A. true

B. false

C. null

D. TypeError

Answer: B. false

Explanation: The !! operator converts a value to a boolean equivalent. Since null is falsy in JavaScript, !!null will be false.

Question 97 What is the output of the following JavaScript code?

console.log(1 + “2” + 3);

A. 6

B. “123”

C. “15”

D. TypeError

Answer: B. “123”

Explanation: In this expression, JavaScript performs string concatenation from left to right, resulting in “12” followed by “123”.

Question 98 Which JavaScript method can be used to check if an object is an array?

A. Array.isArray()

B. isArray()

C. typeof

D. instanceof Array

Answer: A. Array.isArray()

Explanation: Array.isArray() is the preferred method to check if an object is an array. typeof will return “object” for arrays, and instanceof Array can give false negatives if arrays are from different frames.

Question 99 What does the reduce() method do in JavaScript?

A. It reduces the size of an array.

B. It applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

C. It filters out elements in an array that do not match a given condition.

D. It executes a provided function once for each array element.

Answer: B. It applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

Explanation: The reduce() method in JavaScript applies a function to an accumulator and each element in the array to reduce it to a single value.

Question 100 How can you add a comment in a JavaScript file?

A. <!– This is a comment –>

B. // This is a comment

C. /* This is a comment */

D. Both B and C

Answer: D. Both B and C

Explanation: In JavaScript, // is used for single-line comments and /* */ for multi-line comments.

Question 101 What will the following JavaScript code output?

console.log(typeof NaN);

A. “number”

B. “NaN”

C. “undefined”

D. “object”

Answer: A. “number”

Explanation: In JavaScript, the type of NaN (Not a Number) is ironically “number”.

Question 102 Which of the following is true about JavaScript’s arrow functions?

A. They can be used as constructors.

B. They have their own this context.

C. They are not hoisted.

D. They can contain a yield keyword.

Answer: C. They are not hoisted.

Explanation: Arrow functions in JavaScript are not hoisted, meaning they must be defined before they are used. Unlike regular functions, they do not have their own this context and cannot be used as constructors.

Question 103 How do you create a read-only property in an object in JavaScript?

A. Using the const keyword.

B. Using the Object.defineProperty() method.

C. Using the readonly attribute in the object declaration.

D. It is not possible to create read-only properties.

Answer: B. Using the Object.defineProperty() method.

Explanation: Object.defineProperty() allows you to define a property on an object and set its attributes, such as writable, to make it read-only.

Question 104 What will the following JavaScript code output?

let str = “Hello World!”;

console.log(str.slice(0, 5));

A. “Hello”

B. “Hello World”

C. “World!”

D. “H”

Answer: A. “Hello”

Explanation: The slice() method extracts a section of a string and returns it as a new string. slice(0, 5) extracts the first five characters.

Question 105 Which of these is a correct method to create a new and empty array in JavaScript?

A. let arr = new Array();

B. let arr = [];

C. let arr = Array();

D. All of the above

Answer: D. All of the above

Explanation: All three statements are valid ways to create an empty array in JavaScript.

Question 106 What is event bubbling in JavaScript?

A. When the browser’s default event handling is prevented.

B. The concept where an event propagates from the target element up through the ancestors in the DOM tree.

C. The process of binding two different events together.

D. A specific type of event that moves from the outermost element to the target element.

Answer: B. The concept where an event propagates from the target element up through the ancestors in the DOM tree.

Explanation: Event bubbling is a type of event propagation in the DOM where events start from the deepest (innermost) element and then propagate up through the parents.

Question 107 Which of the following methods can be used to send JSON data to the server?

A. XMLHttpRequest.send()

B. fetch()

C. axios.post()

D. All of the above

Answer: D. All of the above

Explanation: All these methods (XMLHttpRequest.send(), fetch(), and axios.post()) can be used to send JSON data to a server in JavaScript.

Question 108 What is the purpose of the async keyword in JavaScript?

A. To pause the execution of a function until a promise is resolved.

B. To indicate that a function can run asynchronously.

C. To automatically optimize code execution.

D. To define a new thread for running the function.

Answer: B. To indicate that a function can run asynchronously.

Explanation: The async keyword is used before a function to signify that it returns a promise and can be executed asynchronously.

Question 109 What does the spread operator do in JavaScript?

A. Spreads the elements of an iterable (like an array) into individual elements.

B. Joins multiple arrays into a single array.

C. Increases the value of numeric elements in an array.

D. Spreads the properties of an object over time.

Answer: A. Spreads the elements of an iterable (like an array) into individual elements.

Explanation: The spread operator (…) allows an iterable (e.g., array) to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.

Question 110 How can you get the total number of arguments passed to a function in JavaScript?

A. arguments.length

B. arguments.count

C. arguments.size

D. this.arguments.length

Answer: A. arguments.length

Explanation: The arguments object in a function contains an array of the arguments used when the function was called and arguments.length will give you the number of arguments passed to the function.

Question 111 What is the output of this JavaScript code?

let x = 10;

let y = “10”;

console.log(x == y);

console.log(x === y);

A. true, true

B. true, false

C. false, true

D. false, false

Answer: B. true, false

Explanation: The == operator checks for value equality with type coercion, so 10 (number) is equal to “10” (string) in terms of value. The === operator checks for both value and type equality without type coercion, so they are not strictly equal.

Question 112 Which of these is not a valid way to declare a function in JavaScript?

A. function myFunc() { … }

B. var myFunc = function() { … };

C. var myFunc = new Function(…);

D. var myFunc => { … };

Answer: D. var myFunc => { … };

Explanation: The correct syntax for an arrow function is var myFunc = () => { … };. The other options are valid ways to declare a function.

Question 113 What will the following JavaScript code output?

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

A. true

B. false

C. TypeError

D. undefined

Answer: B. false

Explanation: Due to floating point precision issues in JavaScript, 0.1 + 0.2 does not exactly equal 0.3.

Question 114 In JavaScript, what is a truthy value?

A. A value that is strictly true.

B. A value that translates to true when evaluated in a boolean context.

C. A value that is not false.

D. A value that is a boolean data type.

Answer: B. A value that translates to true when evaluated in a boolean context.

Explanation: In JavaScript, a truthy value is any value that is not falsy (i.e., not false, 0, “”, null, undefined, or NaN). When evaluated in a boolean context, such as an if statement, these values will be treated as true.

Question 115 Which of the following methods will correctly round the number 7.25 to the nearest integer in JavaScript?

A. Math.round(7.25)

B. Math.ceil(7.25)

C. Math.floor(7.25)

D. parseInt(7.25)

Answer: A. Math.round(7.25)

Explanation: Math.round(7.25) rounds the number to the nearest integer. Math.ceil() rounds up, Math.floor() rounds down, and parseInt() truncates the decimal part.

Question 116 How can you remove a specific element from an array in JavaScript?

A. array.pop(element)

B. delete array[index]

C. array.splice(index, 1)

D. array.remove(element)

Answer: C. array.splice(index, 1)

Explanation: array.splice(index, 1) is used to remove a specific element from an array by its index. pop() removes the last element, delete will leave an undefined hole in the array, and remove() is not a standard array method.

Question 117 What will the following JavaScript code output?

let a = “JavaScript”;

let b = a.substring(4, 7);

console.log(b);

A. “Script”

B. “ava”

C. “Java”

D. “aSc”

Answer: D. “aSc”

Explanation: substring(4, 7) extracts characters from index 4 to 7, not including 7, from the string a. In “JavaScript”, these characters are aSc.

Question 118 Which of the following is a way to define an arrow function that returns the square of its input in JavaScript?

A. const square = (x) => { x * x; };

B. const square = x => x * x;

C. const square = x => { return x * x; };

D. Both B and C are correct

Answer: D. Both B and C are correct

Explanation: Both options B and C correctly define an arrow function that returns the square of its input. Option B uses a concise body syntax that implicitly returns the expression, while option C uses a block body with an explicit return statement.

Question 119 In JavaScript, what is a Callback Function?

A. A function that is executed before another function.

B. A function passed into another function as an argument, which is then invoked inside the outer function.

C. A function that calls back to the server for data.

D. A method of exiting a function early.

Answer: B. A function passed into another function as an argument, which is then invoked inside the outer function.

Explanation: A callback function is a function passed into another function as an argument, which is then executed inside the outer function to complete some kind of action or routine.

Question 120 What is the output of the following JavaScript code snippet?

let obj = { a: 1, b: 2 };

let objCopy = Object.assign({}, obj);

objCopy.a = 3;

console.log(obj.a);

A. 1

B. 2

C. 3

D. undefined

Answer: A. 1

Explanation: Object.assign({}, obj) creates a shallow copy of obj. Changing objCopy.a to 3 does not affect the original obj, so obj.a remains 1.

Question 121 Which of the following is a correct way to declare a class in JavaScript?

A. class MyClass { constructor() {…} }

B. function MyClass() {…}

C. var MyClass = class { constructor() {…} }

D. Both A and C are correct

Answer: D. Both A and C are correct

Explanation: Both class MyClass { … } and var MyClass = class { … } are correct ways to declare a class in JavaScript. The first is a class declaration and the second is a class expression.

Question 122 What will this JavaScript code output?

console.log(4 + 5 + “px”);

console.log(“px” + 5 + 4);

A. “9px” and “px54”

B. “9px” and “px9”

C. “45px” and “px45”

D. “45px” and “px54”

Answer: A. “9px” and “px54”

Explanation: In the first expression, numbers are added before concatenating with a string, resulting in “9px”. In the second expression, string concatenation occurs from left to right, resulting in “px54”.

Question 123 How do you create a new Date object in JavaScript?

A. let now = new Date();

B. let now = Date.now();

C. let now = Date.create();

D. let now = new Date.now();

Answer: A. let now = new Date();

Explanation: new Date() creates a new Date object representing the current date and time. Date.now() returns the number of milliseconds since the Unix Epoch and does not create a Date object.

Question 124 Which method is used to encode a URI component in JavaScript?

A. encodeURI()

B. encodeURIComponent()

C. escape()

D. stringifyURI()

Answer: B. encodeURIComponent()

Explanation: encodeURIComponent() is used to encode a URI component by escaping all characters except alphabetic, decimal digits, and a few special characters.

Question 125 What will this code output?

let a = 10;

function myFunction() {

 let b = a + 10;

 return b;

}

console.log(myFunction());

A. 10

B. 20

C. undefined

D. ReferenceError

Answer: B. 20

Explanation: The function myFunction() adds 10 to the value of a (which is 10) and returns the result. Therefore, it will output 20.

Question 126 What is the correct way to write a JavaScript array?

A. var colors = 1 : (“red”), 2 : (“green”), 3 : (“blue”);

B. var colors = (1:”red”, 2:”green”, 3:”blue”);

C. var colors = [“red”, “green”, “blue”];

D. var colors = “red”, “green”, “blue”;

Answer: C. var colors = [“red”, “green”, “blue”];

Explanation: In JavaScript, an array is defined as a list of elements, separated by commas, and enclosed in square brackets.

Question 127 What will the following code output to the console?

let num = 5;

console.log(num++);

A. 5

B. 6

C. undefined

D. TypeError

Answer: A. 5

Explanation: num++ is the post-increment operator, so it returns the value of num before incrementing. The value of num is 5, so it logs 5 and then num becomes 6.

Question 128 Which statement about setTimeout in JavaScript is true?

A. It pauses the execution of the entire script for a specified duration.

B. It calls a function or executes a code snippet after a specified delay.

C. It is a blocking operation and prevents other operations from executing until it’s finished.

D. It can only execute a function once.

Answer: B. It calls a function or executes a code snippet after a specified delay.

Explanation: setTimeout is used to execute a function or a code snippet after a delay, specified in milliseconds. It is non-blocking and allows other code to run in the meantime.

Question 129 What does the following JavaScript code return?

typeof (“5” + 3)

A. “number”

B. “string”

C. “undefined”

D. “object”

Answer: B. “string”

Explanation: In JavaScript, the + operator with a string will result in string concatenation, so “5” + 3 becomes “53”, which is a string.

Question 130 What is the primary use of the bind method in JavaScript?

A. To join two strings together.

B. To delay the execution of a function.

C. To create a new function that, when called, has its this keyword set to a provided value.

D. To bind an HTML element to a JavaScript function.

Answer: C. To create a new function that, when called, has its this keyword set to a provided value.

Explanation: The bind method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Question 131 Which of the following is not a primitive data type in JavaScript?

A. string

B. number

C. boolean

D. object

Answer: D. object

Explanation: In JavaScript, primitive data types include string, number, boolean, undefined, null, and symbol. object is not a primitive data type; it’s a complex data type.

Question 132 What will the following JavaScript code output?

console.log(’10’ – 5);

A. 5

B. “5”

C. “105”

D. NaN

Answer: A. 5

Explanation: In JavaScript, the – operator is used for subtraction and it converts strings to numbers if possible. Thus, ’10’ – 5 becomes 10 – 5, which equals 5.

Question 133 Which method converts a JavaScript object to a JSON string?

A. JSON.stringify()

B. JSON.parse()

C. Object.toString()

D. Object.toJSON()

Answer: A. JSON.stringify()

Explanation: JSON.stringify() method is used to convert a JavaScript object into a JSON string.

Question 134 How do you define a constant in JavaScript?

A. var CONSTANT_NAME = value;

B. constant CONSTANT_NAME = value;

C. let CONSTANT_NAME = value;

D. const CONSTANT_NAME = value;

Answer: D. const CONSTANT_NAME = value;

Explanation: The const keyword is used to define a constant in JavaScript. A constant is a value that cannot be altered once defined.

Question 135 What does the following code return?

isNaN(‘Hello’);

A. true

B. false

C. null

D. undefined

Answer: A. true

Explanation: isNaN(‘Hello’) returns true because ‘Hello’ is not a number.

Question 136 Which of these statements about JavaScript closures is correct?

A. Closures are functions with preserved data.

B. Closures are the same as global variables.

C. Closures are only created within loops.

D. Closures cannot access variables outside their scope.

Answer: A. Closures are functions with preserved data.

Explanation: A closure in JavaScript is a function that remembers its outer variables and can access them.

Question 137 What will the following JavaScript code output?

let x = “5”;

x = x + 1;

console.log(x);

A. 6

B. “6”

C. “51”

D. NaN

Answer: C. “51”

Explanation: In JavaScript, when you use the + operator with a string and a number, the number is converted to a string and the two strings are concatenated. Thus, “5” + 1 results in “51”.

Question 138 What is the correct syntax to create an array in JavaScript?

A. let arr = new Array(1, 2, 3);

B. let arr = array(1, 2, 3);

C. let arr = {1, 2, 3};

D. let arr = [1, 2, 3];

Answer: D. let arr = [1, 2, 3];

Explanation: In JavaScript, an array is defined using square brackets [] with elements separated by commas.

Question 139 What does the following JavaScript code output?

let a = true;

let b = false;

console.log(a && b);

A. true

B. false

C. null

D. undefined

Answer: B. false

Explanation: The logical AND (&&) operator returns true if both operands are true. Since b is false, a && b evaluates to false.

Question 140 How do you write a conditional statement in JavaScript to check if a variable a is equal to 10?

A. if a = 10 {…}

B. if (a == 10) {…}

C. if a == 10 {…}

D. if (a = 10) {…}

Answer: B. if (a == 10) {…}

Explanation: The correct syntax for an if statement in JavaScript includes parentheses around the condition and uses == or === to check for equality.

Question 141 What is the purpose of the Array.prototype.every() method in JavaScript?

A. To check if every element in an array passes a test implemented by the provided function.

B. To execute a provided function once for each array element.

C. To create a new array with the results of calling a provided function on every element in the calling array.

D. To return the first element in the array that satisfies the provided testing function.

Answer: A. To check if every element in an array passes a test implemented by the provided function.

Explanation: Array.prototype.every() tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Question 142 What will the following JavaScript code output?

console.log(typeof []);

A. “array”

B. “object”

C. “undefined”

D. “null”

Answer: B. “object”

Explanation: In JavaScript, arrays are technically a type of object. Therefore, typeof [] returns “object”.

Question 143 Which statement is used to handle exceptions in JavaScript?

A. try…catch

B. try…except

C. try…finally

D. try…error

Answer: A. try…catch

Explanation: The try…catch statement is used to catch exceptions in JavaScript. Optionally, a finally block can be used after the catch block.

Question 144 What is the correct way to define an async function in JavaScript?

A. async function myFunc() {…}

B. function async myFunc() {…}

C. function myFunc() async {…}

D. async: function myFunc() {…}

Answer: A. async function myFunc() {…}

Explanation: An asynchronous function in JavaScript is defined using the async keyword before the function keyword, like so: async function myFunc() {…}.

Question 145 What does the following JavaScript code output?

let x = “100”;

let y = “10”;

console.log(x / y);

A. “10”

B. 10

C. NaN

D. TypeError

Answer: B. 10

Explanation: JavaScript converts string operands to numbers in all numeric operations. Therefore, “100” / “10” results in 10.

Question 146 What is the default behavior of the this keyword in JavaScript functions?

A. It refers to the window/global object.

B. It refers to the document object.

C. It refers to the function itself.

D. It is undefined.

Answer: A. It refers to the window/global object.

Explanation: In the global execution context (outside of any function), this refers to the global object whether in strict mode or not. Inside a function, the value of this depends on how the function is called.

Question 147 How can you stop the propagation of an event in JavaScript?

A. event.stop()

B. event.preventDefault()

C. event.stopPropagation()

D. event.cancel()

Answer: C. event.stopPropagation()

Explanation: event.stopPropagation() is used to stop the propagation of an event, preventing it from being dispatched to any EventListeners after the current one.

Question 148 Which of the following is true about template literals in JavaScript?

A. They use single quotes.

B. They can contain placeholders.

C. They cannot span multiple lines.

D. They are used for writing regular expressions.

Answer: B. They can contain placeholders.

Explanation: Template literals are enclosed by backticks (`) and can contain placeholders indicated by the dollar sign and curly braces (${expression}).

Question 149 What will the following JavaScript code output?

let str = “JavaScript”;

console.log(str.substring(4));

A. “Script”

B. “Java”

C. “JavaScript”

D. “Scrip”

Answer: A. “Script”

Explanation: str.substring(4) extracts characters from the 4th index (inclusive) to the end of the string, resulting in “Script”.

Question 150 What is the result of the following expression in JavaScript?

“2” + 2 * “2”

A. “24”

B. 6

C. “222”

D. NaN

Answer: A. “24”

Explanation: In this expression, JavaScript first evaluates the multiplication (2 * “2” = 4) and then performs string concatenation (“2” + 4), resulting in “24”.

Question 151 What will the following JavaScript code output?

console.log(typeof (() => {}));

A. “function”

B. “object”

C. “undefined”

D. “string”

Answer: A. “function”

Explanation: The output will be “function”, as an arrow function is still a function, and thus typeof returns “function”.

Question 152 Which method is used to serialize an object into a JSON string in JavaScript?

A. JSON.stringify()

B. JSON.parse()

C. Object.serialize()

D. Object.toString()

Answer: A. JSON.stringify()

Explanation: JSON.stringify() method is used to convert a JavaScript object into a JSON string.

Question 153 What will the following code output?

let a = 3;

let b = new Number(3);

console.log(a == b);

console.log(a === b);

A. true, true

B. false, false

C. true, false

D. false, true

Answer: C. true, false

Explanation: a == b is true because == does type coercion and both a and b represent the same value. a === b is false because === also checks the type, and a is a number while b is an object.

Question 154 Which one of these is a way to define a variable in JavaScript that restricts its scope to the block in which it is defined?

A. var

B. let

C. const

D. Both B and C

Answer: D. Both B and C

Explanation: Both let and const are used to declare variables in JavaScript with block scope. The var keyword defines a variable globally or locally to an entire function regardless of block scope.

Question 155 What is the purpose of the map() method in JavaScript?

A. It calls a function once for each element in an array, in order.

B. It transforms the elements of an array using a callback function and returns a new array.

C. It checks if any elements in an array pass a test provided by a function.

D. It combines all elements of an array into a single string.

Answer: B. It transforms the elements of an array using a callback function and returns a new array.

Explanation: map() creates a new array with the results of calling a provided function on every element in the calling array.

Question 156 What will be logged to the console in the following code?

let x = “Hello”;

let y = x;

x = “World”;

console.log(y);

A. Hello

B. World

C. undefined

D. TypeError

Answer: A. Hello

Explanation: Strings in JavaScript are immutable and passed by value. Changing x to “World” doesn’t affect the value of y, which remains “Hello”.

Question 157 Which method is used to combine two or more arrays in JavaScript?

A. Array.join()

B. Array.concat()

C. Array.push()

D. Array.append()

Answer: B. Array.concat()

Explanation: Array.concat() is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array.

Question 158 How can you add a comment in a JavaScript file?

A. <!– This is a comment –>

B. // This is a comment

C. /* This is a comment */

D. Both B and C

Answer: D. Both B and C

Explanation: In JavaScript, // is used for single-line comments and /* */ for multi-line comments.

Question 159 What is the output of the following JavaScript code?

console.log(’10’ – 5);

A. 5

B. “5”

C. “105”

D. NaN

Answer: A. 5

Explanation: In JavaScript, the – operator converts the operands to numbers (if they are not already), so ’10’ – 5 is treated as 10 – 5, which equals 5.

Question 160 Which of these is a JavaScript package manager?

A. npm

B. CSS

C. HTML

D. FTP

Answer: A. npm

Explanation: npm stands for Node Package Manager. It is the default package manager for the JavaScript runtime environment Node.js.

Question 161 What is the output of the following code snippet?

let a = “Hello”;

let b = a.substring(1, 4);

console.log(b);

A. “Hell”

B. “ell”

C. “ello”

D. “Hel”

Answer: B. “ell”

Explanation: substring(1, 4) extracts characters from index 1 to index 4 (excluding index 4) of the string a. So, it outputs “ell”.

Question 162 Which of these is a correct method to create a deep copy of an object in JavaScript?

A. let newObj = Object.assign({}, obj);

B. let newObj = {…obj};

C. let newObj = JSON.parse(JSON.stringify(obj));

D. let newObj = obj.slice();

Answer: C. let newObj = JSON.parse(JSON.stringify(obj));

Explanation: JSON.parse(JSON.stringify(obj)) creates a deep copy of the object obj. Object.assign() and spread syntax {…obj} only create shallow copies.

Question 163 What will the following JavaScript code output?

console.log(!!”false”);

A. true

B. false

C. null

D. undefined

Answer: A. true

Explanation: The double negation !! will convert a value to its boolean equivalent. Since “false” is a non-empty string, it’s truthy, and thus !!”false” returns true.

Question 164 Which method can be used to round a number to the nearest integer in JavaScript?

A. Math.round()

B. Math.floor()

C. Math.ceil()

D. Math.absolute()

Answer: A. Math.round()

Explanation: Math.round() rounds a number to the nearest integer.

Question 165 What does the following JavaScript code return?

typeof new Boolean(false)

A. “boolean”

B. “object”

C. “false”

D. “undefined”

Answer: B. “object”

Explanation: new Boolean(false) creates a Boolean object wrapper for the value false, so typeof returns “object”.

Question 166 What is the default value of this in a JavaScript function?

A. The global object

B. null

C. The function itself

D. undefined

Answer: A. The global object

Explanation: In JavaScript, the default value of this in a function is the global object (e.g., window in a web browser). In strict mode, it’s undefined.

Question 167 How can you convert a string to an integer in JavaScript?

A. int.parse(string)

B. Number(string)

C. parseInt(string)

D. Both B and C

Answer: D. Both B and C

Explanation: Both Number(string) and parseInt(string) can be used to convert a string to an integer in JavaScript.

Question 168 What is the output of this JavaScript code?

let num = 5;

console.log(num++);

A. 5

B. 6

C. TypeError

D. undefined

Answer: A. 5

Explanation: The num++ is a post-increment operator, which returns the current value of num (5) before incrementing it.

Question 169 Which of the following is not a valid way to declare a function in JavaScript?

A. function myFunc() {…}

B. const myFunc = function() {…};

C. const myFunc = () => {…};

D. function = myFunc() {…};

Answer: D. function = myFunc() {…};

Explanation: The syntax function = myFunc() {…}; is not a valid way to declare a function. The other options are valid function declarations.

Question 170 What is the correct syntax for adding comments in JavaScript?

A. <!– This is a comment –>

B. // This is a comment

C. /* This is a comment */

D. Both B and C

Answer: D. Both B and C

Explanation: In JavaScript, // is used for single-line comments and /* */ for multi-line comments.

Question 171 What is the result of “1” + 2 + 3 in JavaScript?

A. 6

B. “123”

C. “6”

D. undefined

Answer: B. “123”

Explanation: In this expression, JavaScript performs string concatenation. Starting from the left, “1” (a string) concatenated with 2 results in “12”, which is then concatenated with 3, resulting in “123”.

Question 172 Which statement about null in JavaScript is true?

A. null is the same as undefined.

B. null is a type of object.

C. null is a type of boolean.

D. null indicates the absence of a value.

Answer: D. null indicates the absence of a value.

Explanation: null in JavaScript represents the intentional absence of any object value. It is not the same as undefined, which represents a value not yet assigned.

Question 173 How do you find the length of a string str in JavaScript?

A. str.length()

B. length(str)

C. str.length

D. str.getSize()

Answer: C. str.length

Explanation: In JavaScript, the length of a string can be found using the .length property. str.length returns the length of the string str.

Question 174 What does the following JavaScript code output?

let x = 10;

console.log(++x);

A. 10

B. 11

C. NaN

D. undefined

Answer: B. 11

Explanation: The ++x is a pre-increment operator, which increments the value of x before returning it. Hence, it outputs 11.

Question 175 In JavaScript, which of these methods is used to remove the last element from an array?

A. pop()

B. push()

C. shift()

D. unshift()

Answer: A. pop()

Explanation: The pop() method removes the last element from an array and returns that element.

Question 176 What does the || operator represent in JavaScript?

A. AND

B. OR

C. NOT

D. XOR

Answer: B. OR

Explanation: The || operator represents the logical OR in JavaScript. It returns true if either of its operands is true.

Question 177 Which of the following is not a reserved word in JavaScript?

A. interface

B. private

C. throws

D. short

Answer: B. private

Explanation: private is not a reserved word in JavaScript, though it is reserved in some other languages.

Question 178 How can you check if a variable a is less than 10 in JavaScript?

A. if a < 10 {…}

B. if (a < 10) {…}

C. if a lt 10 {…}

D. if (a < 10); {…}

Answer: B. if (a < 10) {…}

Explanation: The correct syntax to check if a variable is less than a certain number in JavaScript is if (condition) {…}.

Question 179 What is console.log(typeof null); going to output in JavaScript?

A. “null”

B. “undefined”

C. “object”

D. “number”

Answer: C. “object”

Explanation: In JavaScript, typeof null is “object”, which is a known quirk of the language. null is not actually an object, but this behavior is a holdover from the early days of JavaScript.

Question 180 Which method is used to join elements of an array into a string in JavaScript?

A. concat()

B. join()

C. merge()

D. collect()

Answer: B. join()

Explanation: The join() method joins all elements of an array into a string. You can optionally specify a separator; if no separator is specified, a comma is used by default.

Question 181 What will this JavaScript code output?

console.log(“1” == 1);

A. true

B. false

C. TypeError

D. undefined

Answer: A. true

Explanation: In JavaScript, the == operator performs type coercion and checks for value equality. Thus, the string “1” is coerced into the number 1, resulting in true.

Question 182 What is the purpose of the bind method in JavaScript?

A. It is used to bind an existing function to a new object.

B. It is used to permanently link a function to a particular value of this.

C. It is used to combine two functions into one.

D. It is used to delay the execution of a function.

Answer: B. It is used to permanently link a function to a particular value of this.

Explanation: The bind method creates a new function that, when called, has its this keyword set to the provided value. It is used to set the this context of a function regardless of how it is called.

Question 183 What is the primary use of the async keyword before a function in JavaScript?

A. It indicates that the function returns a promise.

B. It automatically splits the function into multiple threads.

C. It guarantees that the function will execute asynchronously.

D. It optimizes the function for performance.

Answer: A. It indicates that the function returns a promise.

Explanation: The async keyword is used to declare an asynchronous function, which returns a promise. It allows the use of await within the function.

Question 184 In JavaScript, which method is used to sort the elements of an array in place?

A. Array.sort()

B. Array.order()

C. Array.arrange()

D. Array.sequence()

Answer: A. Array.sort()

Explanation: Array.sort() is a method that sorts the elements of an array in place and returns the sorted array.

Question 185 What is the output of the following JavaScript code?

let arr = [10, 12, 15, 21];

for (let i = 0; i < arr.length; i++) {

 setTimeout(function() {

 console.log(‘Index: ‘ + i + ‘, element: ‘ + arr[i]);

 }, 3000);

}

A. It logs the index and element of each array element after a delay.

B. It throws a syntax error.

C. It logs undefined for each element.

D. It logs the same index and element multiple times.

Answer: A. It logs the index and element of each array element after a delay.

Explanation: The setTimeout function creates a closure for each loop iteration, capturing the current value of i and the corresponding array element. After 3 seconds, it logs the index and element of each item in the array.

Question 186 How can you add a key-value pair to a JavaScript object after it has been created?

A. obj[key] = value;

B. obj.add(key, value);

C. obj.push({key: value});

D. obj.concat({key: value});

Answer: A. obj[key] = value;

Explanation: You can add a new key-value pair to an object by using the syntax obj[key] = value; where key is the name of the property and value is the value you want to assign.

Question 187 What is event delegation in JavaScript?

A. A way to handle events on parent elements rather than individual child elements.

B. A method to delegate event handling to a different element than the one that received the event.

C. A technique to speed up event handling.

D. A process to bind multiple events to a single event listener.

Answer: A. A way to handle events on parent elements rather than individual child elements.

Explanation: Event delegation is a technique where you use a single event listener on a parent element to manage all of its child events, taking advantage of event bubbling.

Question 188 Which of the following is a way to define an arrow function in JavaScript?

A. function myFunc() {…}

B. var myFunc = () => {…};

C. var myFunc = function() {…};

D. var myFunc => {…};

Answer: B. var myFunc = () => {…};

Explanation: Arrow functions in JavaScript are defined using the => syntax. They are a shorter way to write function expressions and do not have their own this value.

Question 189 What will be the output of the following JavaScript expression?

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

A. true

B. false

C. TypeError

D. undefined

Answer: B. false

Explanation: Due to the way JavaScript handles floating-point arithmetic, the result of 0.1 + 0.2 is not exactly 0.3. Therefore, this comparison returns false.

Question 190 In JavaScript, what is the use of the splice method?

A. To join two arrays together.

B. To search for a specified value within an array.

C. To change the contents of an array by removing or replacing existing elements and/or adding new elements.

D. To split a string into an array of substrings.

Answer: C. To change the contents of an array by removing or replacing existing elements and/or adding new elements.

Explanation: The splice method in JavaScript is used to change the contents of an array by removing, replacing, and/or adding new elements in place.

Question 191 What will the following JavaScript code output?

console.log(typeof undefined);

A. “undefined”

B. “object”

C. “null”

D. “string”

Answer: A. “undefined”

Explanation: The typeof operator when used with undefined will return the string “undefined”.

Question 192 What does the === operator check in JavaScript?

A. Only the value equality

B. Only the type equality

C. Both value and type equality

D. The existence of variables

Answer: C. Both value and type equality

Explanation: The === operator, also known as the strict equality operator, checks both the value and the type of the operands, making sure they are identical.

Question 193 How can a for loop be stopped in JavaScript?

A. stop();

B. break;

C. exit;

D. terminate;

Answer: B. break;

Explanation: The break statement is used to exit from a loop before it has looped through all its items.

Question 194 Which of the following is a way to define a constant in JavaScript?

A. var CONSTANT_NAME = value;

B. let CONSTANT_NAME = value;

C. const CONSTANT_NAME = value;

D. #define CONSTANT_NAME value

Answer: C. const CONSTANT_NAME = value;

Explanation: The const keyword is used to define a constant in JavaScript. A constant cannot be reassigned after its declaration.

Question 195 What is the output of the following code?

let x = 10;

let y = “10”;

console.log(x === y);

A. true

B. false

C. TypeError

D. undefined

Answer: B. false

Explanation: The === operator checks for both value and type equality. In this case, x is a number and y is a string, so the comparison returns false.

Question 196 Which method is used to add new items to the beginning of an array in JavaScript?

A. push()

B. pop()

C. unshift()

D. shift()

Answer: C. unshift()

Explanation: The unshift() method is used to add one or more elements to the beginning of an array and returns the new length of the array.

Question 197 What is the purpose of the Array.prototype.reduce() method in JavaScript?

A. To execute a reducer function on each element of the array, resulting in a single output value.

B. To combine all elements of an array into a single string.

C. To iterate over each element of an array.

D. To modify each element in the array and create a new array.

Answer: A. To execute a reducer function on each element of the array, resulting in a single output value.

Explanation: Array.prototype.reduce() applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

Question 198 Which of the following will correctly call a function named myFunction?

A. call myFunction();

B. myFunction();

C. call function myFunction();

D. execute myFunction();

Answer: B. myFunction();

Explanation: In JavaScript, you call a function by writing the function’s name followed by parentheses. So, myFunction(); is the correct syntax.

Question 199 What will the following JavaScript code output?

let num = “10”;

console.log(typeof parseInt(num));

A. “number”

B. “string”

C. “object”

D. “undefined”

Answer: A. “number”

Explanation: parseInt(num) converts the string “10” to the number 10. The typeof operator then returns “number”.

Question 200 In JavaScript, how is a while loop started?

A. while (condition) {…}

B. while condition {…}

C. while loop (condition) {…}

D. while: (condition) {…}

Answer: A. while (condition) {…}

Explanation: A while loop in JavaScript is written with the syntax while (condition) {…}, where the code inside the braces is executed as long as the condition evaluates to true.

These questions cover a variety of JavaScript topics, testing fundamental knowledge as well as more complex concepts.

Question 201 What is the output of the following JavaScript code?

let a = “5”;

let b = 5;

console.log(a === b);

A. true

B. false

C. TypeError

D. undefined

Answer: B. false

Explanation: The strict equality operator (===) checks both the value and the type of the operands. In this case, a is a string and b is a number, so the comparison returns false.

Question 202 Which of the following methods is used to remove an element from the end of an array in JavaScript?

A. shift()

B. pop()

C. push()

D. unshift()

Answer: B. pop()

Explanation: The pop() method removes the last element from an array and returns that element. This alters the length of the array.

Question 203 How do you declare a JavaScript variable that cannot be reassigned?

A. var variableName = value;

B. let variableName = value;

C. const variableName = value;

D. immutable variableName = value;

Answer: C. const variableName = value;

Explanation: The const keyword is used to declare a JavaScript variable that cannot be reassigned after it’s been declared.

Question 204 In JavaScript, what will the following expression return?

typeof NaN

A. “number”

B. “NaN”

C. “undefined”

D. “object”

Answer: A. “number”

Explanation: NaN (Not a Number) in JavaScript is considered a numeric value, and thus typeof NaN returns “number”.

Question 205 What is the purpose of the Array.prototype.map() method in JavaScript?

A. To execute a function on each element in the array and return a new array.

B. To combine all elements of an array into a single value.

C. To filter elements based on a condition and return a new array.

D. To check if at least one element in the array passes a test.

Answer: A. To execute a function on each element in the array and return a new array.

Explanation: Array.prototype.map() creates a new array with the results of calling a provided function on every element in the calling array.

Question 206 What does the following JavaScript code output?

console.log(“Hello” || “World”);

console.log(“Foo” && “Bar”);

A. “Hello” and “Foo”

B. “World” and “Bar”

C. “Hello” and “Bar”

D. “World” and “Foo”

Answer: C. “Hello” and “Bar”

Explanation: The logical OR (||) operator returns the first truthy operand, which is “Hello”. The logical AND (&&) operator returns the second operand if the first is truthy, which in this case is “Bar”.

Question 207 Which of the following is a correct syntax for creating a new object in JavaScript?

A. let obj = new Object();

B. let obj = {};

C. let obj = Object.create(null);

D. All of the above

Answer: D. All of the above

Explanation: All three methods are correct ways to create a new object in JavaScript. new Object() and {} (object literal syntax) are more common, whereas Object.create(null) is used for more complex cases, like creating an object without a prototype.

Question 208 What is the correct way to write a JavaScript array?

A. let arr = new Array(“item1”, “item2”, “item3”);

B. let arr = [“item1”, “item2”, “item3”];

C. let arr = Array(“item1”, “item2”, “item3”);

D. All of the above

Answer: D. All of the above

Explanation: All three statements are valid ways to create an array in JavaScript. new Array() and Array() are constructor syntaxes, while [] is array literal syntax.

Question 209 In JavaScript, how can you catch errors thrown during asynchronous operations?

A. Using a try…catch block.

B. Using a catch() method on a promise.

C. Using an onError event handler.

D. Both A and B

Answer: D. Both A and B

Explanation: For asynchronous operations, errors can be caught using catch() method on a promise. Additionally, with the async/await syntax

Question 210 What will the following JavaScript code output?

console.log(1 + “1”);

A. 11

B. 2

C. “11”

D. undefined

Answer: C. “11”

Explanation: When you use the + operator with a string and a number, JavaScript performs type coercion and converts the number to a string before concatenating them.

Question 211 Which JavaScript method is used to join the elements of an array into a string with a specified separator?

A. join()

B. concat()

C. split()

D. merge()

Answer: A. join()

Explanation: The join() method in JavaScript is used to join the elements of an array into a string with a specified separator.

Question 212 What is the purpose of the Array.prototype.filter() method in JavaScript?

A. To execute a function on each element in the array and return a new array.

B. To combine all elements of an array into a single value.

C. To filter elements based on a condition and return a new array.

D. To check if at least one element in the array passes a test.

Answer: C. To filter elements based on a condition and return a new array.

Explanation: Array.prototype.filter() creates a new array with all elements that pass the test implemented by the provided function.

Question 213 In JavaScript, how do you declare a multi-line string?

A. Use backticks (`) and template literals.

B. Use double quotes (“) with line breaks.

C. Use single quotes (‘) with line breaks.

D. JavaScript does not support multi-line strings.

Answer: A. Use backticks (`) and template literals.

Explanation: In JavaScript, you can declare multi-line strings using backticks (`) and template literals. This allows you to include line breaks within the string.

Question 214 What is the purpose of the Array.prototype.forEach() method in JavaScript?

A. To execute a function on each element in the array and return a new array.

B. To combine all elements of an array into a single value.

C. To iterate over each element of an array and apply a function.

D. To check if at least one element in the array passes a test.

Answer: C. To iterate over each element of an array and apply a function.

Explanation: Array.prototype.forEach() is used to iterate over each element of an array and apply a function to each element without creating a new array.

Question 215 What will the following JavaScript code output?

console.log(3 === 3 && 4 !== 5);

A. true

B. false

C. TypeError

D. undefined

Answer: A. true

Explanation: The && operator performs a logical AND operation. Both conditions 3 === 3 (true) and 4 !== 5 (true) are satisfied, so the result is true.

Question 216 In JavaScript, which of the following statements is used to skip the current iteration of a loop and continue with the next one?

A. break

B. continue

C. return

D. exit

Answer: B. continue

Explanation: The continue statement is used to skip the current iteration of a loop and continue with the next iteration.

Question 217 What is the purpose of the Array.prototype.indexOf() method in JavaScript?

A. To find the index of the first occurrence of a value in an array.

B. To sort the elements of an array.

C. To add an element to the end of an array.

D. To remove the last element from an array.

Answer: A. To find the index of the first occurrence of a value in an array.

Explanation: Array.prototype.indexOf() is used to find the index of the first occurrence of a specified value in an array.

Question 218 What is the output of the following JavaScript code?

let x = 5;

function myFunction() {

 let x = 10;

 console.log(x);

}

myFunction();

console.log(x);

A. 5 and 5

B. 10 and 5

C. 5 and 10

D. 10 and 10

Answer: B. 10 and 5

Explanation: Inside the myFunction function, there is a local variable x with a value of 10, which is different from the global variable x with a value of 5. So, the first console.log outputs 10, and the second console.log outputs 5.

Question 219 What does the isNaN() function in JavaScript do?

A. It checks if a value is not a string.

B. It checks if a value is not a number.

C. It checks if a value is not an array.

D. It checks if a value is not an object.

Answer: B. It checks if a value is not a number.

Explanation: The isNaN() function checks if a value is not a number and returns true if the value is not a number, false if it is a number.

Question 220 What is the result of the following JavaScript code?

console.log(2 + 2 + “2”);

A. 22

B. 42

C. 6

D. TypeError

Answer: A. 22

Explanation: JavaScript performs the addition from left to right, so 2 + 2 equals 4, and then “2” is concatenated to it, resulting in “22”.

Question 221 Which keyword is used to declare a function in JavaScript?

A. function

B. def

C. func

D. method

Answer: A. function

Explanation: The function keyword is used to declare a function in JavaScript.

Question 222 What will the following JavaScript code output?

console.log(“5” – 3);

A. 2

B. 8

C. “2”

D. TypeError

Answer: A. 2

Explanation: JavaScript performs type coercion when using the – operator, so the string “5” is converted to a number, resulting in 5 – 3, which equals 2.

Question 223 In JavaScript, which of the following is used to create a new object with the same prototype as an existing object?

A. Object.assign()

B. Object.create()

C. Object.copy()

D. Object.clone()

Answer: B. Object.create()

Explanation: The Object.create() method is used to create a new object with the specified prototype object.

Question 224 What is the purpose of the Array.prototype.reduce() method in JavaScript?

A. To execute a reducer function on each element of the array, resulting in a single output value.

B. To combine all elements of an array into a single string.

C. To iterate over each element of an array.

D. To filter elements based on a condition and return a new array.

Answer: A. To execute a reducer function on each element of the array, resulting in a single output value.

Explanation: Array.prototype.reduce() applies a function against an accumulator and each element in the array to reduce it to a single value.

Question 225 Which of the following statements is true about JavaScript variables declared with var inside a function?

A. They have block scope.

B. They are hoisted to the top of the function.

C. They cannot be reassigned.

D. They are block-scoped and not hoisted.

Answer: B. They are hoisted to the top of the function.

Explanation: Variables declared with var are hoisted to the top of their containing function, which means their declarations are processed before the code is executed.

Question 226 What is the output of the following JavaScript code?

console.log(10 / 0);

A. 10

B. 0

C. Infinity

D. NaN

Answer: C. Infinity

Explanation: Division by zero in JavaScript results in Infinity.

Question 227 In JavaScript, which loop is used for iterating over the properties of an object?

A. for…in loop

B. for loop

C. while loop

D. do…while loop

Answer: A. for…in loop

Explanation: The for…in loop is used for iterating over the properties of an object.

Question 228 What is the purpose of the Array.prototype.includes() method in JavaScript?

A. To check if an element is included in an array.

B. To add an element to the beginning of an array.

C. To remove an element from an array.

D. To check if an array is empty.

Answer: A. To check if an element is included in an array.

Explanation: Array.prototype.includes() is used to check if a specified element is present in an array and returns true or false.

Question 229 What will the following JavaScript code output?

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

A. true

B. false

C. TypeError

D. undefined

Answer: B. false

Explanation: Due to floating-point precision in JavaScript, 0.1 + 0.2 is not exactly equal to 0.3, so the comparison returns false.

Question 230 What is the result of the following JavaScript code?

console.log(“5” + 3);

A. 53

B. 8

C. “53”

D. TypeError

Answer: A. 53

Explanation: When you use the + operator with a string and a number, JavaScript performs concatenation, so “5” is concatenated with 3, resulting in “53”.

Question 231 Which of the following is a correct way to declare a JavaScript function?

A. function myFunction() { … }

B. let myFunction() { … }

C. const myFunction() { … }

D. var myFunction() { … }

Answer: A. function myFunction() { … }

Explanation: The correct way to declare a JavaScript function is using the function keyword followed by the function name.

Question 232 What will the following JavaScript code output?

console.log(typeof “Hello”);

A. “string”

B. “object”

C. “null”

D. “undefined”

Answer: A. “string”

Explanation: The typeof operator when used with a string will return the string “string”.

Question 233 In JavaScript, which loop is used to execute a block of code at least once, and then repeatedly as long as a condition is true?

A. for loop

B. while loop

C. do…while loop

D. if statement

Answer: C. do…while loop

Explanation: The do…while loop is used to execute a block of code at least once, and then repeatedly as long as a condition is true.

Question 234 What is the purpose of the Array.prototype.concat() method in JavaScript?

A. To execute a function on each element in the array and return a new array.

B. To combine all elements of an array into a single string.

C. To merge two or more arrays and return a new array.

D. To check if an array is empty.

Answer: C. To merge two or more arrays and return a new array.

Explanation: Array.prototype.concat() is used to merge two or more arrays into a new array.

Question 235 What does the Math.random() function in JavaScript return?

A. A random integer between 0 and 1.

B. A random integer between 0 and 100.

C. A random floating-point number between 0 (inclusive) and 1 (exclusive).

D. A random floating-point number between 0 (inclusive) and 100 (exclusive).

Answer: C. A random floating-point number between 0 (inclusive) and 1 (exclusive).

Explanation: Math.random() returns a random floating-point number between 0 (inclusive) and 1 (exclusive).

Question 236 In JavaScript, which operator is used to test if a specific property exists in an object?

A. in

B. exists

C. has

D. check

Answer: A. in

Explanation: The in operator is used to test if a specific property exists in an object.

Question 237 What is the output of the following JavaScript code?

let x = 10;

let y = 5;

console.log(x > y ? “x is greater” : “y is greater”);

A. “x is greater”

B. “y is greater”

C. “x > y ? “x is greater” : “y is greater”

D. TypeError

Answer: A. “x is greater”

Explanation: This is a ternary conditional expression. It checks if x is greater than y and, if true, returns “x is greater”, otherwise, it returns “y is greater”.

Question 238 What is the purpose of the Array.prototype.slice() method in JavaScript?

A. To execute a function on each element in the array and return a new array.

B. To remove the last element from an array.

C. To create a shallow copy of an array containing a portion of the original array.

D. To check if an array is empty.

Answer: C. To create a shallow copy of an array containing a portion of the original array.

Explanation: Array.prototype.slice() is used to create a shallow copy of an array containing a portion of the original array.

Question 239 What will the following JavaScript code output?

let x = 5;

let y = “5”;

console.log(x == y);

A. true

B. false

C. TypeError

D. undefined

Answer: A. true

Explanation: The == operator performs type coercion, so it converts the string “5” to a number and then checks for equality, which is true.

Question 240 What will the following JavaScript code output?

console.log(3 * “2”);

A. 6

B. “6”

C. 32

D. TypeError

Answer: A. 6

Explanation: JavaScript performs type coercion, so the string “2” is converted to a number, and then the multiplication 3 * 2 results in 6.

Question 241 Which of the following is the correct way to comment out a single line of JavaScript code?

A. /* This is a comment */

B. <!– This is a comment –>

C. # This is a comment

D. // This is a comment

Answer: D. // This is a comment

Explanation: In JavaScript, you can use // to comment out a single line of code.

Question 242 In JavaScript, what is the purpose of the break statement?

A. To declare a variable.

B. To exit a loop or switch statement prematurely.

C. To add an element to an array.

D. To remove an element from an array.

Answer: B. To exit a loop or switch statement prematurely.

Explanation: The break statement is used to exit a loop or switch statement prematurely.

Question 243 What will the following JavaScript code output?

let str = “Hello, World!”;

console.log(str.length);

A. 5

B. 7

C. 12

D. 13

Answer: D. 13

Explanation: The str.length property returns the length of the string, which is 13 characters in this case.

Question 244 Which of the following statements is true about JavaScript’s null value?

A. null is a number.

B. null represents an empty string.

C. null represents the absence of a value or an empty object.

D. null is the same as undefined.

Answer: C. null represents the absence of a value or an empty object.

Explanation: null in JavaScript represents the absence of a value or an empty object. It is not a number or an empty string.

Question 245 What is the purpose of the Array.prototype.unshift() method in JavaScript?

A. To execute a function on each element in the array and return a new array.

B. To add a new element to the end of an array.

C. To add one or more elements to the beginning of an array and return the new length.

D. To remove the first element from an array.

Answer: C. To add one or more elements to the beginning of an array and return the new length.

Explanation: Array.prototype.unshift() adds one or more elements to the beginning of an array and returns the new length of the array.

Question 246 What is the output of the following JavaScript code?

console.log(“Hello” + 5 + 3);

A. Hello8

B. Hello53

C. Hello15

D. TypeError

Answer: B. Hello53

Explanation: JavaScript performs concatenation from left to right, so “Hello” + 5 results in “Hello5”, and then “Hello5” + 3 results in “Hello53”.

Question 247 In JavaScript, how can you check if a variable is an array?

A. Using the typeof operator.

B. Using the isArray() method.

C. Using the isArray property.

D. Using the array keyword.

Answer: B. Using the isArray() method.

Explanation: You can use the Array.isArray() method to check if a variable is an array in JavaScript.

Question 248 What will the following JavaScript code output?

let x = 5;

let y = 10;

console.log(x > y ? “x is greater” : “y is greater”);

A. “x is greater”

B. “y is greater”

C. “x > y ? “x is greater” : “y is greater”

D. TypeError

Answer: B. “y is greater”

Explanation: This is a ternary conditional expression. It checks if x is greater than y, which is false, so it returns “y is greater”.

Question 249 What is the purpose of the Array.prototype.reverse() method in JavaScript?

A. To execute a function on each element in the array and return a new array.

B. To reverse the order of elements in an array in place.

C. To sort the elements of an array.

D. To check if an array is empty.

Answer: B. To reverse the order of elements in an array in place.

Explanation: Array.prototype.reverse() is used to reverse the order of elements in an array in place, without creating a new array.

Question 250 What is the purpose of the Array.prototype.every() method in JavaScript?

A. To execute a function on each element in the array and return a new array.

B. To combine all elements of an array into a single value.

C. To check if at least one element in the array passes a test.

D. To check if all elements in the array pass a test.

Answer: D. To check if all elements in the array pass a test.

Explanation: Array.prototype.every() is used to check if all elements in an array pass a test implemented by a provided function. It returns true if all elements pass the test, otherwise, it returns false.