First 50 of 199 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.
Continue in the practice tool above to load more questions, 50 at a time.
Question 1JavaScript Basics and Syntax
Which keyword declares a block-scoped variable that cannot be reassigned after initialization?
- let
- static
- var
- const Correct answer.
Explanation
The const declaration creates a binding that is block-scoped and must be initialized at declaration time, and reassigning it throws a TypeError. The binding itself is immutable, though objects it references can still be mutated.
Question 2JavaScript Basics and Syntax
What does the expression typeof null evaluate to?
- "undefined"
- "object" Correct answer.
- "boolean"
- "null"
Explanation
typeof null returns the string "object" because of a long-standing historical quirk in the language's original implementation. This is not a reliable null check; comparing with === null is the correct approach.
Question 3JavaScript Basics and Syntax
Given let x = 10, what is the value of the expression x++ ?
- 10 Correct answer.
- NaN
- 11
- undefined
Explanation
The postfix increment operator returns the value before incrementing. So the expression evaluates to 10 while x becomes 11 afterward. Using ++x would return 11 instead.
Question 4JavaScript Basics and Syntax
Which operator compares two values for equality without performing type coercion?
- !=
- =
- === Correct answer.
- ==
Explanation
The strict equality operator === compares both type and value and returns false when the operands differ in type. The loose equality operator == coerces operands before comparing.
Question 5JavaScript Basics and Syntax
What is the result of evaluating the string expression "5" + 3 in JavaScript?
- "53" Correct answer.
- 8
- "8"
- NaN
Explanation
When either operand of + is a string, the operator performs string concatenation rather than numeric addition. The number 3 is converted to the string "3" and appended, giving "53".
Question 6JavaScript Basics and Syntax
Which method adds one or more elements to the end of an array and returns the new length?
- push Correct answer.
- shift
- unshift
- pop
Explanation
Array.prototype.push appends its arguments to the end of the array and returns the updated length. This differs from pop, which removes from the end and returns the removed element.
Question 7JavaScript Basics and Syntax
What value does a function return when it completes execution without encountering a return statement?
- null
- undefined Correct answer.
- false
- 0
Explanation
A function with no executed return statement implicitly returns undefined. This is distinct from returning no value explicitly, which also yields undefined.
Question 8JavaScript Basics and Syntax
Which statement about let declarations is accurate?
- They create properties on the global object when declared at top level.
- They are accessible before declaration within the same block.
- They create a binding limited to the enclosing block. Correct answer.
- They are hoisted to the top of the function scope.
Explanation
let declarations are scoped to the nearest enclosing block, such as a loop body or an if block. The binding is also hoisted but stays in a temporal dead zone until the declaration is evaluated.
Question 9JavaScript Basics and Syntax
What does the following expression evaluate to? 0 == false
- false
- true Correct answer.
- undefined
- TypeError
Explanation
Loose equality coerces the boolean false to the number 0 before comparing, so 0 == false is true. Using strict equality, 0 === false, would evaluate to false because the types differ.
Question 10JavaScript Basics and Syntax
Which value is falsy in JavaScript?
- []
- {}
- "0"
- 0 Correct answer.
Explanation
The number 0 is one of the falsy values, along with "", null, undefined, NaN, and false. Empty arrays and empty objects are truthy because objects are always truthy.
Question 11JavaScript Basics and Syntax
What is the output of the following code? for (var i = 0; i < 3; i++) {} console.log(i);
- undefined
- ReferenceError
- 3 Correct answer.
- 2
Explanation
A var declaration in a for loop is function-scoped, so i remains accessible after the loop. The loop stops when i reaches 3, which is the value printed.
Question 12JavaScript Basics and Syntax
Which expression correctly checks whether the variable name holds the string "admin"?
- name === "admin" Correct answer.
- name.equals("admin")
- name == "admin"
- name = "admin"
Explanation
The strict equality operator === compares value and type, so name === "admin" is true only when name is exactly that string. It avoids the coercion surprises of loose equality.
Question 13JavaScript Basics and Syntax
What does the expression 10 % 3 evaluate to?
- 3.33
- 3
- 1 Correct answer.
- 0
Explanation
The remainder operator % returns what is left after dividing the left operand by the right one. Since 10 divided by 3 leaves a remainder of 1, the result is 1.
Question 14JavaScript Basics and Syntax
Which statement about a switch statement is correct?
- The default clause must appear last.
- Fall-through between cases cannot occur.
- It evaluates each case expression using strict comparison. Correct answer.
- Case expressions may be arbitrary function calls.
Explanation
switch compares the discriminant against each case using strict equality, so values must match in type and value. Without break, execution falls through to subsequent cases.
Question 15JavaScript Basics and Syntax
What is logged by the following code? let a = 1; { let a = 2; } console.log(a);
- undefined
- ReferenceError
- 1 Correct answer.
- 2
Explanation
The inner let a inside the block creates a separate binding shadowing the outer one. When the block ends, the outer a is still 1, which is what console.log prints.
Question 16JavaScript Basics and Syntax
Which of these creates a valid arrow function that returns the square of its argument?
- const sq = (x) { return x * x; };
- const sq = x -> x * x;
- const sq = x => x * x; Correct answer.
- const sq = function x * x;
Explanation
An arrow function may omit parentheses for a single parameter and use an expression body whose value is returned implicitly. So x => x * x returns the square of x without a return keyword.
Question 17JavaScript Basics and Syntax
What does the following code print? console.log(typeof typeof 42);
- "undefined"
- "number"
- "object"
- "string" Correct answer.
Explanation
The inner typeof 42 evaluates to the string "number". Applying typeof again to that string yields "string", because the operand is now a string value.
Question 18JavaScript Basics and Syntax
Which array method returns a new array containing only elements that satisfy a provided test function?
- map
- forEach
- reduce
- filter Correct answer.
Explanation
filter builds and returns a new array whose elements all pass the predicate function. The original array is not modified by filter.
Question 19JavaScript Basics and Syntax
What is the value of the expression [1, 2, 3].length?
- 3 Correct answer.
- undefined
- 4
- 2
Explanation
The length property of an array reports the number of elements it contains. An array literal with three elements therefore has length 3.
Question 20JavaScript Basics and Syntax
What is logged by this code? console.log(0.1 + 0.2 === 0.3);
- false Correct answer.
- TypeError
- true
- NaN
Explanation
Binary floating-point cannot represent 0.1 and 0.2 exactly, so their sum is slightly greater than 0.3. The strict comparison therefore evaluates to false.
Question 21JavaScript Basics and Syntax
What does this code output? let i = 0; while (i < 3) { i++; } console.log(i);
- 3 Correct answer.
- 0
- 2
- 4
Explanation
The loop increments i while it is below 3, exiting when i reaches 3. Because the increment happens before the condition is rechecked, the final logged value is 3.
Question 22JavaScript Basics and Syntax
Given the code below, what is logged? const obj = { n: 1, get: function() { return this.n; } }; console.log(obj.get());
- 1 Correct answer.
- undefined
- ReferenceError
- NaN
Explanation
When a method is called as obj.get(), this refers to obj, so this.n resolves to 1. The result changes if the function is detached and called without a receiver.
Question 23JavaScript Basics and Syntax
What is the result of the expression true && "hello" ?
- false
- undefined
- true
- "hello" Correct answer.
Explanation
The logical AND operator returns the first falsy operand or the last operand if all are truthy. Since true is truthy, evaluation proceeds and returns "hello".
Question 24JavaScript Basics and Syntax
What is printed by this code? console.log(1 + 2 + "3");
- "33" Correct answer.
- 6
- "6"
- "123"
Explanation
Addition evaluates left to right, so 1 + 2 first yields the number 3. Then 3 + "3" triggers string concatenation because the right operand is a string, producing "33".
Question 25JavaScript Basics and Syntax
What is logged by this code? const f = function g() { return typeof g; }; console.log(f());
- "function" Correct answer.
- ReferenceError
- "undefined"
- "g"
Explanation
A named function expression binds its name inside its own scope, so g is accessible within the body. The typeof operator therefore reports "function", even though g is not visible outside.
Question 26JavaScript Basics and Syntax
Which operator has higher precedence in the expression a + b * c, and how does that affect evaluation?
- The + operator binds tighter, so a + b is evaluated before multiplying by c.
- The * operator binds tighter, so b * c is evaluated before a is added. Correct answer.
- Both operators have equal precedence, so evaluation proceeds strictly left to right.
- Precedence is undefined for mixed arithmetic, so the engine may choose either order.
Explanation
JavaScript assigns multiplication a higher precedence level than addition, so the grouping is a + (b * c) rather than (a + b) * c. This means the product is computed first and its result is then added to a. Parentheses can always override this default grouping when a different order is needed.
Question 27JavaScript Basics and Syntax
A developer writes the identifier totalPrice for a variable. Which statement about JavaScript identifiers is accurate?
- Identifiers may contain hyphens, so total-price is a legal variable name.
- Identifiers may begin with any digit as long as a letter appears somewhere later in the name.
- Identifiers may contain letters, digits, dollar signs, and underscores, but cannot start with a digit. Correct answer.
- Identifiers must be written entirely in uppercase to avoid clashing with keywords.
Explanation
An identifier must start with a letter, dollar sign, or underscore, and subsequent characters may also include digits. Starting with a digit is never allowed because such tokens would be parsed as numeric literals. Hyphens are reserved for subtraction, so they cannot appear inside a single identifier.
Question 28JavaScript Basics and Syntax
What is logged by the following code? const x = 5; function outer() { const x = 10; function inner() { return x; } return inner(); } console.log(outer());
- A ReferenceError is thrown
- 10 Correct answer.
- undefined
- 5
Explanation
The inner function closes over the scope in which it was defined, which is the body of outer where a local x equals 10. Lexical scoping resolves the name to the nearest enclosing binding, so the outer const x = 5 is not consulted. The returned value is therefore 10.
Question 29JavaScript Basics and Syntax
What is the value of the expression typeof null in JavaScript?
- "boolean"
- "undefined"
- "object" Correct answer.
- "null"
Explanation
The typeof operator reports "object" for null, a long-standing quirk retained for backward compatibility since the earliest implementations. To test for null reliably, compare directly with the null value using strict equality. The result does not indicate that null is a usable object.
Question 30JavaScript Basics and Syntax
What is logged by the following code? const nums = [10, 20, 30]; const result = nums.map(function (n, i) { return n + i; }); console.log(result.join(","));
- 10,21,30
- 10,21,32 Correct answer.
- 10,20,30
- 11,21,31
Explanation
The map callback receives the element value and its index, so each element is incremented by its position: 10+0, 20+1, and 30+2. The method returns a new array of those results, and join converts it to the comma-separated string 10,21,32. The original array is left unchanged.
Question 31Variables, Data Types, and Type Coercion
Which primitive type does the expression `typeof null` return in JavaScript?
- "object" Correct answer.
- "undefined"
- "null"
- "boolean"
Explanation
Since the earliest implementations, `typeof null` evaluates to the string "object" because of how the type tag for null was encoded. This is a long-standing quirk that is preserved for backward compatibility. It does not mean null is an object; it is a distinct primitive value.
Question 32Variables, Data Types, and Type Coercion
What value does the expression `0.1 + 0.2 === 0.3` evaluate to?
- NaN
- false Correct answer.
- true
- undefined
Explanation
Numbers are IEEE 754 double-precision floats, so 0.1 and 0.2 cannot be stored exactly. Their sum is slightly greater than 0.3, so the strict comparison yields false. This is a floating-point representation issue, not a JavaScript bug.
Question 33Variables, Data Types, and Type Coercion
Which statement about `let` and `var` declarations is correct?
- `let` bindings are hoisted but remain in a temporal dead zone until initialized. Correct answer.
- Both are hoisted and initialized to undefined at the top of their scope.
- `var` declarations are block-scoped while `let` declarations are function-scoped.
- Neither `let` nor `var` declarations are hoisted.
Explanation
`let` declarations are hoisted to the top of their block but are not initialized, so accessing them before the declaration throws a ReferenceError. This uninitialized period is called the temporal dead zone. By contrast, `var` is hoisted and initialized to undefined.
Question 34Variables, Data Types, and Type Coercion
What is the result of `"5" + 3` in JavaScript?
- "8"
- "53" Correct answer.
- NaN
- 8
Explanation
The `+` operator performs string concatenation when either operand is a string, so the number 3 is converted to "3" and joined to "5". The result is the string "53", not a numeric sum. Numeric addition only occurs when both operands are numbers after conversion.
Question 35Variables, Data Types, and Type Coercion
What does the expression `[] + []` evaluate to?
- "" (empty string) Correct answer.
- []
- 0
- NaN
Explanation
Both arrays are converted to primitives, and an array's default string conversion is its joined elements. Two empty arrays each become the empty string, and string concatenation yields the empty string. The `+` operator sees strings and concatenates rather than adding.
Question 36Variables, Data Types, and Type Coercion
Which value is the only one that is not strictly equal to itself (`x === x` is false)?
- -0
- NaN Correct answer.
- null
- undefined
Explanation
NaN represents an invalid numeric result and is defined to be unequal to every value, including itself. That is why `Number.isNaN` or `Object.is` is used to detect it. The other listed values are strictly equal to themselves.
Question 37Variables, Data Types, and Type Coercion
What is the value of `typeof undefined`?
- "null"
- "void"
- "undefined" Correct answer.
- "object"
Explanation
The `typeof` operator returns the string "undefined" for the undefined value as well as for variables that have been declared but not assigned. This distinguishes it from null, which returns "object". There is no "void" type name in the typeof result set.
Question 38Variables, Data Types, and Type Coercion
Which declaration creates a block-scoped constant that cannot be reassigned?
- var x = 1;
- let x = 1;
- static x = 1;
- const x = 1; Correct answer.
Explanation
`const` creates a binding that is block-scoped and must be initialized at declaration, and reassigning it throws a TypeError. `let` is block-scoped but reassignable, and `var` is function-scoped. `static` is not a variable declaration keyword at the top level.
Question 39Variables, Data Types, and Type Coercion
What does `Number("12px")` return?
- undefined
- 12
- NaN Correct answer.
- "12px"
Explanation
The Number function converts its argument strictly: the whole string must be a valid numeric literal, otherwise the result is NaN. Because "12px" contains trailing letters, conversion fails and NaN is returned. `parseInt` would instead parse the leading digits and return 12.
Question 40Variables, Data Types, and Type Coercion
What is the result of `Boolean("0")`?
- true Correct answer.
- false
- 0
- NaN
Explanation
When converting to boolean, only a specific set of values are falsy: false, 0, -0, 0n, "", null, undefined, and NaN. Any non-empty string, including "0", is truthy. Therefore Boolean("0") returns true.
Question 41Variables, Data Types, and Type Coercion
Which of these values is falsy in a boolean context?
- "false"
- []
- {}
- 0n Correct answer.
Explanation
The bigint zero literal 0n is one of the falsy values, along with false, 0, -0, "", null, undefined, and NaN. Non-empty strings, arrays, and objects are all truthy regardless of their contents. This is why an empty array is truthy.
Question 42Variables, Data Types, and Type Coercion
What does `null == undefined` evaluate to, and why?
- false, because they are different types
- true, because the loose equality algorithm treats them as equal Correct answer.
- true, because both coerce to 0
- false, because null is an object
Explanation
The abstract equality algorithm contains a special rule: null and undefined are loosely equal to each other and to nothing else. No numeric coercion happens in this comparison. With strict equality, `null === undefined` is false because their types differ.
Question 43Variables, Data Types, and Type Coercion
What is the value of `"" == 0`?
- undefined
- false
- true Correct answer.
- NaN
Explanation
With loose equality between a string and a number, the string is converted to a number first. The empty string converts to 0, so the comparison becomes 0 == 0, which is true. Strict equality `"" === 0` would be false because the types differ.
Question 44Variables, Data Types, and Type Coercion
What is the result of `[1,2,3] + ""`?
- [1,2,3]
- NaN
- 6
- "1,2,3" Correct answer.
Explanation
Adding a string forces the array to be converted to a primitive, and Array.prototype.toString joins the elements with commas. The result is the string "1,2,3". The elements are not summed, because the `+` operator concatenates once a string operand is present.
Question 45Variables, Data Types, and Type Coercion
Which operator returns the type of an operand as a lowercase string?
- typeof Correct answer.
- isPrototypeOf
- instanceof
- constructor
Explanation
The `typeof` operator returns a string such as "number", "string", "boolean", "object", "function", or "undefined". `instanceof` instead tests whether an object's prototype chain includes a given constructor's prototype. The other options are object methods, not type-returning operators.
Question 46Variables, Data Types, and Type Coercion
What does `undefined + 1` evaluate to?
- 1
- "undefined1"
- undefined
- NaN Correct answer.
Explanation
The `+` operator converts undefined to a number, which yields NaN, and any arithmetic on NaN produces NaN. Because there is no string operand, concatenation does not occur. The result is therefore NaN.
Question 47Variables, Data Types, and Type Coercion
Which statement correctly describes the value of `typeof function(){}`?
- "callable"
- "undefined"
- "object"
- "function" Correct answer.
Explanation
The `typeof` operator returns the special string "function" for callable objects, even though functions are technically objects. This is a deliberate exception so that functions can be distinguished from ordinary objects. There is no "callable" result from typeof.
Question 48Variables, Data Types, and Type Coercion
What is the result of `2 + 3 + "4"`?
- NaN
- 9
- "54" Correct answer.
- "234"
Explanation
The `+` operator is left-associative and evaluated left to right. First 2 + 3 performs numeric addition to give 5, then 5 + "4" concatenates to produce the string "54". The position of the string operand changes the outcome.
Question 49Variables, Data Types, and Type Coercion
What does `NaN === NaN` evaluate to?
- undefined
- NaN
- false Correct answer.
- true
Explanation
NaN is defined as unequal to every value, including itself, so `NaN === NaN` is false. This is why `Number.isNaN` exists to test for NaN reliably. The comparison operator returns a boolean, not NaN.
Question 50Variables, Data Types, and Type Coercion
What is the value of `typeof 10n`?
- "object"
- "number"
- "integer"
- "bigint" Correct answer.
Explanation
BigInt values are a distinct primitive type, and `typeof` reports them as "bigint". They are not the same type as regular numbers, even though they represent integers. Mixing a bigint and a number in arithmetic without explicit conversion throws a TypeError.