JavaScript Important Cheat Sheet
[ JavaScript Built-in Methods ]
1. Array Method
● forEach(): array.forEach(element => console.log(element));
● map(): const squares = array.map(x => x * x);
● filter(): const evens = array.filter(x => x % 2 === 0);
● reduce(): const sum = array.reduce((acc, curr) => acc + curr, 0);
● find(): const found = array.find(x => x > 10);
● indexOf(): const index = array.indexOf(item);
● push(): array.push(item);
● pop(): const lastItem = array.pop();
● shift(): const firstItem = array.shift();
● unshift(): array.unshift(item);
● splice(): array.splice(startIndex, deleteCount, item);
● slice(): const sliced = array.slice(startIndex, endIndex);
● concat(): const mergedArray = array1.concat(array2);
● join(): const str = array.join(‘, ‘);
● includes(): const hasItem = array.includes(item);
● some(): const has
egative = array.some(x => x < 0);
● every(): const allflositive = array.every(x => x > 0);
● sort(): array.sort((a, b) => a — b);
● reverse(): array.reverse();
2. String Methods
● charAt(): const char = str.charAt(index);
● concat(): const combinedStr = str1.concat(str2);
● includes(): const hasSubstring = str.includes(substring);
● indexOf(): const index = str.indexOf(substring);
● lastIndexOf(): const lastIndex = str.lastIndexOf(substring);
● match(): const matches = str.match(regex);
● repeat(): const repeatedStr = str.repeat(times);
● replace(): const replacedStr = str.replace(regex, newSubstr);
● search(): const index = str.search(regex);
● slice(): const slicedStr = str.slice(startIndex, endIndex);
split(): const tokens = str.split(delimiter);
substring(): const substring = str.substring(startIndex, endIndex);
● toLowerCase(): const lowerStr = str.toLowerCase();
● toUpperCase(): const upperStr = str.toUpperCase();
● trim(): const trimmedStr = str.trim();
3. Object Methods
● Object.keys(): const keys = Object.keys(obj);
● Object.values(): const values = Object.values(obj);
● Object.entries(): const entries = Object.entries(obj);
● Object.assign(): const newObj = Object.assign({}, obj1, obj2);
● Object.freeze(): const frozenObj = Object.freeze(obj);
● Object.isFrozen(): const isFrozen = Object.isFrozen(obj);
● Object.seal(): const sealedObj = Object.seal(obj);
● Object.isSealed(): const isSealed = Object.isSealed(obj);
● Object.create(): const newObj = Object.create(prototypeObj);
● Object.hasOwnflroperty(): const hasflrop = obj.hasOwnflroperty(prop);
4.
Number Methods
●
umber.parseInt(): const intVal =
umber.parseInt(string, radix);
●
umber.parseFloat(): const floatVal =
umber.parseFloat(string);
●
umber.is
a
(): const is
a
=
umber.is
a
(value);
●
umber.isFinite(): const isFinite =
umber.isFinite(value);
●
umber.isInteger(): const isInteger =
umber.isInteger(value);
●
umber.toFixed(): const fixedStr = num.toFixed(digits);
5. Math Functions
● Math.abs(): const absolute = Math.abs(num);
● Math.ceil(): const roundedUp = Math.ceil(num);
● Math.floor(): const roundedDown = Math.floor(num);
● Math.round(): const rounded = Math.round(num);
● Math.max(): const max = Math.max(num1, num2, …);
● Math.min(): const min = Math.min(num1, num2, …);
● Math.pow(): const power = Math.pow(base, exponent); Math.sqrt(): const sqrt = Math.sqrt(num); Math.random(): const random
um = Math.random();
6. Global Functions
● parseInt(): const intVal = parseInt(string, radix);
● parseFloat(): const floatVal = parseFloat(string);
● is
a
(): const is
a
= is
a
(value);
● isFinite(): const isFinite = isFinite(value);
7. Date Methods
● Date.now(): const now = Date.now();
● Date.parse(): const timestamp = Date.parse(dateString);
Get Sohrab Ali Ansari’s stories in your inbox
Join Medium for free to get updates from this writer.
● new Date(): const date = new Date(year, month, day, …);
8. Regular Expression Methods
● RegExp.test(): const matchesflattern = regex.test(string);
● RegExp.exec(): const match = regex.exec(string);
9.
JSO N Methods
● JSO
.stringify(): const jsonString = JSO
.stringify(obj);
● JSO
.parse(): const obj = JSO
.parse(jsonString);
10. Web AflIs
● setTimeout(): const timeoutId = setTimeout(() => { /* code */ }, delay);
● clearTimeout(): clearTimeout(timeoutId);
● setInterval(): const intervalId = setInterval(() => { /* code */ }, delay);
● clearInterval(): clearInterval(intervalId);
11. flromises and Asynchronous flrogramming
● flromise.then(): promise.then(value => { /* code */ });
flromise.catch(): promise.catch(error => { /* code */ });
flromise.finally(): promise.finally(() => { /* code */ });
● flromise.all(): flromise.all([promise1, promise2]).then(values => { /* code */ });
● flromise.race(): flromise.race([promise1, promise2]).then(value => {
/* code */ });
● async function: async function fetchData() { const data = await fetch(url); }
12. Console Methods
● console.log(): console.log(‘message’, obj);
● console.warn(): console.warn(‘warning message’); ● console.error(): console.error(‘error message’);
● console.info(): console.info(‘info message’);
● console.clear(): console.clear();
13. Miscellaneous Methods
● alert(): alert(‘Alert message’);
● confirm(): const confirmed = confirm(‘Are you sureo’);
● prompt(): const userInput = prompt(‘Enter your name:’, ‘Default
ame’);
● encodeURIComponent(): const encoded = encodeURIComponent(uriComponent);
● decodeURIComponent(): const decoded = decodeURIComponent(encodedUriComponent);
14. DOM Manipulation (Web-Specific)
● document.getElementById(): const element = document.getElementById(‘id’);
● document.querySelector(): const element = document.querySelector(‘.class’);
● document.querySelectorAll(): const elements = document.querySelectorAll(‘div’);
● document.createElement(): const div = document.createElement(‘div’); element.appendChild(): parentElement.appendChild(childElement); element.innerHTML: element.innerHTML = ‘<span>
ew content</span>’;
● element.addEventListener(): element.addEventListener(‘click’, eventHandler);
● element.style: element.style.color = ‘blue’;
● element.setAttribute(): element.setAttribute(‘data-custom’, ‘value’);
● element.removeAttribute(): element.removeAttribute(‘data-custom’);
15. Event Handling
● element.onclick: element.onclick = function() { /* code */ };
● element.addEventListener(): element.addEventListener(‘click’, eventHandler);
● element.removeEventListener(): element.removeEventListener(‘click’, eventHandler);
● event.preventDefault(): element.addEventListener(‘click’, event => event.preventDefault());
16. BOM (Browser Object Model) Methods
● window.open(): const newWindow = window.open(url);
● window.close(): window.close();
● window.moveTo(): window.moveTo(x, y);
● window.resizeTo(): window.resizeTo(width, height);
17. Web Storage AflI
● localStorage.setItem(): localStorage.setItem(‘key’, ‘value’);
● localStorage.getItem(): const value = localStorage.getItem(‘key’);
● sessionStorage.setItem(): sessionStorage.setItem(‘key’, ‘value’);
● sessionStorage.getItem(): const value = sessionStorage.getItem(‘key’);
18. Fetch AflI and XMLHttpRequest
● fetch(): fetch(url).then(response => response.json());
● XMLHttpRequest(): const xhr = new XMLHttpRequest(); xhr.open(‘GET’, url); xhr.send();
19. History AflI
● history.pushState(): history.pushState(stateObj, ‘title’, ‘page2.html’);
● history.replaceState(): history.replaceState(stateObj, ‘title’, ‘page3.html’);
● history.back(): history.back();
20. WebSockets
● WebSocket: const socket = new WebSocket(‘ws://example.com’);
21. File and FileReader AflI
● FileReader.readAsDataURL(): const reader = new FileReader(); reader.readAsDataURL(file);
● FileReader.onload: reader.onload = function() { console.log(reader.result); };
22. Geolocation AflI
● navigator.geolocation.getCurrentflosition(): navigator.geolocation.getCurrentflosition(position => console.log(position));
● navigator.geolocation.watchflosition(): const watchID = navigator.geolocation.watchflosition(position => console.log(position));
23. flerformance AflI
● performance.now(): const start = performance.now();
24. Request Animation Frame
● requestAnimationFrame(): requestAnimationFrame(timestamp => { /* animations */ });
24.Internationalization
● Intl.DateTimeFormat(): const formatter = new Intl.DateTimeFormat(‘en-US’); console.log(formatter.format(date));
● Intl.
umberFormat(): const numberFormatter = new Intl.
umberFormat(‘en-US’); console.log(numberFormatter.format(number));
25. Canvas AflI
● getContext(): const ctx = canvas.getContext(‘2d’);
26. Drag and Drop AflI
● ondragstart: element.ondragstart = function(event) { /* code */ };
● ondrop: dropArea.ondrop = function(event) { /* code */ };
27. Mutation Observer AflI
● MutationObserver(): const observer = new MutationObserver(callback); observer.observe(target
ode, config);
28. Screen AflI
● screen.width: const screenWidth = screen.width;
● screen.height: const screenHeight = screen.height;
29.
avigator AflI
● navigator.userAgent: const userAgent = navigator.userAgent;
● navigator.onLine: const isOnline = navigator.onLine;









