JavaScript Beginner Quiz
30 questions on JavaScript Basics.
Question 1: Which keyword declares a block-scoped variable in JavaScript?
- A. var
- B. define
- C. let β (correct answer)
- D. dim
Explanation: let (and const) create block-scoped variables in modern JavaScript (ES6+). var is function-scoped.
Question 2: What will the following code log to the console?
``javascript
console.log(typeof null);
``
- A. "null"
- B. "undefined"
- C. "object" β (correct answer)
- D. "number"
Explanation: In JavaScript, typeof null is historically evaluated as "object". This is considered a long-standing bug in the language design but cannot be changed to prevent breaking existing websites.
Question 3: Which method is used to select an element by its ID from the HTML DOM?
- A. document.getElementByClass()
- B. document.selectId()
- C. document.getElementById() β (correct answer)
- D. document.querySelectorId()
Explanation: The document.getElementById("myId") method searches the DOM tree for a unique element matching the specified ID.
Question 4: What will be the output of the following JavaScript expression?
``javascript
console.log(2 + "2");
``
- A. 4
- B. "22" β (correct answer)
- C. NaN
- D. TypeError
Explanation: When adding a number and a string, JavaScript performs **coercion** (type conversion). It converts the number to a string and concatenates them, resulting in "22".
Question 5: Which symbol is used to define single-line comments in JavaScript?
- A. #
- B. // β (correct answer)
- C. /*
- D. <!--
Explanation: Double slashes // start single-line comments in JavaScript. Block comments are enclosed in /* ... */.
Question 6: How do you declare a constant variable whose value cannot be reassigned?
- A. const β (correct answer)
- B. let
- C. var
- D. fixed
Explanation: The const keyword declares read-only constant variables that must be assigned at declaration and cannot be reassigned.
Question 7: What will console.log(2 === "2"); print to the console?
- A. true
- B. false β (correct answer)
- C. undefined
- D. TypeError
Explanation: The strict equality operator === checks both value and type without coercion. Since 2 is a number and "2" is a string, it returns false. Using == would return true.
Question 8: Which of the following represents an array in JavaScript?
- A.
let arr = {};
- B.
let arr = []; β (correct answer)
- C.
let arr = ();
- D.
let arr = <>;
Explanation: Square brackets [] are used to define arrays (ordered lists). Curly braces {} define object literals (key-value collections).
Question 9: Which array method adds one or more elements to the end of an array?
- A. pop()
- B. shift()
- C. push() β (correct answer)
- D. unshift()
Explanation: push() appends elements to the end of an array. pop() removes from the end. unshift() adds to the front. shift() removes from the front.
Question 10: How do you create a standard arrow function in ES6?
- A.
let func = () => { }; β (correct answer)
- B.
let func = function() { };
- C.
let func = -> { };
- D.
let func = => ( ) { };
Explanation: Arrow functions use parameters enclosed in parentheses followed by the fat arrow operator => and function body block.
Question 11: Which built-in object represents an absolute JSON text parser in JavaScript?
- A. XML
- B. JSON β (correct answer)
- C. Parse
- D. String
Explanation: The global JSON object contains JSON.parse() to convert JSON strings to JavaScript objects, and JSON.stringify() to convert objects to JSON strings.
Question 12: What is the output of the following statement?
``javascript
console.log(isNaN("Hello"));
``
- A. true β (correct answer)
- B. false
- C. NaN
- D. TypeError
Explanation: isNaN() stands for "is Not a Number". Since "Hello" cannot be parsed into a valid number, the check is true.
Question 13: Which event listener is triggered when a user clicks on an HTML button?
- A. onhover
- B. onclick β (correct answer)
- C. onchange
- D. onsubmit
Explanation: The click event (or onclick attribute) is triggered when an element is clicked.
Question 14: What is the default return value of a function that does not return anything explicitly?
- A. null
- B. undefined β (correct answer)
- C. false
- D. NaN
Explanation: If a function does not contain an explicit return statement, it returns the value undefined by default.
Question 15: What will console.log(typeof NaN); output?
- A. "NaN"
- B. "undefined"
- C. "number" β (correct answer)
- D. "object"
Explanation: NaN stands for "Not a Number", but technically its datatype in the JavaScript runtime is a floating-point numeric representation ("number").
Question 16: What is the difference between == and ===?
- A.
== performs type coercion; === checks both value and type strictly β (correct answer)
- B. They are identical
- C.
=== is slower
- D.
== checks references; === checks structures
Explanation: == converts operands to matching types before comparing (coercion). === requires operands to have both matching values and matching datatypes.
Question 17: What will the following code print?
``javascript
const obj = { name: "Alice" };
obj.name = "Bob";
console.log(obj.name);
``
- A. Alice
- B. Bob β (correct answer)
- C. TypeError
- D. undefined
Explanation: Declaring an object with const prevents the variable obj from being reassigned to a different reference. However, the properties of the object itself remain completely mutable.
Question 18: What is a closure in JavaScript?
- A. A function that has access to outer scope variables even after outer function has closed β (correct answer)
- B. A loop terminator
- C. A class destructor
- D. A CSS selector method
Explanation: Closures are created every time a function is created. They allow nested functions to access scopes of their parent functions, preserving execution state.
Question 19: Which method creates a new array with all elements that pass a test implemented by the provided function?
- A. map()
- B. filter() β (correct answer)
- C. reduce()
- D. every()
Explanation: filter() executes a callback function for each element, returning a new array of only elements for which the callback returns true.
Question 20: What will the following code log?
``javascript
const arr = [1, 2, 3];
const double = arr.map(x => x * 2);
console.log(double);
``
- A. [1, 2, 3]
- B. [2, 4, 6] β (correct answer)
- C. 6
- D. undefined
Explanation: map() iterates over each element in arr, executes the callback (x => x * 2), and returns a new array with the transformed values.
Question 21: What is the event loop in JavaScript?
- A. A loop that iterates over array elements
- B. The mechanism that handles asynchronous callbacks (like promises, setTimeout) in a single-threaded runtime β (correct answer)
- C. A database indexing strategy
- D. A CSS layout animation cycle
Explanation: JavaScript is single-threaded. The event loop monitors the call stack and callback queue, executing asynchronous tasks when the stack becomes empty.
Question 22: What will console.log([] == ![]); evaluate to in JavaScript?
- A. true β (correct answer)
- B. false
- C. TypeError
- D. NaN
Explanation: This is a famous JavaScript coercion edge-case. ![] coerces to boolean false. An empty array [] is coerced to numeric 0. false is also coerced to 0. Since 0 == 0, it returns true.
Question 23: Which keyword is used to handle asynchronous code in a synchronized-looking fashion inside modern ES2017+ functions?
- A. async/await β (correct answer)
- B. try/catch
- C. then/catch
- D. defer/load
Explanation: async and await are syntax wrappers around Promises, letting programmers write asynchronous code that reads sequentially like synchronous code.
Question 24: What is the output?
``javascript
let count = 0;
console.log(count++ + ++count);
``
- A. 0
- B. 2 β (correct answer)
- C. 1
- D. 3
Explanation: count++ returns 0 and then increments count to 1. ++count increments count from 1 to 2 and returns 2. Therefore, 0 + 2 = 2.
Question 25: Which function is used to schedule a one-time execution of code after a specified delay in milliseconds?
- A. setInterval()
- B. setTimeout() β (correct answer)
- C. delay()
- D. sleep()
Explanation: setTimeout() schedules a callback function to execute once after a specified time interval in milliseconds. setInterval() repeats the execution.
Question 26: What is the result of checking equality between double-declaring objects: console.log({} === {});?
- A. true
- B. false β (correct answer)
- C. undefined
- D. TypeError
Explanation: In JavaScript, objects are compared by reference, not value. Since {} creates a new object instance in memory, they point to separate references and are not equal.
Question 27: What does the this keyword refer to inside a standard arrow function?
- A. The global window object exclusively
- B. The dynamic call context object
- C. The lexically surrounding (parent) scope's
this context β (correct answer)
- D. The current function definition
Explanation: Arrow functions do not bind their own this. Instead, they inherit this lexically from the surrounding scope in which they were defined.
Question 28: What is hoisting in JavaScript?
- A. Selecting DOM nodes with classes
- B. Reassigning object variables dynamically
- C. The compiler behavior where variable and function declarations are moved to the top of their scope before code execution β (correct answer)
- D. Optimizing loop executions
Explanation: Hoisting allows using functions or variables declared with var before their declarations appear in the code, though variable initializations are not hoisted.
Question 29: What is the output of the following JavaScript code?
``javascript
const p = new Promise((resolve) => {
resolve("Success");
});
p.then(res => console.log(res));
console.log("Done");
``
- A. Success Done
- B. Done Success β (correct answer)
- C. Done
- D. Success
Explanation: Promises are asynchronous microtasks. The code runs synchronously first, printing "Done". Then, in the next tick of the event loop, the promise callback executes, printing "Success".
Question 30: What is the purpose of "use strict" in JavaScript files?
- A. Speeds up dynamic compilation
- B. Enforces strict parsing and error handling, preventing accidental globals and quiet errors β (correct answer)
- C. Imports system modules automatically
- D. Disables loop executions
Explanation: The "use strict" directive signals the browser/compiler to execute the script in Strict Mode, flagging silent bugs as explicit errors.