14 JavaScript One-Liners to Boost Your Productivity 🚀

durvesh_danve

Durvesh Danve

Posted on August 10, 2024

14 JavaScript One-Liners to Boost Your Productivity 🚀

01 - Remove Duplicates from an Array

const uniqueArr = arr => [...new Set(arr)];
console.log(uniqueArr([1, 2, 3, 4, 5, 5, 5]));

// [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

02 - Sum of Array Elements

const nums = [1, 2, 3, 4, 5];
const sum = nums.reduce((acc, num) => acc + num);
console.log(sum);

// 15
Enter fullscreen mode Exit fullscreen mode

03 - Check if a Number is Odd or Even

const isEven = num => num % 2 === 0;
console.log(isEven(5));

// false
Enter fullscreen mode Exit fullscreen mode

04 - Capitalize Every Word in a String

const capitalizeWords = str => 
  str.split(' ')
     .map(word => word.charAt(0).toUpperCase() + word.slice(1))
     .join(' ');

console.log(capitalizeWords('hello world'));
// 'Hello World'
Enter fullscreen mode Exit fullscreen mode

05 - Reverse a String (By letters & words)

let str = 'Hello World!';
console.log(str.split('').reverse().join('')); 
console.log(str.split(' ').reverse().join(' '));

// '!dlroW olleH'
// 'World! Hello'
Enter fullscreen mode Exit fullscreen mode

06 - Truncate a String to a Specific Length

const truncate = (str, len) => str.slice(0, len) + '...';
console.log(truncate("Lorem ipsum is simply dummy text of the printing and typesetting industry.", 15));

// 'Lorem ipsum is...'
Enter fullscreen mode Exit fullscreen mode

07 - Short-Circuit Evaluation

const fullName = fname || 'John Doe';
console.log(fullName);

// 'John Doe'
Enter fullscreen mode Exit fullscreen mode

08 - Remove a Specific Value from an Array

const toRemove = 5;
const newArr = nums.filter(val => val !== toRemove);
console.log(newArr);

// [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

09 - Swap Two Variables

[a, b] = [b, a];
Enter fullscreen mode Exit fullscreen mode

10 - Toggle a Boolean Value

const toggle = bool => !bool;
Enter fullscreen mode Exit fullscreen mode

11 - Generate a Random String

const randomString = () => Math.random().toString(36).slice(2);
console.log(randomString());

// 'eqsdasfefsa'  // Example output
Enter fullscreen mode Exit fullscreen mode

12 - Detect Dark Mode

const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').match;

Enter fullscreen mode Exit fullscreen mode

13 - Scroll to the Top of the Page

const scrollTop = () => window.scrollTo(0, 0);
Enter fullscreen mode Exit fullscreen mode

14 - Replace switch with Object Literals

// Traditional way 👇
const getAnimalName = (animalType) => {
    switch (animalType) {
    case 'lion':
        return '🦁';
    case 'elephant':
        return '🐘';
    case 'tiger':
        return '🐅';
    default:
        return 'Unknown';
    }
};

// Using object literals 👇
const animalNames = {
  lion: '🦁',
  elephant: '🐘',
  tiger: '🐅',
};

const getAnimalName = animalType => animalNames[animalType] || 'Unknown';
console.log(getAnimalName('lion'));

// '🦁'
Enter fullscreen mode Exit fullscreen mode

These one-liners offer quick solutions to common JavaScript tasks, helping you write cleaner, more efficient code. Give them a try!

💖 💪 🙅 🚩
durvesh_danve
Durvesh Danve

Posted on August 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related