Skip to main content

Command Palette

Search for a command to run...

Must-Know JavaScript One-Liners for Developers: Part 1

Level up your JavaScript skills with these powerful single-line codes.

Updated
1 min read
Must-Know JavaScript One-Liners for Developers: Part 1
V

🚀 Software Engineer at TechM | Ex Merkle | Design Enthusiast 🎨 | Graduate of KLS Gogte Institute of Technology 🎓 | Connector of People & Ideas 💡 | | React.js Aficionado | Eager Learner & Growth Advocate 🌱

  • Reverse a string

    The split function helps us break a string into multiple individual elements, which can then be reversed and joined to a full string.

function isPalindrome(str) {
   return str === str.split('').reverse().join('');

}

// Example usage:
console.log(isPalindrome("racecar")); // Output: true   // Output: false
  • Removing Duplicates from an Array

Removing duplicates involves creating a new array that only contains unique elements.

function removeDuplicates(arr) {
    return [...new Set(arr)];
}

// Example usage:
console.log(removeDuplicates([9, 2, 2, 3, 4, 4, 5])); 
// Output: [9, 2, 3, 4, 5]
  • flattenArray

flatten array can be directly done using the flat property of the array.

function flattenArray(arr) {
    return arr.flat(Infinity);
}
// Example usage:
console.log(flattenArray([1, [2, [3, [4, 5]]]]));

That's it for today's brief read. More will be coming in the next parts, so stay tuned. Follow for more updates.