DEV Community

Cover image for Streamlining JavaScript Development: Unleash Efficiency with these 18 Pro-Level One-Liners
Pawan Kumar
Pawan Kumar

Posted on

Streamlining JavaScript Development: Unleash Efficiency with these 18 Pro-Level One-Liners

Introduction:

JavaScript, with its expressive syntax and functional capabilities, allows developers to write concise and powerful code. One-liners, short snippets of code that achieve a specific task in a single line, showcase the elegance and efficiency of JavaScript. In this article, we'll explore 18 pro-level one-liners that can help you write more expressive and concise code.

1. Flatten an Array:
const flatArray = nestedArray => [].concat(...nestedArray);

2. Remove Duplicates from an Array:
const uniqueArray = array => [...new Set(array)];

3. Get Random Element from an Array:
const getRandomElement = array => array[Math.floor(Math.random() * array.length)];

4. Sum Array Elements:
const sumArray = array => array.reduce((acc, val) => acc + val, 0);

5. Filter Falsy Values from an Array:
const truthyArray = array => array.filter(Boolean);

6. Check if Array Contains a Specific Value:
const hasValue = (array, value) => array.includes(value);

7. Generate an Array of Numbers:
const generateArray = n => [...Array(n).keys()];

8. Swap Variables without Temporary Variable:
let a = 1, b = 2;
[a, b] = [b, a];

9. Convert String to Integer Array:
const intArray = string => string.split(',').map(Number);

10. Create an Object from Arrays:
const createObject = (keys, values) => Object.fromEntries(keys.map((key, i) => [key, values[i]]));

- Count Occurrences of Values in an Array:
const countOccurrences = array => array.reduce((acc, val) => (acc[val] = (acc[val] || 0) + 1, acc), {});

- Capitalize the First Letter of Each Word:
const capitalizeWords = sentence => sentence.replace(/\b\w/g, char => char.toUpperCase());

- Reverse a String:
const reverseString = string => [...string].reverse().join('');

- Check if String is a Palindrome:
const isPalindrome = string => string === string.split('').reverse().join('');

- Generate a Random Hex Color:
const randomHexColor = () => #${Math.floor(Math.random()*16777215).toString(16)};

- Destructure an Object with Default Values:
const { name = 'John', age = 25 } = person;

- Get Unique Values from Two Arrays:
const uniqueValues = (arr1, arr2) => [...new Set([...arr1, ...arr2])];

- Calculate Factorial:
const factorial = n => n > 1 ? n * factorial(n - 1) : 1;

Conclusion:

Mastering one-liners in JavaScript not only demonstrates your proficiency but also contributes to writing more efficient and readable code. The examples provided above showcase the versatility and expressiveness of JavaScript, allowing you to elevate your coding game and tackle common tasks with elegance and precision.

For more insights into JavaScript development and other coding techniques, visit CodeWithPawan.com. Incorporate these one-liners into your codebase judiciously, understanding their functionality, and leverage them to streamline your JavaScript development workflow. Happy coding!

Top comments (0)