20+ Handy JavaScript Functions to Simplify Your Code | JavaScript Tutorial

Rahul Sharma (DevsMitra)
3 min readMay 12, 2024
20+ Handy JavaScript Functions to Simplify Your Code | JavaScript Tutorial

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)…

--

--

Rahul Sharma (DevsMitra)
Rahul Sharma (DevsMitra)

Written by Rahul Sharma (DevsMitra)

I’m a technology enthusiast who does web development. Passionate to contribute to open-source projects and make cool products.

Responses (2)