Must-Know JavaScript One-Liners for Developers: Part 1
Level up your JavaScript skills with these powerful single-line codes.

Reverse a string
The
splitfunction helps us break a string into multiple individual elements, which can then bereversedandjoinedto a full string.
function isPalindrome(str) {
return str === str.split('').reverse().join('');
}
// Example usage:
console.log(isPalindrome("racecar")); // Output: true // Output: false
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]
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.
About the author
馃殌 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 馃尡
Comments
Join the discussionNo comments yet. Be the first to comment.






