JavaScript Junior

1 / 21

What is the difference between var, let, and const?

Select the correct answer

1

var and let are global-scoped; const is block-scoped and always reassignable.

2

var is block-scoped; let and const are function-scoped, and let can't be reassigned.

3

All three are block-scoped, but only const is hoisted to the top of its block.

4

var is function-scoped; let and const are block-scoped, and const can't be reassigned.

Explain the difference between Global Scope, Function Scope, and Block Scope.

Select the correct answer

1

Global covers a file; function scope covers blocks; block scope covers modules only.

2

Global is per module; function scope is global; block scope covers entire functions.

3

Global is per file; function scope is per block; block scope applies only to loops.

4

Global is accessible everywhere; function scope is per function; block scope is per {}.

What is variable shadowing?

Select the correct answer

1

A constant is reassigned and its previous value is hidden by the engine

2

A variable is referenced before its declaration is hoisted within the scope

3

A global variable is silently created when an assignment omits a keyword

4

An inner scope declares a variable with the same name as one in an outer scope

What is the difference between null and undefined? When would you intentionally use one over the other?

Select the correct answer

1

undefined is a deliberate empty value, while null means a missing variable

2

null and undefined are identical and both indicate a deleted object reference

3

null is reserved for numbers, while undefined applies to all object types

4

null is an intentional absence of value, while undefined means not yet assigned

How does the typeof operator behave with null and arrays, and why is typeof null returning 'object' a historical bug?

Select the correct answer

1

Both return "object"; null had a type tag of zero in early implementations

2

Arrays return "array" and null returns "null", reflecting their distinct value tags

3

Arrays return "object" and null returns "undefined" due to a parser limitation

4

Both return "object" because null was deliberately defined as an empty object reference

What are the primitive data types in JavaScript?

Select the correct answer

1

string, number, boolean, null, object, symbol, array

2

string, number, boolean, object, array, function, date

3

string, number, float, boolean, undefined, symbol, bigint

4

string, number, boolean, null, undefined, symbol, bigint

Why does 0.1 + 0.2 not equal 0.3 in JavaScript?

Select the correct answer

1

JavaScript stores all numbers as integers, so the decimal fractions get truncated on addition

2

The addition operator rounds each operand to one digit before combining the two values

3

Floating-point math is performed in base ten but limited to fifteen significant digits total

4

These decimals cannot be represented exactly in binary floating-point, causing rounding error

What is type coercion, and why is === generally preferred over ==? Are there cases where == is actually useful?

Select the correct answer

1

=== coerces types and == does not, making == faster and safer for nearly all comparisons.

2

Neither coerces types, but === also compares object identity, so the two are equivalent.

3

== coerces types and === does not, but == null usefully checks both null and undefined.

4

Both compare identically, so === merely exists for backward compatibility with older code.

What is a higher-order function? Provide examples from the built-in Array.prototype.

Select the correct answer

1

A function defined at the top level of a module, such as parseInt, isNaN, and encodeURI.

2

A function that mutates the array it is called on in place, such as push, pop, and splice.

3

A function that accepts a function argument or returns one, such as map, filter, and reduce.

4

A function that runs faster because the engine optimizes it, like sort and join.

What is the difference between an arrow function and a regular function regarding this?

Select the correct answer

1

Arrow functions create a new this bound to whatever object invokes them at call time.

2

Arrow functions have no own this and inherit it from the enclosing lexical scope.

3

Both resolve this dynamically, but arrow functions can be rebound later with bind.

4

Regular functions inherit this lexically, while arrow functions resolve it dynamically.

What is the difference between a function declaration and a function expression?

Select the correct answer

1

Both are hoisted identically, but only declarations can be assigned to a variable name.

2

A declaration is hoisted with its body, while an expression's name is not usable before definition.

3

An expression is hoisted with its body, while a declaration must be defined before it is called.

4

Neither is hoisted, but expressions are evaluated earlier during the parsing phase.

What is the difference between map, forEach, filter, and reduce?

Select the correct answer

1

map transforms into a new array, forEach returns nothing, filter selects, reduce yields one value.

2

map mutates in place, forEach copies it, filter sorts, and reduce reverses the array.

3

map returns a value, forEach returns an array, filter counts, and reduce groups items.

4

map and forEach both return new arrays, while filter and reduce mutate the original.

What are the three states of a Promise?

Select the correct answer

1

Waiting, settled, and rejected.

2

Pending, fulfilled, and rejected.

3

Started, completed, and failed.

4

Pending, resolved, and cancelled.

What does preventDefault() do, and how does it differ from stopPropagation()?

Select the correct answer

1

preventDefault cancels the browser's default action; stopPropagation stops the event from propagating further

2

preventDefault delays the default action briefly; stopPropagation prevents the event from being dispatched at all

3

preventDefault stops the event bubbling to parents; stopPropagation cancels the browser's default behavior entirely

4

preventDefault removes all listeners on the element; stopPropagation pauses the event loop until the handler returns

How can you use closures to create private variables?

Select the correct answer

1

Declaring variables with the private keyword inside a function body to hide them

2

An outer function holds variables and returns inner functions that can access them

3

Marking variables as const so external code cannot read or reassign them

4

Using the # prefix on variables declared at the top level of a module

Explain the difference between passing by value and passing by reference in JavaScript.

Select the correct answer

1

All values including primitives are passed as references to the original

2

Primitives are passed by reference while objects are passed by their value

3

All values including objects are deeply copied when passed into a function

4

Primitives are passed by value while objects are passed by their reference

What is the Nullish Coalescing operator (??) and how does it differ from the Logical OR operator?

Select the correct answer

1

?? returns the right side only when the left is null or undefined

2

|| returns the right side only when the left is null or undefined

3

?? returns the left side only when the right is null or undefined

4

?? returns the right side whenever the left is any falsy value such as 0

What is 'Optional Chaining' (?.) and where does it short-circuit?

Select the correct answer

1

It returns undefined yet keeps evaluating the rest of the chain anyway

2

It returns undefined and stops evaluating when a reference is null or undefined

3

It returns null and stops evaluating when a reference is any falsy value at all

4

It throws an error but stops evaluating when a reference is null or undefined

What is the difference between the spread operator and rest parameters, and how are they used?

Select the correct answer

1

Both collect multiple arguments into a single array passed into the function

2

Both expand iterables into individual elements depending on where they are used

3

Spread expands an iterable into elements; rest collects arguments into one array

4

Rest expands an iterable into elements; spread collects arguments into one array

What is destructuring assignment, and how does it work for objects and arrays?

Select the correct answer

1

It unpacks array values by position and object values by matching property names

2

It copies entire arrays and objects into new variables without unpacking them

3

It merges multiple arrays and objects into one combined destination variable

4

It unpacks array values by property name and object values by their position

What is the difference between for...in and for...of loops?

Select the correct answer

1

for...in runs synchronously while for...of runs asynchronously over the iterator protocol

2

for...in iterates enumerable property keys; for...of iterates values of iterable objects

3

for...in iterates array values; for...of iterates only the numeric index keys of objects

4

for...in works on iterables only; for...of works on any plain object with enumerable keys