🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

A JavaScript Cheat Sheet: A Reference Guide of Functions

Reading time 3 min
Published Oct 2, 2019
Updated Apr 25, 2024

A

alert Shows a modal window that has a specific message and an OK button.
array.length Returns the length of an array
array.map Invokes a function for every element of an array (in order)
array.push Adds elements to an array and declares a new length
array.sort Sorts array elements in either alphabetic or ascending, and descending or ascending order

B

boolean Returns either true or false based on the specified statement or condition
break and continue Break stops a loop from running. Continue skips an iteration of a loop

C

className Sets or returns the name of a class of an HTML element
confirm Displays a specified message in a dialog box, containing OK and CANCEL buttons

D

decodeURIComponent Decodes URI components

I

if Runs code when a condition is met
indexOf Searches the array for a specified item, then returns its position
innerHTML Sets or returns the HTML content inside an element

M

Math Performs mathematical calculations
Math.random Generates a random number from 0 (inclusive) up to but not including 1 (exclusive)

N

Number Converts object arguments into numbers
number.toString Converts a number to a string

O

onclick An event that occurs when the user clicks on an element
onload Transpires when an object is loaded

R

replace Searches a string for a regular expression or a specified value and returns a string with replaced specified values

S

setAttribute Adds a specified attribute to an element, giving the attribute a certain value
setInterval Executes a specified function multiple times at set time intervals specified in milliseconds (1000ms = 1second)
setTimeout Calls a function after the time specified in milliseconds (1000 ms = 1 second) has passed
slice Returns selected elements of an array into a new array object
splice Adds or removes items from an array
string.includes Determines whether a string contains characters of a specified string or not
string.indexOf Returns the position of the first occurrence of a specified value inside a string variable
string.split Splits a data string into a substrings array and returns the new array
style.display Sets or returns the display type of a specified element
submit Submits an HTML form
substr Extracts parts of a string
substring Extracts the symbols between two indexes and returns a data string
switch Executes actions for various conditions

F

for Runs a piece of code a set number of times
forEach Applies a specified function for every item in a particular array individually

L

location.reload Reloads the current web page

P

parseInt Converts a string to an integer
prompt Displays a dialog box, which would require a person to type in input

T

test Looks for a match inside a string variable
throw, try and catch The try and catch block lets you set an error handling code: try defines a code of block that will be tested for errors, and catch defines a code of block that handles the error
toLowerCase Converts all letters in a specified string lowercase
toUpperCase Converts all letters in a specified string to uppercase

U

use strict Sets JavaScript code to be executed in strict mode

W

while Executes a block of code as long as the defined condition is true
window.history Holds information about internet browsers history
window.location Gets information about the current address (URL)
window.navigator Contains the information about the user's browser
window.screen Holds the information about the screen of the browser (for instance, the screen width and height)