JavaScript Cheat Sheet
A quick and easy JavaScript cheat sheet covering syntax, functions, arrays, objects, DOM, and ES6 features with simple examples for beginners and developers.
By netrapal Singh · · 5 min read
Variables: Store data values.
Function: Reusable blocks of code.
Objects: Collection of key-value pairs.
Arrays: Ordered lists of items.
Loops: Repeat actions multiple times.
Conditionals: Make decisions in code.
ES6: New JavaScript features.
Arrow Functions: Shorter function syntax.
Promises: Handle asynchronous operations.
Async / Await: Syntactic sugar for promises.
DOM: Interface to interact with HTML elements.
Event Listeners: React to user actions.
Hoisting: Variable and function declarations moved to the top.
Closures: Functions with preserved scope.
Scope: Access to variables and functions.
Callbacks: Functions passed as arguments.
IIFE: Function that executes immediately.
Prototype: Mechanism for object inheritance.
Classes: Blueprint for creating objects.
Modules: Reusable code units.
Spread Operator: Expand arrays or objects.
Destructuring: Extract values from arrays or objects.
Template Literals: String interpolation with backticks.
Type Coercion: Automatic type conversion.
Strict Mode: Enforce stricter parsing and error handling.
Map: Object with key-value pairs.
Set: Collection of unique values.
WeakMap: Map with weakly held keys.
WeakSet: Set with weakly held values.
Symbol: Unique and immutable data type.
Generators: Functions that can be paused and resumed.
Proxy: Custom behavior for basic operations.
Reflect: Methods to interact with JavaScript objects.
JSON: Format for storing and transporting data.
Fetch API: Modern way to make HTTP requests.
LocalStorage: Store data in the browser.
SessionStrorage: Temporary storage in the browser.
Web Workers: Run scripts in background threads.
Promise.all: Wait for multiple promises.
Promise.race: Resolve with the first settled promise.
BigInt: Large integer representation.
Nullish Coalescing: Handle null or undefined values.
Optional Chaining: Safe access to nested properties.
Intl: Internationalization API.
Error Handling: Manager errors in code.
Event Bubbling: Event propagation through DOM.
Debouncing: Limit the rate of function execution.
Throttling: Ensure function execution at set intervals.
Memoization: Optimize function performance.
Garbage Collection: Automatic memory management.
JavaScript Number Methods
toFixed(): Formats a number to fixed decimal places.
let num = 1.2345; num.toFixed(2); //”1.23”toString(): Converts a number to a string.
let num=123; num.toString(); //”123”parseInt(): Parses a string to an integer.
parseInt(“123”); //123parseFloat(): Parses a string to a floating-point number.
parseFloat(“12.34”); // 12.34isNaN(): Check if a value is NaN (Not-a-Number).
isNaN(“abc”); //trueisInteger(): Checks if a value is an integer.
Number.isInteger(123); //trueisFinite(): Checks if a value is a finite number.
isFinite(123); // truetoExponential(): Converts a number to exponential notation.
let num = 123; num.toExponential(2); // “1.23e+2”toPrecision(): Formats a number to a specified length.
let num = 123; num.toPrecisioin(2); // “1.2e+2”valueOf(): Returns the primitive value of a number.
let num = 123; num.valueOf(); 123
JavaScript Object Methods
keys(): Returns an array of an object’s keys.
let obj = {a: 1}; Object.keys(obj); // [“a”]values(): Returns an array of an object’s values.
let obj = {a: 1}; Object.values(obj); // [1]entries(): Returns an array of an object’s key-value pairs.
let obj = {a: 1}; Object.entries(obj); // [[“a”,1]]assign(): Copies values of all properties from one or more objects.
Object.assign({}, {a,1}, {b,2}); // {a: 1, b: 2}freeze(): Prevents modifications to an object properties.
let obj = {a: 1}; Object.freeze(obj);seal(): Prevents adding or removing properties, but allows modification.
let obj {a: 1}; Object.seal(obj);hasOwnProperty(): Checks if an object contains a specific property.
let obj = {a: 1}; obj.hasOwnProperty(“a”); // trueis(): Compares if two values are the same.
Object.is(1,1); // truecreate(): Creates a new object with the specified prototype.
let obj = Object.create({a: 1});fromEntries(): Creates an object from an array of key-value pairs.
Object.fromEntries([[“a”, 1]]); // {a: 1}
JavaScript Array Methods
push(): Adds elements to the end of an array.
let arr = [1,2]; arr.push(3); // [1,2,3]pop(): Removes the last element from an array.
let arr = [1,2]; arr.pop(); // [1,2]shift(): Removes the first element from an array.
let arr = [1,2,3]; arr.shift(); [2,3]unshift(): Adds elements to the beginning of an array.
let arr = [2,3]; arr.unshift(1); // [1,2,3]splice(): Adds/removes elements from an array.
let arr = [1,2,3]; arr.splice(1,1); //[1,3]slice(): Returns a portion of an array.
let arr = [1,2,3]; arr.slice(1); // [2,3]concat(): Merges two or more arrays.
let arr1 = [1]; let arr2 = [2]; arr1.concat(arr2); // [1,2]indexOf(): Returns the first index of a value.
let arr = [1,2,3]; arr.indexOf(2); // 1includes(): Checks if an array contains a value.
let arr = [1,2,3]; arr.includes(2); // trueforEach(): Executes a function on each array element.
let arr = [1,2]; arr.forEach(x => console.log(x));map(): Creates a new array with the results of a function.
let arr = [1,2]; arr.map(x => x*2); // [2,4]filter(): Creates a new array with elements that pass a test.
let arr = [1,2,3]; arr.filter(x => x>2); // [2,3]reduce(): Reduces array to a single value using a function.
let arr = [1,2]; arr.reduce((a,b) => a+b); // 2find(): Returns the first element that passes a test.
let arr = [1,2,3]; arr.find(x => x>1);findIndex(): Return the index of the first element that passes a test.
let arr = [1,2,3]; arr.findIndex(x => x>1); // 1sort(): Sorts array elements in place.
let arr = [3,1,2]; arr.sort(); //[1,2,3]reverse(): Reverses the order of array elements.
let arr = [1,2,3]; arr.reverse(); [3,2,1]join(): Joins array elements into a string.
let arr = [1,2,3]; arr.join(‘-’); “1-2-3”every(): Checks if all elements pass a test.
let arr = [1,2,3]; arr.every(x => x>0); // truesome(): Checks if any element passes a test.
let arr = [1,2,3]; arr.some(x => x>2); // true
JavaScript String Method
charAt(): Returns the character at a specific index.
let str = “abc”; str.charAt(1); // “b”concat(): Combines two or more strings.
let str = “Hello”; str.concat(“ world”); // “Hello world”includes(): Check if a string contains a specific value.
let str = “Hello”; str.includes(“ell); // trueindexOf(): Returns the index of the first occurrence of a value.
let str = “Hello”; str.indexOf(“e”); //2slice(): Extracts a part of a string.
let str = “Hello”; str.slice(1,3); // “el”split(): Splits a string into an array of substrings.
let str = “a,b,c”; str.split(“,”); // [“a”, “b”, “c”]toLowerCase(): Converts a string to lowercase.
let str = “Hello”; str.toLowerCase(); // hellotoUpperCase(): Converts a string to uppercase.
let str = “hello”; str.toUpperCase(); //HELLOtrim(): Removes whitespace from both ends of a string.
let str = “ hello ”; str.trim(); // “hello”replace(): Replaces a substring with a new value.
let str = “Hello”; str.repalce(“e”,”a”); // “Hallo”