JavaScript Cheat Sheet
- 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.
- Example: let num = 1.2345; num.toFixed(2); //”1.23”
- toString(): Converts a number to a string.
- Example: let num=123; num.toString(); //”123”
- parseInt(): Parses a string to an integer.
- Example: parseInt(“123”); //123
- parseFloat(): Parses a string to a floating-point number.
- Example: parseFloat(“12.34”); // 12.34
- isNaN(): Check if a value is NaN (Not-a-Number).
- Example: isNaN(“abc”); //true
- isInteger(): Checks if a value is an integer.
- Example: Number.isInteger(123); //true
- isFinite(): Checks if a value is a finite number.
- Example: isFinite(123); // true
- toExponential(): Converts a number to exponential notation.
- Example: let num = 123; num.toExponential(2); // “1.23e+2”
- toPrecision(): Formats a number to a specified length.
- Example: let num = 123; num.toPrecisioin(2); // “1.2e+2”
- valueOf(): Returns the primitive value of a number.
- Example: let num = 123; num.valueOf(); 123
JavaScript Object Methods
- keys(): Returns an array of an object’s keys.
- Example: let obj = {a: 1}; Object.keys(obj); // [“a”]
- values(): Returns an array of an object’s values.
- Example: let obj = {a: 1}; Object.values(obj); // [1]
- entries(): Returns an array of an object’s key-value pairs.
- Example: let obj = {a: 1}; Object.entries(obj); // [[“a”,1]]
- assign(): Copies values of all properties from one or more objects.
- Example: Object.assign({}, {a,1}, {b,2}); // {a: 1, b: 2}
- freeze(): Prevents modifications to an object properties.
- Example: let obj = {a: 1}; Object.freeze(obj);
- seal(): Prevents adding or removing properties, but allows modification.
- Example: let obj {a: 1}; Object.seal(obj);
- hasOwnProperty(): Checks if an object contains a specific property.
- Example: let obj = {a: 1}; obj.hasOwnProperty(“a”); // true
- is(): Compares if two values are the same.
- Example: Object.is(1,1); // true
- create(): Creates a new object with the specified prototype.
- Example: let obj = Object.create({a: 1});
- fromEntries(): Creates an object from an array of key-value pairs.
- Example: Object.fromEntries([[“a”, 1]]); // {a: 1}
- Example: Object.fromEntries([[“a”, 1]]); // {a: 1}
JavaScript Array Methods
- push(): Adds elements to the end of an array.
- Example: let arr = [1,2]; arr.push(3); // [1,2,3]
- pop(): Removes the last element from an array.
- Example: let arr = [1,2]; arr.pop(); // [1,2]
- shift(): Removes the first element from an array.
- Example: let arr = [1,2,3]; arr.shift(); [2,3]
- unshift(): Adds elements to the beginning of an array.
- Example: let arr = [2,3]; arr.unshift(1); // [1,2,3]
- splice(): Adds/removes elements from an array.
- Example: let arr = [1,2,3]; arr.splice(1,1); //[1,3]
- slice(): Returns a portion of an array.
- Example: let arr = [1,2,3]; arr.slice(1); // [2,3]
- concat(): Merges two or more arrays.
- Example: let arr1 = [1]; let arr2 = [2]; arr1.concat(arr2); // [1,2]
- indexOf(): Returns the first index of a value.
- Example:let arr = [1,2,3]; arr.indexOf(2); // 1
- includes(): Checks if an array contains a value.
- Example: let arr = [1,2,3]; arr.includes(2); // true
- forEach(): Executes a function on each array element.
- Example: let arr = [1,2]; arr.forEach(x => console.log(x));
- map(): Creates a new array with the results of a function.
- Example: let arr = [1,2]; arr.map(x => x*2); // [2,4]
- filter(): Creates a new array with elements that pass a test.
- Example: let arr = [1,2,3]; arr.filter(x => x>2); // [2,3]
- reduce(): Reduces array to a single value using a function.
- Example: let arr = [1,2]; arr.reduce((a,b) => a+b); // 2
- find(): Returns the first element that passes a test.
- Example: let arr = [1,2,3]; arr.find(x => x>1); //
- findIndex(): Return the index of the first element that passes a test.
- Example: let arr = [1,2,3]; arr.findIndex(x => x>1); // 1
- sort(): Sorts array elements in place.
- Example: let arr = [3,1,2]; arr.sort(); //[1,2,3]
- reverse(): Reverses the order of array elements.
- Example: let arr = [1,2,3]; arr.reverse(); [3,2,1]
- join(): Joins array elements into a string.
- Example: let arr = [1,2,3]; arr.join(‘-’); “1-2-3”
- every(): Checks if all elements pass a test.
- Example: let arr = [1,2,3]; arr.every(x => x>0); // true
- some(): Checks if any element passes a test.
- Example: let arr = [1,2,3]; arr.some(x => x>2); // true
- Example: let arr = [1,2,3]; arr.some(x => x>2); // true
JavaScript String Method
- charAt(): Returns the character at a specific index.
- Example: let str = “abc”; str.charAt(1); // “b”
- concat(): Combines two or more strings.
- Example: let str = “Hello”; str.concat(“ world”); // “Hello world”
- includes(): Check if a string contains a specific value.
- Example: let str = “Hello”; str.includes(“ell); // true
- indexOf(): Returns the index of the first occurrence of a value.
- Example: let str = “Hello”; str.indexOf(“e”); //2
- slice(): Extracts a part of a string.
- Example: let str = “Hello”; str.slice(1,3); // “el”
- split(): Splits a string into an array of substrings.
- Example: let str = “a,b,c”; str.split(“,”); // [“a”, “b”, “c”]
- toLowerCase(): Converts a string to lowercase.
- Example: let str = “Hello”; str.toLowerCase(); // hello
- toUpperCase(): Converts a string to uppercase.
- Example: let str = “hello”; str.toUpperCase(); //HELLO
- trim(): Removes whitespace from both ends of a string.
- Example: let str = “ hello ”; str.trim(); // “hello”
- replace(): Replaces a substring with a new value.
- Example: let str = “Hello”; str.repalce(“e”,”a”); // “Hallo”
- Example: let str = “Hello”; str.repalce(“e”,”a”); // “Hallo”