Code has been added to clipboard!

A JavaScript Cheat Sheet: A Reference Guide of Functions

Reading time 3 min
Published Oct 2, 2019
Updated Jun 13, 2021


alert Shows a modal window that has a specific message and an OK button.
array.length Returns the length of an array 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


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


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


decodeURIComponent Decodes URI components


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


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


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


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


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


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


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


location.reload Reloads the current web page


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


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


use strict Sets JavaScript code to be executed in strict mode


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)

Web Development Course:

Web Development Course

Certificate Included
900h of content
For beginners