You are on page 1of 76

01/76

75 Useful
JavaScript Code
Snippets for Your
Next Project

SWIPE

Sajan Kota
02/76

Generate a random string


We can use Math.random() to generate a random
alphanumeric string, it’s very convenient when we need a
unique ID.

SWIPE
Sajan Kota
03/76

Generate a random string of


a given length

SWIPE
Sajan Kota
04/76

Copy content to the clipboard

SWIPE

Sajan Kota
05/76

Clear all cookies

SWIPE

Sajan Kota
06/76

Get the selected text

SWIPE

Sajan Kota
07/76

Scroll to the top of the page

SWIPE
Sajan Kota
08/76

Check whether the user has


scrolled to the bottom of a
page

SWIPE

Sajan Kota
09/76

Find out whether the current


tab is active

SWIPE
Sajan Kota
10/76

Redirect the user to a URL

SWIPE
Sajan Kota
11/76

Open the browser print box

SWIPE

Sajan Kota
12/76

Generate a random boolean


value

SWIPE

Sajan Kota
13/76

Generate a random number


between two values

SWIPE
Sajan Kota
14/76

Check if a given number is an


integer

SWIPE

Sajan Kota
15/76

Remove duplicate values in


an array

SWIPE
Sajan Kota
16/76

Get the unique values in an


array

SWIPE
Sajan Kota
17/76

Check if a variable is an array

SWIPE
Sajan Kota
18/76

Check if the date is Weekend

SWIPE

Sajan Kota
19/76

Calculate number of days


between two dates

SWIPE

Sajan Kota
20/76

Capitalize a String

SWIPE

Sajan Kota
21/76

Get the day of the year from


a date

SWIPE

Sajan Kota
22/76

Check if a string is a
palindrome

SWIPE

Sajan Kota
23/76

Get the first n elements of an


array

SWIPE

Sajan Kota
24/76

Get the last n elements of an


array

SWIPE

Sajan Kota
25/76

Remove all vowels from a


string

SWIPE

Sajan Kota
26/76

Check if a string contains a


substring

SWIPE
Sajan Kota
27/76

Get the current time in


hh:mm:ss format

SWIPE
Sajan Kota
28/76

Check if an object is empty

SWIPE

Sajan Kota
29/76

Checks if the provided array is


not empty

SWIPE

Sajan Kota
30/76

Find the max value in an array

SWIPE

Sajan Kota
31/76

Get the current date and time

SWIPE

Sajan Kota
32/76

Reverse a String

SWIPE

Sajan Kota
33/76

Sort Arrays

SWIPE

Sajan Kota
34/76

Extract the Domain name


from an email

SWIPE

Sajan Kota
35/76

Flatten an Nested Array

SWIPE

Sajan Kota
36/76

Generates a Random Color in


Hexadecimal Format

SWIPE

Sajan Kota
37/76

Checks if a given value is a


valid Hexadecimal Color Code

SWIPE

Sajan Kota
38/76

Get the current time in a


specific timezone

SWIPE

Sajan Kota
39/76

Convert a String to kebab-


case

SWIPE

Sajan Kota
40/76

Shuffle an Array

SWIPE

Sajan Kota
41/76

Shuffle an array using the


Fisher-Yates (Knuth) Shuffle
algorithm

SWIPE
Sajan Kota
42/76

Convert RGB color code to


valid Hexadecimal color code

SWIPE
Sajan Kota
43/76

Truncate a number to a fixed


decimal point

SWIPE
Sajan Kota
44/76

Remove falsy values from


Array

SWIPE
Sajan Kota
45/76

A function that toggles a


boolean value

SWIPE
Sajan Kota
46/76

Swapping Two Variables

A concise way to swap the values of


two variables using array

SWIPE
Sajan Kota
47/76

Merging multiple arrays using


Concatenation
The concat() method returns a new array
containing elements from both arr1 and
arr2.

SWIPE
Sajan Kota
48/76

Merging multiple arrays using


Spread Operator

The spread operator is concise and


visually clear. It takes the elements from
both arrays and creates a new array.

SWIPE
Sajan Kota
49/76

Merging multiple arrays using


push() and apply()
This approach modifies arr1 in place by
using push() along with apply() to add
elements from arr2.

SWIPE
Sajan Kota
50/76

Merging multiple arrays using


push and Spread Operator

The spread operator is used to expand the


elements of arr2 into individual arguments
for the push() method.

SWIPE
Sajan Kota
51/76

Merging multiple arrays using


concat() with Spread
Operator
The spread operator is used to
expand the elements of arr2 as
arguments to the concat() method.

SWIPE
Sajan Kota
52/76

Get the actual type of


JavaScript primitives

SWIPE
Sajan Kota
53/76

Truncate string at the end


The function will truncate the input string to
the specified length and add an ellipsis (...) at
the end if the string is longer than the specified
length.

SWIPE
Sajan Kota
54/76

Truncate string from the


middle
The function is to truncate the input string
while keeping the specified number of
characters from the start and end, and adding
an ellipsis (...) in the middle.

SWIPE
Sajan Kota
Get the value of a 55/76

browser cookie by its


name

SWIPE
Sajan Kota
56/76

Get the time (HH:MM:SS)


from a JavaScript Date
object.

SWIPE
Sajan Kota
57/76

Check if a number is even or odd

SWIPE
Sajan Kota
58/76

Converts the first character


of a given string to lowercase

SWIPE
Sajan Kota
59/76

Repeats a given string a


specific number of times

SWIPE
Sajan Kota
60/76

Check if the code is running


in Node.js

SWIPE
Sajan Kota
61/76

Check if the code is running


in the browser

SWIPE
Sajan Kota
62/76

Get all siblings of a given


element

SWIPE
Sajan Kota
63/76

Go back to the previous page


using the history object

SWIPE
Sajan Kota
64/76

Function to get the largest


element in an array

SWIPE
Sajan Kota
65/76

Function to get the smallest


element in an array

SWIPE
Sajan Kota
66/76

Function to toggle the


visibility of an HTML element

SWIPE
Sajan Kota
67/76

Function to remove HTML


tags from a given String

SWIPE
Sajan Kota
68/76

Define a function to check if a


given value is a DOM Element

SWIPE
Sajan Kota
69/76

Define a function to check if a


given string is a valid URL

SWIPE
Sajan Kota
70/76

Check if a string starts with a


given prefix

SWIPE
Sajan Kota
71/76

Check if a string ends with a


given suffix

SWIPE
Sajan Kota
72/76

Check if a value is an object

SWIPE
Sajan Kota
73/76

Check if a value is a function

SWIPE
Sajan Kota
74/76

Check if a value is a Promise

SWIPE
Sajan Kota
75/76

Check if an HTML element is


entirely within the viewport

SWIPE
Sajan Kota
76/76

Which of these
JavaScript
code snippets
do you use in
your projects?

Sajan Kota

You might also like