10 Must-Know Javascirpt One-liners to Simplify Your Code

Javascirpt One-liners

JavaScript, known for its flexibility, offers a variety of powerful and concise solutions to common problems. One-liners are a great way to make your code more efficient and readable, especially for routine tasks. In this post, we will explore 10 must-know Javascirpt One-liners that can simplify your code, save time, and reduce unnecessary complexity.

1. Check if a Number is Even or Odd

You can check if a number is even or odd using a simple one-liner with the modulo operator.

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

				
			

This one-liner returns true if the number is even and false if it’s odd.


2. Swap Two Variables Without a Temporary Variable

A quick one-liner to swap two variables without needing a temporary third variable.

				
					[a, b] = [b, a];

				
			

This destructuring assignment simplifies swapping two values in JavaScript.

3. Create an Array of Numbers from 1 to n

Want to generate an array from 1 to n? Use this one-liner:

				
					const array = [...Array(n).keys()].map(i => i + 1);
				
			

This will create an array with values from 1 to n.

4. Remove Duplicates from an Array

To quickly remove duplicates from an array, you can use the Set object and spread syntax.

				
					const uniqueArray = [...new Set(arr)];
				
			

This one-liner efficiently removes duplicates and returns an array of unique elements.

5. Reverse a String

Reversing a string in JavaScript can be easily achieved in one line.

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

This breaks the string into an array of characters, reverses them, and joins them back into a string.

6. Flatten a Multi-Dimensional Array

Flattening a nested array is simple with the flat() method.

				
					const flatArray = arr.flat(Infinity);
				
			

Using .flat(Infinity), this one-liner flattens an array of any depth.

7. Get a Random Item from an Array

To get a random element from an array, you can use this one-liner:

				
					const randomItem = arr => arr[Math.floor(Math.random() * arr.length)];
				
			

This provides a quick way to pick a random element from any array.

8. Capitalize the First Letter of a String

Capitalizing the first letter of a string can be done in just one line.

				
					const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);
				
			

This one-liner converts the first character to uppercase and appends the rest of the string.

9. Check for a Palindrome

Checking whether a string is a palindrome can be simplified using this concise one-liner.

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

This compares the original string with its reversed version to check if they’re identical.

10. Calculate the Sum of an Array

Summing up all numbers in an array is easily done with the reduce() method.

				
					const sum = arr.reduce((acc, val) => acc + val, 0);
				
			

This one-liner adds up all the elements in an array and returns the total sum.

Conclusion:

Using JavaScript one-liners can significantly reduce the length of your code, making it more concise and easier to maintain. The key is to strike a balance between brevity and readability, ensuring your one-liners remain understandable. With these 10 must-know JavaScript one-liners, you can simplify everyday coding tasks and improve the overall efficiency of your code.