Web Development JavaScript 4 — Questions and Answers
Question 1: What is the output of `console.log([] + [])`?
- []
- "" (Correct answer)
- 0
- null
Correct answer: ""
Both arrays are coerced to empty strings, and string concatenation produces an empty string `""`.
Question 2: Which of the following correctly uses destructuring to extract values from an object?
- const [name, age] = person;
- const {name, age} = person; (Correct answer)
- const name, age = person.name, person.age;
- const (name, age) = person;
Correct answer: const {name, age} = person;
Object destructuring uses curly braces to extract named properties into variables.
Question 3: What does `event.stopPropagation()` do in JavaScript?
- Prevents the default browser action for an event
- Stops the event from bubbling up to parent elements (Correct answer)
- Removes all event listeners from an element
- Cancels the event entirely before it reaches the target
Correct answer: Stops the event from bubbling up to parent elements
`stopPropagation()` halts the event from traveling up (or down) the DOM tree after the current handler executes.
Question 4: What is a JavaScript generator function?
- A function that automatically generates test data
- A function declared with `function*` that can pause and resume execution using `yield` (Correct answer)
- A factory function that produces class instances
- A function that runs asynchronously without a callback
Correct answer: A function declared with `function*` that can pause and resume execution using `yield`
Generator functions use `function*` syntax and `yield` to produce a sequence of values on demand, pausing execution between yields.
Question 5: What does `localStorage.setItem('key', value)` do when `value` is an object?
- Stores the object with full fidelity
- Converts the object to JSON automatically
- Stores `[object Object]` as a string (Correct answer)
- Throws a TypeError
Correct answer: Stores `[object Object]` as a string
`localStorage` only stores strings, so objects are coerced via `.toString()`, producing `[object Object]` unless you manually call `JSON.stringify()`.
Question 6: Which method is used to attach an event handler to a DOM element in modern JavaScript?
- element.attachEvent()
- element.on()
- element.addEventListener() (Correct answer)
- element.bindEvent()
Correct answer: element.addEventListener()
`addEventListener()` is the standard method to attach event handlers, allowing multiple handlers on the same element.
Question 7: What is the purpose of the `WeakMap` in JavaScript?
- A Map that allows only primitive keys
- A Map whose keys must be objects and are held weakly so they can be garbage collected (Correct answer)
- A Map with a limited maximum size
- A read-only version of Map
Correct answer: A Map whose keys must be objects and are held weakly so they can be garbage collected
`WeakMap` keys must be objects and are weakly referenced, meaning they don't prevent garbage collection when no other references exist.
What is the output of `console.log([] + [])`?