10 Bizarre JavaScript One-Liners That Will Blow Your Mind

10 Bizarre JavaScript One-Liners That Will Blow Your Mind

JavaScript is a powerful and versatile programming language, and part of what makes it so useful is the ability to write concise and expressive code. In this article, we’ll take a look at some unusual and lesser-known JavaScript one-liners that can help you write concise, efficient code.

  1. Reverse a string with the .split('').reverse().join('') method chain

Did you know that you can easily reverse a string in JavaScript using just one line of code? The key is to use the split(), reverse(), and join() methods. Here's an example:

const str = 'hello';
const reversedStr = str.split('').reverse().join('');
console.log(reversedStr); // outputs 'olleh'

The split() method is used to split the string into an array of individual characters. The reverse() method is called on the array, which reverses the order of the elements. Finally, the join() method is called on the reversed array, which combines the elements back into a string.

2. Check if a string is a palindrome with the .split('').reverse().join('') method chain

A palindrome is a word or phrase that is spelled the same forwards and backwards, such as “racecar” or “A man, a plan, a canal, Panama!”. You can use the same split(), reverse(), and join() method chain to check if a string is a palindrome:

const str = 'racecar';
const isPalindrome = str === str.split('').reverse().join('');
console.log(isPalindrome(racecar)); // outputs true

3. Check if an array includes a value with the includes() method

The includes() method can be used to check if an array includes a specific value. It returns a boolean value, so you can use it in a concise one-liner like this:

const arr = [1, 2, 3, 4, 5];
const includesThree = arr.includes(3);
console.log(includesThree); // outputs true

4. Generate a random integer with the Math.random() and Math.floor() methods

The Math.random() method returns a random float between 0 (inclusive) and 1 (exclusive). By multiplying this value by the maximum integer you want to generate, and then using the Math.floor() method to round down to the nearest integer, you can generate a random integer within a given range:

const max = 10;
const min = 1;
const randomInt = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(randomInt); // outputs a random integer between 1 and 10 (inclusive)

5. Swap variables with destructuring assignment

You can use destructuring assignment to easily swap the values of two variables:

let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a); // outputs 2
console.log(b); // outputs 1

6. Check if an object is empty with the Object.keys() method

You can use the Object.keys() method to get an array of an object's keys, and then check the length of that array to determine if the object is empty:

const obj = {};
const isEmpty = !Object.keys(obj).length;
console.log(isEmpty); // outputs true

7. Get the first element of an array with destructuring assignment

You can use destructuring assignment to easily get the first element of an array:

const arr = [1, 2, 3];
const [first] = arr;
console.log(first); // outputs 1

8. Get the last element of an array with the slice() method

You can use the slice() method to get the last element of an array like this:

const arr = [1, 2, 3];
const last = arr.slice(-1)[0];
console.log(last); // outputs 3

9. Generate a random color with the Math.random() and toString() methods

You can use the Math.random() method to generate a random float between 0 (inclusive) and 1 (exclusive), and then use the toString() method to convert that float to a string in hexadecimal format. Finally, you can use string concatenation to add the necessary # symbol to the beginning of the hex string:

const randomColor = '#' + Math.random().toString(16).slice(-6);
console.log(randomColor); // outputs a random color code in hexadecimal format

10. Check if an object has a specific property with the in operator

You can use the in operator to check if an object has a specific property:

const obj = {
  foo: 'hello',
  bar: 'world'
};
const hasFoo = 'foo' in obj;
console.log(hasFoo); // outputs true

These are just a few examples of the many weird and little-known JavaScript one-liners that are out there. With a little creativity and some knowledge of the built-in methods and features of the language, you can write concise and expressive code that gets the job done.