javascriptintermediate
JavaScript ES6+ Quiz
Arrow functions, destructuring, promises, modules, and modern JavaScript features.
7 questions
By EZ4Code Team
1. What is the key difference between an arrow function and a regular function?
const obj = {
name: "Alice",
greet: function() { return this.name; },
greetArrow: () => this.name
};
obj.greet(); // ?
obj.greetArrow(); // ?Arrow functions have their own `this`
Arrow functions inherit `this` from the enclosing scope
Arrow functions cannot take parameters
Arrow functions are slower
Explanation: Arrow functions do NOT have their own `this` — they inherit it from the enclosing lexical scope. So `obj.greetArrow()` returns `undefined` (or the outer `this.name`), while `obj.greet()` returns `"Alice"`. This is why arrow functions are not suitable for object methods that need `this`.
2. What does this destructuring produce?
const { a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 };
console.log(rest);{ c: 3, d: 4 }
[3, 4]
{ a: 1, b: 2 }
undefined
Explanation: The rest operator `...rest` collects all remaining properties into a new object. After extracting `a` and `b`, the remaining `c` and `d` go into `rest`, giving `{ c: 3, d: 4 }`.
3. What does `async/await` resolve to?
async function f() {
return 42;
}
f().then(v => console.log(v));42
Promise { 42 }
undefined
Error
Explanation: An `async` function always returns a Promise. When you `return 42`, the Promise resolves with `42`, so `.then(v => ...)` receives `42`. The `await` keyword unwraps this inside another async function.
4. Which export syntax is a NAMED export?
// A
export const x = 1;
// B
export default function() {}
// C
export default class {}
// D
export default 42;export const x = 1;
export default function() {}
export default class {}
export default 42;
Explanation: `export const x = 1;` is a named export — you import it with `import { x } from './module'`. The other three are default exports, imported as `import anyName from './module'`. A module can have many named exports but only one default.
5. What is the output of this Promise chain?
Promise.resolve(1)
.then(v => v + 1)
.then(v => v * 2)
.then(v => console.log(v));2
4
3
6
Explanation: Start with 1, then `v + 1` = 2, then `v * 2` = 4. Each `.then` receives the resolved value of the previous one. So the final output is `4`.
6. What does the spread operator do here?
const a = [1, 2];
const b = [3, 4];
const merged = [...a, ...b];
console.log(merged);[[1, 2], [3, 4]]
[1, 2, 3, 4]
{1, 2, 3, 4}
Error
Explanation: The spread operator `...` expands an iterable into individual elements. So `[...a, ...b]` creates a new array containing all elements of `a` followed by all elements of `b`, giving `[1, 2, 3, 4]`.
7. What is a `Set` in JavaScript?
An ordered collection that allows duplicates
A collection of unique values
A key-value mapping
A fixed-size array
Explanation: A `Set` is a collection of unique values — duplicates are automatically removed. Key-value mappings are `Map`s. Sets are not indexed (you can't do `set[0]`), but they are iterable in insertion order.
More javascript Quizzes
JavaScript Basics Quiz
beginnerVariables, types, operators, functions, and core JavaScript concepts.
JavaScript DOM Manipulation
intermediateDOM manipulation and event handling
JavaScript Asynchronous Programming
intermediatePromise, async/await, fetch
JavaScript Array Methods
intermediatemap, filter, reduce and other array higher-order methods
JavaScript Closures and Scope
advancedClosures, scope chain and hoisting