JavaScript One-Liners You (Probably) Didn't Know

  Published:

JavaScript is a powerful language with many features that can help simplify complex tasks. When used to its full potential, JavaScript debugging techniques can drastically improve your coding experience while also making your applications more efficient.

So, here are 10 one-liners that you may not know about:

Array.from(document.querySelectorAll('selector')) 

This one-liner creates an array from all the elements that match the specified selector in the DOM. It's an easy way to manipulate multiple elements at once.


Object.keys(obj)

This one-liner returns an array of all the keys in an object. It's useful for iterating over an object's properties.


array.filter(Boolean) 

This one-liner filters out all the false values in an array. It's a quick way to remove empty or null values.


string.trim()

This one-liner removes all whitespace characters from the beginning and end of a string. It's useful for cleaning up user input.


JSON.stringify(obj)

This one-liner converts an object to a JSON string. It's helpful for sending data to a server or saving data to a file.


array.includes(item)

This one-liner checks if an array contains a specific item. It's a simple way to search for a value in an array.


string.replace(searchValue, replaceValue)

This one-liner replaces all instances of a search value in a string with a replace value. It's an easy way to make mass edits to a string.


array.reduce((accumulator, currentValue) => accumulator + currentValue)

This one-liner adds up all the values in an array. It's useful for calculating totals or averages.


setInterval(() => console.log('Hello'), 1000)

This one-liner runs a function repeatedly at a set interval. It's a simple way to create a timer or animate an element.


array.sort((a, b) => a - b)

This one-liner sorts an array in ascending order. It's helpful for organizing data in a consistent way.

Now, here's a bonus 10 one-liners you (probably) didn't know:

Array.from({ length: 5 }, (_, i) => i + 1)

This one-liner creates an array with a specified length and fills it with values generated by a function. It's a great way to create an array of numbers or objects with a specific structure.


Object.values(obj)

This one-liner returns an array of all the values in an object. It's useful for extracting data from an object in a structured way.


array.flat() 

This one-liner flattens an array by removing all nested arrays. It's useful for working with multidimensional arrays or for removing empty values.


string.split('').reverse().join('')

This one-liner reverses a string by splitting it into an array, reversing the order of the elements, and then joining them back together. It's a quick way to reverse a string without using a loop.


Math.max(...array)

This one-liner returns the largest value in an array. It's a simple way to find the maximum value in an array without having to loop through it.


new Set(array)

This one-liner creates a new Set object from an array, removing all duplicate values. It's a quick way to remove duplicates from an array without using a loop.


string.padStart(num, char)

This one-liner adds a specified character to the beginning of a string until it reaches a certain length. It's useful for formatting numbers or strings in a specific way.


array.splice(startIndex, deleteCount, ...items)

This one-liner removes or adds elements to an array at a specified index. It's a quick way to modify an array without having to loop through it.


Object.freeze(obj)

This one-liner freezes an object, preventing any changes to its properties. It's useful for ensuring that an object's properties cannot be modified accidentally.


string.replaceAll(searchValue, replaceValue)

This one-liner replaces all instances of a search value in a string with a replace value. It's similar to the replace() method, but it replaces all instances instead of just the first one.


These one-liners may not be as well-known as some of the more common JavaScript methods, but they can still be incredibly useful for simplifying your code and making it more efficient. By incorporating these techniques into your code, you can improve your coding experience and create more powerful and effective applications.