Swap VariablesProblem StatementUse array destructuring to swap two variables `a` and `b` without using a temporary variable.Approach 1. Use the syntax `[a, b] = [b, a]`. Solution`function swap(a, b) { [a, b] = [b, a]; return [a, b];
Template LiteralsProblem StatementWrite a function `formatInfo(name, age)` that returns "Name: name, Age: age" using a template literal.Approach` 1. Use backticks ` `. 2. Use `${variableSolution`function formatInfo(name, age) { return `Name: ${name
Default ParametersProblem StatementWrite a function `power(base, exponent)` where `exponent` defaults to 2.Approach 1. Define the parameter as `exponent = 2`. Solution`function power(base, exponent = 2) { return Math.pow(base, exponent);
Clone ArrayProblem StatementWrite a function `cloneArray(arr)` that returns a shallow copy of an array using the spread operator.Approach 1. Use `[...arr]`. Solution`function cloneArray(arr) { return [...arr];
Rest ParametersProblem StatementWrite a function `sumArgs(...args)` that results in the sum of all arguments passed.Approach 1. `...args` converts arguments into an array. 2. Use `.reduce()` to sum them. Solution`function sumArgs(...args) { return args.reduce((acc, val) => acc + val, 0);
Property ShorthandProblem Statement`Write a function `makeUser(id, name)` that returns an object `{id: id, name: nameApproach` 1. Just use `return { id, nameSolution`function makeUser(id, name) { return { id, name
Object DestructuringProblem StatementExtract `x` and `y` from an object `point` using destructuring.Approach` 1. Use `const { x, ySolution`function getPoint(point) { const { x, y
Merge ObjectsProblem StatementWrite a function `mergeObjects(obj1, obj2)` that combines two objects into a new one using spread.Approach` 1. Use `{...obj1, ...obj2Solution`function mergeObjects(obj1, obj2) { return { ...obj1, ...obj2
Optional ChainingProblem StatementSafely access `user.info.email` using optional chaining. Return `null` if it doesn't exist.Approach 1. Use `user?.info?.email`. 2. Wrap in a nullish coalescing or just return the result. Solution`function getEmail(user) { return user?.info?.email ?? null;
Nullish CoalescingProblem StatementWrite a function `getValue(val)` that returns `val` unless it is `null` or `undefined`, in which case it returns "Default".Approach 1. Use `??`. Note that it differs from `||` as it keeps falsy values like `0` or `""`. Solution`function getValue(val) { return val ?? "Default";
Array at()Problem StatementUse the `.at()` method to get the second to last element of an array.Approach 1. `.at(-2)` gets the second to last element. Solution`function getSecondToLast(arr) { return arr.at(-2);
Unique ArrayProblem StatementWrite a function `getUnique(arr)` that uses a `Set` to remove duplicates from an array.Approach 1. Create `new Set(arr)`. 2. Spread it back into an array `[...set]`. Solution`function getUnique(arr) { return [...new Set(arr)];
Map ObjectProblem StatementCreate a Map and add a key-value pair where the key is an object `{ id: 1 }` and the value is \"User 1\".Approach 1. Use `new Map()`. 2. Use `map.set(key, value)`. Solution`function createMap() { const map = new Map(); const key = { id: 1
Loop with For-ofProblem StatementWrite a function `logAll(arr)` that uses a `for...of` loop to sum all numbers.Approach 1. Use `for (const item of arr)`. Solution`function sumLoop(arr) { let sum = 0; for (const num of arr) { sum += num;
Numeric SeparatorsProblem StatementWrite a function that returns the number one million using numeric separators for readability.Approach 1. Use underscores like `1_000_000`. Solution`function getMillion() { return 1_000_000;
String IncludesProblem StatementCheck if a sentence includes the word "JavaScript" regardless of case.Approach 1. Convert both to lowercase. 2. Use `.includes()`. Solution`function hasJS(sentence) { return sentence.toLowerCase().includes("javascript");
Promise All SettledProblem StatementWrite a function `checkAll(promises)` that waits for all promises to finish regardless of success/fail.Approach 1. Use `Promise.allSettled(promises)`. Solution`function checkAll(promises) { return Promise.allSettled(promises);
Logical AssignmentProblem StatementUse the nullish assignment operator (`??=`) to set `obj.timeout` to 3000 only if it is currently `null` or `undefined`.Approach 1. The operator `??=` assigns if the left side is nullish. Solution`function setDefaultTimeout(obj) { obj.timeout ??= 3000; return obj;
From EntriesProblem StatementConvert an array of pairs `[['a', 1], ['b', 2]]` into an object using `Object.fromEntries()`.Approach 1. Pass the array of arrays to `Object.fromEntries()`. Solution`function toObject(pairs) { return Object.fromEntries(pairs);
Private FieldsProblem StatementCreate a class `Counter` with a private field `#count` initialized to 0. Add an `increment()` method.Approach 1. Declare the private field with `#`. 2. Access it within the class using `this.#count`. Solution`class Counter { #count = 0; increment() { this.#count++; return this.#count;