JavaScript one liners you should know

Using JavaScript one liners you can write clear, effective, and expressive code. 

One-liners are frequently used for simple and direct activities like filtering an array, working with a string, or computing a value or it's mostly useful when you want to achieve effective results using less line of code.

Javascript image

In this blog we are going to discuss about most commonly used javascript one liners that you should know if you want to use javascript effectively.

Convert a string to title case:

const titleCase = str => str.toLowerCase().replace(/\b(\w)/g, char => char.toUpperCase());

Check if a variable is an array:

const isArray = variable => Array.isArray(variable);

Flatten an array:

const flattenArray = arr => arr.flat();

Remove duplicates from an array:

const removeDuplicates = arr => [...new Set(arr)];

Sum all numbers in an array:

const sumArray = arr => arr.reduce((total, num) => total + num, 0);

Check if a string is a palindrome:

const isPalindrome = str => str === str.split('').reverse().join('');

Shuffle an array:

const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);

Get the current date in a specific format (e.g., "YYYY-MM-DD"):

const currentDate = () => new Date().toISOString().split('T')[0];

Generate a random integer between a specified range:

const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;

Convert a number to a string with commas as thousand separators:

const addCommas = num => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');

Get the maximum value from an array of numbers:

const maxNumber = arr => Math.max(...arr);

Get the minimum value from an array of numbers:

const minNumber = arr => Math.min(...arr);

Convert a string to title case:

const titleCase = str => str.toLowerCase().replace(/\b(\w)/g, char => char.toUpperCase());

Check if an object is empty:

const isEmptyObject = obj => Object.keys(obj).length === 0;

Get the current time in a specific format (e.g., "HH:MM:SS"):

const currentTime = () => new Date().toTimeString().slice(0, 8);

Reverse a string:

const reverseString = str => str.split('').reverse().join('');

Capitalize the first letter of each word in a string:

const capitalizeWords = str => str.replace(/\b\w/g, char => char.toUpperCase());

Check if a number is even:

const isEven = num => num % 2 === 0;

Check if a number is prime:

 const isPrime = num => {
  for(let i = 2, sqrt = Math.sqrt(num); i <= sqrt; i++) {
    if(num % i === 0) return false;
  }
  return num > 1;
 };

Find the average value of an array of numbers:

const average = arr => arr.reduce((total, num) => total + num, 0) / arr.length;

Remove falsy values from an array:

const removeFalsy = arr => arr.filter(Boolean);

Generate a random alphanumeric string of a specified length:

const generateRandomString = length => Math.random().toString(36).substring(2, length + 2);

Calculate the factorial of a number:

const factorial = num => num <= 1 ? 1 : num * factorial(num - 1);

Check if a number is even:

const objectToArray = obj => Object.entries(obj);
Thanks for reading this article, if you found this article useful then share it with your friends and share your thoughts in comment section.

Want to learn more about javascript? checkout these amazing  Javascript Tutorials .

Comments