You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letx;// Declare a block-scope variableconsty=2;// Declare a block-scope, read-only constant
Functions
functionmyFunction(){// Function declaration}constmyFunction=()=>{// Arrow function expression}
Conditional statements
if(condition){// Execute if condition is true}elseif(otherCondition){// Execute if otherCondition is true}else{// Execute if neither condition is true}
Loops
for(leti=0;i<10;i++){// Foor loop; repeats code while a condition is true}while(condition){// While loop; repeats code while a condition is true}do{// Executes code block at least onde, then repeats while a condition is true}while(condition);
Array methods
letarr=[1,2,3];arr.push(4);// Adds a new element to the end of the arrayarr.pop();// Removes the last element from the arrayarr.shift();// Removes the first element from the arrayarr.unshift(0);// Adds a new element to the beginning of the arrayarr.splice(2,1);// Removes a range of elements from the arrayarr.reverse();// Reverses the order of the elements in the arrayarr.sort();// Sorts the elements in the arrayarr.slice(2,5);// Returns a new array with a subset of the elements
Object methods
letobj={name: 'John',age: 30,};obj.keys(obj);// Returns an array of the object's keysobj.values(obj);// Returns an array of the object's valuesobj.entries(obj);// Returns an array of the object's key-value pairs
String methods
letstr='Hello, world!';str.length;// Returns the length of the stringstr.toUpperCase();// Converts the string to uppercasestr.toLowerCase();// Converts the string to lowercasestr.includes('o');// Checks if the string includes a specified substring
Dom manipulation
document.getElementById('myElement');// Selects an element by its IDdocument.querySelector('.myClass');// Selects the first element that matches a specified CSS selectordocument.querySelectorAll('.myClass');// Selects all elements that match a specified CSS selectordocument.createElement('div');// Creates a new element
Event listeners
element.addEventListener('click',function();// Attaches a click event listener to an element
JSON methods
JSON.stringify(obj);// Converts an object to a JSON stringJSON.parse(str);// Converts a JSON string to an object
Asynchronous operations
fetch(url).then(response=>response.json())// Performs a network request and returns a promise.then(data=>console.log(data)).catch(error=>console.error(error));// Catches any errors in the fetch operationasyncfunctionfetchData(){try{constresponse=awaitfetch(url);// Waits for the fetch operation to completeconstdata=awaitresponse.json();// Waits for the response to be parsed as JSONconsole.log(data);}catch(error){console.error(error);// Catches any errors in the fetch operation}}
Date and time
letnow=newDate();// Creates a new Date objectnow.getFullYear();// Returns the yearnow.getMonth();// Returns the month (0-11)now.getDate();// Returns the day of the monthnow.getHours();// Returns the hour (0-23)now.getMinutes();// Returns the minute (0-59)now.getSeconds();// Returns the second (0-59)now.getMilliseconds();// Returns the millisecond (0-999)
Math methods
Math.random();// Returns a random number between 0 and 1Math.floor(3.7);// Returns the largest integer less than or equal to the given numberMath.ceil(3.7);// Returns the smallest integer greater than or equal to the given numberMath.round(3.7);// Returns the nearest integer to the given numberMath.abs(-3.7);// Returns the absolute value of the given numberMath.sqrt(16);// Returns the square root of the given numberMath.pow(2,3);// Returns the result of raising 2 to the power of 3Math.max(1,3);// Returns the largest of the given numbersMath.min(1,3);// Returns the smallest of the given numbers
Error handling
try{// Attempts to execute code that may throw an error}catch(error){// Handle the error}finally{// Executes code regardless of whether an error was thrown or not}
Module Impots and Exports
exportconstmyFunction=()=>{/* ... */};// Exports a function from the current moduleimport{myFunction}from'./myModule';// Imports a function from another module
JS Class
classPerson{constructor(name,age){// Constructor method for initializing an objectthis.name=name;this.age=age;}sayHello(){// Method that prints a greeting messageconsole.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);}}