This content originally appeared on DEV Community and was authored by Lawaanu Borthakur
Here is the full list of mostly used JavaScript inbuilt functions along with their definitions and usage examples, categorized for easy reading.
ARRAYS
const arr = [1, 2, 3];
// 1. push() – Adds element to the end
arr.push(4); // [1,2,3,4]
// 2. pop() – Removes last element
arr.pop(); // [1,2,3]
// 3. shift() – Removes first element
arr.shift(); // [2,3]
// 4. unshift() – Adds element at the start
arr.unshift(1); // [1,2,3]
// 5. concat() – Merges arrays
arr.concat([4, 5]); // [1,2,3,4,5]
// 6. slice() – Extracts a section (non-destructive)
arr.slice(0, 2); // [1,2]
// 7. splice() – Adds/removes items (destructive)
arr.splice(1, 1); // removes 1 element at index 1
// 8. indexOf() – Finds index of an element
arr.indexOf(3); // returns index or -1
// 9. filter() – Filters based on a condition
arr.filter(x => x > 1);
// 10. map() – Transforms each element
arr.map(x => x * 2);
// 11. Array.from() – Converts iterable/string to array
Array.from('abc'); // ['a','b','c']
// 12. toString() – Converts array to comma string
arr.toString(); // "1,3"
// 13. includes() – Checks for existence
arr.includes(3); // true
// 14. sort() – Sorts array
arr.sort(); // in-place sort
// 15. reverse() – Reverses array
arr.reverse();
// 16. join() – Joins into a string
arr.join('-'); // "1-3"
// 17. find() – Finds first matching element
arr.find(x => x > 1);
// 18. forEach() – Loops through elements
arr.forEach(x => console.log(x));
// 19. every() – Checks if all elements meet condition
arr.every(x => x > 0);
// 20. some() – Checks if any meet condition
arr.some(x => x === 3);
STRINGS
let str = "hello";
// 1. charAt() – Gets character by index
str.charAt(1); // 'e'
// 2. toUpperCase() – Uppercase
str.toUpperCase();
// 3. toLowerCase() – Lowercase
str.toLowerCase();
// 4. indexOf() – First occurrence index
str.indexOf('l');
// 5. lastIndexOf() – Last occurrence index
str.lastIndexOf('l');
// 6. substring() – Slice string between indexes
str.substring(1, 4);
// 7. replace() – Replace part of string
str.replace('e', 'a');
// 8. split() – Split into array
str.split(''); // ['h','e','l','l','o']
// 9. trim() – Removes whitespace
" hello ".trim(); // "hello"
OBJECTS
const obj = { a: 1, b: 2 };
// 1. Object.keys() – Array of keys
Object.keys(obj);
// 2. Object.values() – Array of values
Object.values(obj);
// 3. Object.entries() – Array of [key, value]
Object.entries(obj);
// 4. Object.assign() – Merge objects
Object.assign({}, obj);
// 5. Object.freeze() – Lock object (no change)
Object.freeze(obj);
// 6. Object.seal() – Prevent add/delete
Object.seal(obj);
// 7. hasOwnProperty() – Check own property
obj.hasOwnProperty('a');
// 8. Object.create() – Create with prototype
const newObj = Object.create(obj);
DATE
const date = new Date();
// 1. new Date() – Current date/time
// 2. getDate() – Day of the month
date.getDate();
// 3. getMonth() – Month (0-11)
date.getMonth();
// 4. getFullYear() – Year
date.getFullYear();
// 5. getHours(), getMinutes(), getSeconds()
date.getHours();
date.getMinutes();
date.getSeconds();
// 6. toISOString() – ISO string
date.toISOString();
// 7. toLocaleDateString() – Localized date
date.toLocaleDateString();
// 8. getTime() – ms since 1970
date.getTime();
MATH
// 1. Math.abs() – Absolute value
Math.abs(-5);
// 2. Math.round() – Round to nearest
Math.round(4.5);
// 3. Math.floor() – Round down
Math.floor(4.9);
// 4. Math.ceil() – Round up
Math.ceil(4.2);
// 5. Math.max(), Math.min()
Math.max(1, 2, 3);
Math.min(1, 2, 3);
// 6. Math.pow(), Math.sqrt()
Math.pow(2, 3); // 8
Math.sqrt(16); // 4
// 7. Math.random() – 0 to 1
Math.random();
// 8. Math.PI
Math.PI;
SET
const s = new Set();
// 1. add(), delete(), has()
s.add(1);
s.delete(1);
s.has(1);
// 2. size – Count
s.size;
// 3. clear() – Remove all
s.clear();
// 4. forEach() – Iterate
s.forEach(val => console.log(val));
// 5. values(), entries()
[...s.values()];
[...s.entries()];
MAP
const m = new Map();
// 1. set(), get(), has(), delete()
m.set('a', 1);
m.get('a');
m.has('a');
m.delete('a');
// 2. size – Count
m.size;
// 3. clear()
m.clear();
// 4. forEach(), keys(), values(), entries()
m.forEach((v, k) => console.log(k, v));
[...m.keys()];
[...m.values()];
[...m.entries()];
LOOPS
const obj = { a: 1, b: 2 };
// 1. for...in – Loop over object keys
for (let key in obj) {
console.log(key);
}
// 2. for...of – Loop over iterable values
for (let val of [1, 2, 3]) {
console.log(val);
}
// 3. forEach – Loop over array items
[1, 2, 3].forEach(x => console.log(x));
This content originally appeared on DEV Community and was authored by Lawaanu Borthakur