Web Development JavaScript 5 — Questions and Answers
Question 1: What does the `async` keyword do when placed before a function declaration?
- Makes the function run on a separate thread
- Causes the function to always return a Promise
- Allows the function to pause execution with `await`
- Both B and C (Correct answer)
Correct answer: Both B and C
An `async` function always returns a Promise and enables the use of `await` inside it to pause execution until a Promise settles.
Question 2: What is the correct way to clone an object shallowly in modern JavaScript?
- Object.clone(obj)
- Object.assign({}, obj) (Correct answer)
- obj.clone()
- JSON.parse(obj)
Correct answer: Object.assign({}, obj)
`Object.assign({}, obj)` copies all enumerable own properties from `obj` into a new object, creating a shallow clone.
Question 3: What is 'hoisting' in JavaScript?
- Moving JavaScript files to the top of the HTML document
- The behavior where variable and function declarations are moved to the top of their scope before execution (Correct answer)
- A performance optimization that caches frequently used functions
- A method to import modules at the start of a script
Correct answer: The behavior where variable and function declarations are moved to the top of their scope before execution
JavaScript hoisting moves declarations (not initializations) to the top of their scope during compilation, so functions and `var` declarations are available before their line in code.
Question 4: What does `Symbol()` create in JavaScript?
- A mathematical symbol constant
- A unique and immutable primitive value used as object property keys (Correct answer)
- A string with special formatting
- An enumeration type
Correct answer: A unique and immutable primitive value used as object property keys
`Symbol()` creates a unique primitive that is guaranteed to be distinct from all other values, even other Symbols created with the same description.
Question 5: Which of the following correctly uses optional chaining (`?.`)?
- user?.name ?? 'Guest' (Correct answer)
- user?name
- user?.?.name
- user.?name
Correct answer: user?.name ?? 'Guest'
`user?.name` safely accesses `name` on `user`, returning `undefined` instead of throwing if `user` is `null` or `undefined`.
Question 6: What is the purpose of `Object.keys()` in JavaScript?
- Returns an array of all properties including inherited ones
- Returns an array of an object's own enumerable string-keyed property names (Correct answer)
- Returns the number of properties in an object
- Checks if a key exists in an object
Correct answer: Returns an array of an object's own enumerable string-keyed property names
`Object.keys()` returns an array of the object's own enumerable property names, excluding inherited or Symbol-keyed properties.
Question 7: In JavaScript, what is the 'event loop' responsible for?
- Looping through DOM elements to attach event listeners
- Managing the order in which tasks and microtasks are executed in the single-threaded runtime (Correct answer)
- Automatically re-rendering the UI when events fire
- Handling errors thrown inside event callbacks
Correct answer: Managing the order in which tasks and microtasks are executed in the single-threaded runtime
The event loop continuously checks the call stack and task/microtask queues, executing queued callbacks when the stack is empty to enable non-blocking async behavior.
What does the `async` keyword do when placed before a function declaration?