-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-liners.js
61 lines (41 loc) · 1.31 KB
/
one-liners.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//JS One-liners
// Find the max value in an array:
Math.max(...array)
// Remove duplicates from an array:
[...new Set(array)]
// Generate a random number between 1 and 100:
Math.floor(Math.random() * 100) + 1
// Check if a string is a valid number:
!isNaN(parseFloat(string))
// Get the current date and time:
Date().toString()
// Check if a variable is an array:
Array.isArray(variable)
// Check if a variable is an object:
typeof variable === "object"
// Convert an array to a string:
array.join(",")
// Check if a variable is a function:
typeof variable === "function"
// Convert an object to an array:
Object.values(object)
// Count the occurrences of an element in an array:
array.filter(x => x === element).length
// Create a new object with a dynamic key and value:
{ [key]: value }
// Check if a string is a palindrome:
string === string.split("").reverse().join("")
// Get the the sum of all the numbers in an array
array.reduce((a, b) => a + b, 0));
// Get the current timestamp:
Date.now()
// Check if a variable is null:
variable === null
// Check if a variable is undefined:
typeof variable === "undefined"
// Find the minimum value in an array
Math.min(...array)
// Check if an array is empty:
array.length === 0
// Create a new array with a specified range of numbers:
Array.from({ length: n }, (_, i) => i)