CIW JavaScript Specialist 2 — Questions and Answers
Question 1: Which JavaScript method returns a new array containing only elements that pass a provided test function?
- map()
- filter() (Correct answer)
- reduce()
- forEach()
Correct answer: filter()
filter() creates a new array with all elements for which the callback returns a truthy value.
Question 2: What does the 'this' keyword refer to inside an arrow function?
- The arrow function itself
- The global object always
- The enclosing lexical context (Correct answer)
- undefined in strict mode
Correct answer: The enclosing lexical context
Arrow functions do not have their own 'this'; they inherit it from the surrounding lexical scope.
Question 3: Which event is fired when the browser has fully loaded the HTML and built the DOM, but before images and stylesheets finish loading?
- load
- DOMContentLoaded (Correct answer)
- readystatechange
- beforeunload
Correct answer: DOMContentLoaded
DOMContentLoaded fires when the HTML document is fully parsed and the DOM is ready, without waiting for external resources.
Question 4: What is the result of: typeof null in JavaScript?
- 'null'
- 'undefined'
- 'object' (Correct answer)
- 'boolean'
Correct answer: 'object'
typeof null returns 'object' due to a historic bug in JavaScript that was never corrected for backward compatibility.
Question 5: Which Array method executes a reducer function on each element and returns a single accumulated value?
- find()
- some()
- reduce() (Correct answer)
- flat()
Correct answer: reduce()
reduce() applies a callback function against an accumulator and each element to reduce the array to a single value.
Question 6: In JavaScript, what does the spread operator (...) do when used with an array?
- Creates a deep copy of nested objects
- Expands the array into individual elements (Correct answer)
- Merges arrays without removing duplicates
- Converts an array to a Set
Correct answer: Expands the array into individual elements
The spread operator unpacks an array's elements so they can be passed individually as function arguments or combined into another array.
Question 7: Which JavaScript statement is used to exit a loop immediately?
- continue
- return
- break (Correct answer)
- exit
Correct answer: break
The break statement terminates the current loop or switch statement and transfers control to the code following it.
Which JavaScript method returns a new array containing only elements that pass a provided test function?