Web Development JavaScript 3 — Questions and Answers
Question 1: What is the purpose of the `Promise.all()` method?
- Runs promises sequentially and returns the last result
- Resolves when all promises resolve, or rejects if any one rejects (Correct answer)
- Returns the first promise to settle
- Ignores rejected promises and returns only resolved values
Correct answer: Resolves when all promises resolve, or rejects if any one rejects
`Promise.all()` takes an iterable of promises and returns a single promise that resolves with an array of results when all input promises resolve.
Question 2: What does the spread operator (`...`) do when used with an array?
- Creates a reference to the array
- Deletes the array elements
- Expands the array elements into individual arguments or elements (Correct answer)
- Sorts the array in place
Correct answer: Expands the array elements into individual arguments or elements
The spread operator expands an iterable like an array into individual elements, useful for function calls or array literals.
Question 3: Which event fires when the DOM is fully loaded but before images and stylesheets have loaded?
- window.onload
- DOMContentLoaded (Correct answer)
- readystatechange
- beforeunload
Correct answer: DOMContentLoaded
`DOMContentLoaded` fires when the initial HTML is parsed and DOM is ready, without waiting for stylesheets, images, or subframes.
Question 4: What is the result of `typeof undefined`?
- 'null'
- 'void'
- 'undefined' (Correct answer)
- 'object'
Correct answer: 'undefined'
`typeof undefined` returns the string `'undefined'`, which is one of the few safe uses of `typeof` with uninitialized values.
Question 5: How do you prevent an object's properties from being modified in JavaScript?
- Object.seal()
- Object.freeze() (Correct answer)
- Object.lock()
- Object.protect()
Correct answer: Object.freeze()
`Object.freeze()` prevents adding, removing, or modifying properties on an object.
Question 6: What is the difference between `==` and `===` in JavaScript?
- `==` checks reference equality; `===` checks value equality
- `==` performs type coercion before comparison; `===` requires both value and type to match (Correct answer)
- `===` is faster but less accurate than `==`
- They are identical in behavior
Correct answer: `==` performs type coercion before comparison; `===` requires both value and type to match
`==` (loose equality) coerces types before comparing, while `===` (strict equality) requires both value and type to be identical.
Question 7: What does `Array.prototype.reduce()` do?
- Filters array elements based on a condition
- Reduces the array length by removing duplicates
- Executes a reducer function on each element to produce a single accumulated value (Correct answer)
- Sorts elements and returns the smallest
Correct answer: Executes a reducer function on each element to produce a single accumulated value
`reduce()` iterates over an array applying a callback to an accumulator and each element, ultimately returning a single value.
What is the purpose of the `Promise.all()` method?