Member-only story
5 Javascript trick you must know
Okay, here are 5 tricks you can master to enhance your coding skills and productivity greatly. let’s get to it
Destructuring assignment
The destructuring assignment offers a concise method to extract values from arrays or objects and assign them to variables, thereby simplifying your code and enhancing readability. For arrays, bracket notation is used, while braces are employed for objects.
// Destructuring assignment
const[a,b, ...theRest] = [1,2,3,4,5,6,7];
console.log(a);
// Expected otput: 1
console.log(b);
// Expected otput: 2
console.log(theRest);
// Expected output: Array [3,4,5,6,7]
// Destructuring object
const {name, breed, ...details} = {name: 'Nimbus', age:'4', type:'cat', color:'orange'};
console.log(name);
// Expected otput: "Nimbus"
console.log(b);
// Expected otput: "4"
console.log(details.type);
// Expected output: cat
console.log(details.color);
// Expected output: orange
Spread Syntax
Spread syntax looks exactly like rest syntax. In a way, spread syntax is the opposite of rest syntax.
The spread syntax allows you to spread the elements of an array or the properties of an object into another array or object. This is particularly useful for tasks like making copies, merging objects, and passing multiple…