DEV Community

Cover image for 14 JavaScript One-Liners to Boost Your Productivity ๐Ÿš€
Durvesh Danve
Durvesh Danve

Posted on

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!

Top comments (2)

Collapse
 
fsabre profile image
FSabre • Edited

The 10th oneliner does not toggle a boolean, it merely inverts it (and you might not want a whole function to do that).

Collapse
 
durvesh_danve profile image
Durvesh Danve

It actually does.
Inverting a boolean value and toggling a boolean value is one and the same