1. Short-Circuit Evaluation for Default Values
const name = userInput || 'Guest';
2. Destructuring for Cleaner Code
const person = { name: "John", age: 25 };
const { name, age } = person;
3. Optional Chaining (?.)
const street = user?.address?.street;
4. Remove Duplicates from an Array
const unique = [...new Set([1, 2, 2, 3, 4])]; // [1, 2, 3, 4]
5. Swap Variables Without Temporary Variable
let a = 1, b = 2;
[a, b] = [b, a];
6. Convert Anything to Boolean
!!"" // false
!!"hello" // true
7. Measure Code Performance
console.time("loop");
for (let i = 0; i < 1000; i++) {}
console.timeEnd("loop");
8. Random Number in a Range
const random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
9. Repeat a String
'Hi! '.repeat(3); // "Hi! Hi! Hi! "
10. Dynamically Access Object Keys
const key = "name";
const person = { name: "Alice" };
console.log(person[key]); // Alice
11. Convert String to Number
12. Merge Objects
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const merged = { ...obj1, ...obj2 };
13. Object Property Shorthand
const name = "John", age = 30;
const user = { name, age };
14. Debounce Function
function debounce(func, delay) {
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(() => func.apply(this, args), delay);
};
}
15. One-Liner for Deep Clone (Simple Objects)
const clone = JSON.parse(JSON.stringify(obj));