👨💻 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.
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.
B.
C.
D.
Answer: C.
Explanation: The src attribute in the