DEV Community

Cover image for 20 Killer JavaScript One-Liners That’ll Save You Hours of Coding 🤯🔥
Ram Maheshwari ♾️
Ram Maheshwari ♾️

Posted on • Edited on

20 Killer JavaScript One-Liners That’ll Save You Hours of Coding 🤯🔥

Take your JavaScript skills to the next level with these essential one-liners that will also save you hours of coding 🚀


1) Find the max value in an array:

Math.max(...array)
Enter fullscreen mode Exit fullscreen mode

2) Remove duplicates from an array:

[...new Set(array)]
Enter fullscreen mode Exit fullscreen mode

3) Generate a random number between 1 and 100:

Math.floor(Math.random() * 100) + 1
Enter fullscreen mode Exit fullscreen mode

4) Check if a string is a valid number:

!isNaN(parseFloat(string))
Enter fullscreen mode Exit fullscreen mode

5) Get the current date and time:

new Date().toString()
Enter fullscreen mode Exit fullscreen mode

6) Check if a variable is an array:

Array.isArray(variable)
Enter fullscreen mode Exit fullscreen mode

7) Check if a variable is an object:

typeof variable === "object"
Enter fullscreen mode Exit fullscreen mode

8) Convert an array to a string:

array.join(",")
Enter fullscreen mode Exit fullscreen mode

9) Check if a variable is a function:

typeof variable === "function"
Enter fullscreen mode Exit fullscreen mode

10) Convert an object to an array:

Object.values(object)
Enter fullscreen mode Exit fullscreen mode

11) Count the occurrences of an element in an array:

array.filter(x => x === element).length
Enter fullscreen mode Exit fullscreen mode

12) Create a new object with a dynamic key and value:

{ [key]: value }
Enter fullscreen mode Exit fullscreen mode

13) Check if a string is a palindrome:

string === string.split("").reverse().join("")
Enter fullscreen mode Exit fullscreen mode

14) Get the the sum of all the numbers in an array

array.reduce((a, b) => a + b, 0));
Enter fullscreen mode Exit fullscreen mode

15) Get the current timestamp:

Date.now()
Enter fullscreen mode Exit fullscreen mode

16) Check if a variable is null:

variable === null
Enter fullscreen mode Exit fullscreen mode

17) Check if a variable is undefined:

typeof variable === "undefined"
Enter fullscreen mode Exit fullscreen mode

18) Find the minimum value in an array

Math.min(...array)
Enter fullscreen mode Exit fullscreen mode

19) Check if an array is empty:

array.length === 0
Enter fullscreen mode Exit fullscreen mode

20) Create a new array with a specified range of numbers:

Array.from({ length: n }, (_, i) => i)
Enter fullscreen mode Exit fullscreen mode

Hope this is helpful ✨

Do Like ❤️ & Save 🔖


Do 𝗙𝗼𝗹𝗹𝗼𝘄 me on Linkedin for more:
Tips💡+ Guides📜 + Resources ⚡ related to Programming and Web Development 👨‍💻

Ram Maheshwari (@rammcodes) Linkedin


Do Follow me here on dev.to ✅

Latest comments (91)

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited

You don't need parseFloat with isNaN, to check if value is a number. it will be converted to number before the check:

!isNaN('100') == true
!isNaN('hello') == false
Enter fullscreen mode Exit fullscreen mode

There is Number.isNaN that don't do the conversion.

Collapse
 
flyingcrp profile image
flyingCrp

Image description

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Somewhat weirdly, this post appears to have changed authors? 🤔 @thepracticaldev

Collapse
 
rammcodes profile image
Ram Maheshwari ♾️ • Edited

Yeah, I don't know how it got removed from my account and got transferred into this account @thepracticaldev , maybe some bug?

Collapse
 
myrlandnu profile image
Jørn André Myrland

Hey, you forgot this one:

!!~text.indexOf(query)
Enter fullscreen mode Exit fullscreen mode

I use this one all the time.

Collapse
 
ra1nbow1 profile image
Matvey Romanov

Thanks a lot :)

Collapse
 
iikitty profile image
iikitty

4) Check if a string is a valid number:

!isNaN(parseFloat(string))
Enter fullscreen mode Exit fullscreen mode

This one is not working properly

const string = '1234.5678';
!isNaN(parseFloat(string)); // true
!isNaN(string);             // true
Enter fullscreen mode Exit fullscreen mode
const string = '1234あ5678';
!isNaN(parseFloat(string)); // true
!isNaN(string);             // false
Enter fullscreen mode Exit fullscreen mode
const string = '1234.5678.9';
!isNaN(parseFloat(string)); // true
!isNaN(string);             // false
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jimmonte profile image
jimmonte

Starting at the left end of the string after skipping any whitespace characters, parseFloat() only returns NaN if the first characters do not form a float number. The function stops parsing at the character that ends the number and returns the number found to that point.

Collapse
 
iikitty profile image
iikitty

This one works better & properly.

!isNaN(+string)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
chinmayj93 profile image
Chinmay Joshi • Edited

How can you forget the most valuable code while debugging - console.log("asdasdasd");

Collapse
 
zeeh1975 profile image
zeeh1975

I like 20, very clever!

Collapse
 
gaoryrt profile image
gaoryrt

20) Create a new array with a specified range of numbers:

[...Array(n).keys()]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nameandnums profile image
FiftyP

Why does #3 seem 'smelly' to me?

"Generate a random number between 1 and 100:
Math.floor(Math.random() * 100) + 1"

Math.random() => 0 to 1 (NOT including 1) => [0, 1)
Math.random()*100. => 0 to 100 (NOT including 100) => [0, 100)
Math.floor(Math.random()*100) => int(0 to 100) (NOT including 100) => [0, 100)
Math.floor(Math.random()*100)+1 => int(1 to 101) (NOT including 101) => [1, 101)

so, the Math.floor(Math.random()*100)+1 CAN include 100, so is NOT '1 to 100 (not including 100)', which would be expected from all the other descriptions of 'A to B' which DON'T include the upper bound.
IMO, the description "between" is (1, 100), but the functions used seem to be [a,b), so the user might also expect [1,100), but they actually get [1,100] - or [1,101) since they're only integers.

Collapse
 
nameandnums profile image
FiftyP

found = false;
count = 0;
while (!found) {
const value = Math.floor(Math.random() * 100) + 1;
found = value === 100;
count++;
}
197