CheatSheets

JavaScript Cheat Sheet

JS SnippetDescription
console.log()Used to print a message to the console
document.getElementById()Used to select an element by its id
document.querySelector()Used to select an element by its CSS selector
document.createElement()Used to create a new HTML element
element.addEventListener()Used to attach an event listener to an element
element.appendChild()Used to add an element as a child of another element
element.innerHTMLUsed to access or set the HTML content of an element
element.styleUsed to access or set the styles of an element
element.classListUsed to add, remove, or toggle a class on an element
function foo()Used to define a function named foo
foo()Used to call the function foo
if (condition)Used to check if a condition is true
for (let i = 0; i < 10; i++)Used to create a loop that runs 10 times
const variable = valueUsed to declare a constant variable with a value
let variable = valueUsed to declare a variable with a value
array.push(element)Used to add an element to the end of an array
array.pop()Used to remove the last element from an array
array.shift()Used to remove the first element from an array
array.slice(start, end)Used to create a new array with elements from an existing array
array.map(callback)Used to create a new array by running a callback function on each element of an existing array
array.filter(callback)Used to create a new array with only the elements that pass a certain condition
array.reduce(callback)Used to reduce an array to a single value by running a callback function on each element
object.propertyUsed to access the value of a property on an object
object.property = valueUsed to set the value of a property on an object
object.method()Used to call a method on an object
string.indexOf('substring')Used to find the position of a substring in a string
string.replace('old', 'new')Used to replace a substring in a string with a new string
string.split('delimiter')Used to split a string into an array based on a delimiter
string.trim()Used to remove whitespace from the beginning and end of a string
string.toLowerCase()Used to convert a string to lowercase
string.toUpperCase()Used to convert a string to uppercase
parseInt(string)Used to convert a string to an integer
parseFloat(string)Used to convert a string to a floating-point number
JSON.stringify(object)Used to convert an object to a JSON string
JSON.parse(string)Used to convert a JSON string to an object
Math.random()Used to generate a random number between 0 and 1
Math.floor(number)Used to round a number down to the nearest integer
Math.ceil(number)Used to round a number up to the nearest integer
Math.round(number)Used to round a number to the nearest integer
Math.min(n1, n2, ...)Used to find the minimum value from a list of numbers
Math.max(n1, n2, ...)Used to find the maximum value from a list of numbers
Math.abs(number)Used to get the absolute value of a number
Math.pow(base, exponent)Used to calculate the power of a number
Math.sqrt(number)Used to calculate the square root of a number
Previous
Git Commands