20+ Handy JavaScript Functions to Simplify Your Code | JavaScript Tutorial
3 min readMay 12, 2024
JavaScript is a functional programming language, and functions play a crucial role. They allow you to encapsulate reusable code and perform specific tasks. Here are some quick examples of functions that can make your life easier:
Regular function
function sum(a, b) {
return a + b;
}
Function expression
const sum = function (a, b) {
return a + b;
};
Arrow function
const sum = (a, b) => {
return a + b;
};
// OR
const sum = (a, b) => a + b;
Generator function
function* indexGenerator() {
let index = 0;
while (true) {
yield index++;
}
}
const g = indexGenerator();
console.log(g.next().value); // => 0
console.log(g.next().value); // => 1
Create an array of numbers from 1 to n
const range = (n) => Array.from({ length: n }, (_, i) => i + 1)…