I Tried and Loved: 7 Javascript tricks

👋🏻 Hey, I'm Priya I am software developer at HackerRank.
I have been a Student Software Developer under Google Summer of Code for CHAOSS and have been an intern at Dineout (Times Internet).
💬 Reach me: twitter.com/shivikapriya
These are my notes for this video
Removing Falsy Values from an Array:
- Use the
filtermethod with a callback function that returnsBoolean(name)to remove falsy values.
- Use the
const filteredName = myName.filter(name => Boolean(name));
Checking for Even Numbers in an Array:
- Use the
somemethod to check if at least one element in the array passes a test.
- Use the
const hasEvens = array.some(num => num % 2 === 0);
Removing Duplicate Items from an Array:
- Use a
Setto automatically remove duplicate items and spread the set into a new array.
- Use a
const uniqueArray = [...new Set(array)];
Using Nullish Coalescing Operator for Default Values:
- Instead of using a ternary operator, use the nullish coalescing operator (
??) to provide a default value for null or undefined.
- Instead of using a ternary operator, use the nullish coalescing operator (
const greeting = name ?? "friend";
Simplifying Sort Order in Array Sorting:
- Use
aandzas variables instead ofaandbwhen sorting in ascending order, and usezandafor descending order.
- Use
array.sort((a, z) => a - z); // ascending order
array.sort((z, a) => a - z); // descending order
Destructuring Arrays and Objects:
- Use destructuring assignment to assign variables from arrays or objects.
const [first, middle, last] = myName;
const { name, username, age } = person;
Creating Arrays of a Specific Length and Filling with Values:
- Use the
Array.frommethod with a mapping function to create an array of a specific length filled with arbitrary values.
- Use the
const newArray = Array.from({ length: n }, (_, index) => index);




