Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 27, 2023 09:32 am GMT

20 Killer JavaScript One-Liners Thatll 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)

2) Remove duplicates from an array:

[...new Set(array)]

3) Generate a random number between 1 and 100:

Math.floor(Math.random() * 100) + 1

4) Check if a string is a valid number:

!isNaN(parseFloat(string))

5) Get the current date and time:

new Date().toString()

6) Check if a variable is an array:

Array.isArray(variable)

7) Check if a variable is an object:

typeof variable === "object"

8) Convert an array to a string:

array.join(",")

9) Check if a variable is a function:

typeof variable === "function"

10) Convert an object to an array:

Object.values(object)

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

array.filter(x => x === element).length

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

{ [key]: value }

13) Check if a string is a palindrome:

string === string.split("").reverse().join("")

14) Create a new array with unique values:

[...new Set(array)]

15) Get the current timestamp:

Date.now()

16) Check if a variable is null:

variable === null

17) Check if a variable is undefined:

typeof variable === "undefined"

18) Create a new array with no duplicates:

[...new Set(array)]

19) Check if an array is empty:

array.length === 0

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

Array.from({ length: n }, (_, i) => i)

Hope this is helpful

Do Like & Save

Do me on Linkedin for more:
Tips+ Guides + Resources related to programming and Web Development

Do Follow me here on dev.to


Original Link: https://dev.to/rammcodes/20-killer-javascript-one-liners-thatll-save-you-hours-of-coding-o14

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To