JavaScript Function Cheatsheet

Javascript allows to declare functions in various ways. This article provides a quick overview of each type.
Simple function definition
function hello() { console.log("hello") } hello(); // Output: hello
Anonymous function expression
const hello = function() { console.log("hello") } hello(); // Output: hello
Named function expression
const hello = function hello1() { console.log("hello") } hello() // Output: hello
Arrow function (always anonymous)
const hello = () => { console.log("hello") } hello(); // Output: hello
Single line arrow function
const hello = () => console.log("hello") hello(); // Output: hello const hello = () => "hello" console.log(hello()); // Output: hello const obj = () => {a: "1", b: "2"} // Error const obj = () => ({a: "1", b: "2"}) // Returns object
Anonymous callback
setTimeout(function () { console.log("hello"); }) // Output: hello
Named callback
setTimeout(function hello() { console.log("hello"); }) // Output: hello
Arrow function callback
setTimeout(() => { console.log("hello") }) // Output: hello
IIFE(Immediately Invoked Function Expression)
( function() { console.log("hello") } )(); // Output: hello
Named IIFE
( function hello() { console.log("hello") } )(); // Output: hello
Arrow function IIFE
( () => { console.log("hello") } )(); // Output: hello
Default parameters function
function hello(name = "User") { console.log("hello, ", name) } hello() hello("Demo User") // Output // hello, User // hello, Demo User
Arguments Object (Only in non-arrow functions)
function hello() { for(let i=0;i<arguments.length;i++) { console.log(arguments[i]) } } hello(1, 2, 3, "Demo"); /* Output: 1 2 3 Demo */
Rest parameters (At the end of parameters list, included in arguments object)
function hello(name, ...params) {
console.log(name);
params.map(item => console.log(item))
}
hello("Demo", 1, 2, 3)
/* Output:
Demo
1
2
3
*/
Higher-Order Functions (HOF)
Either takes function as argument
or returns function as result
Inbulit HOFs: map, reduce, filter, etc.
function name() {
return "Demo"
}
function greet(callback) {
console.log("Hello", callback());
}
greet(name);
// Output: Hello Demo
function outer() {
return function inner() {
console.log("hello")
}
}
// const outer = () => () => console.log("hello")
outer()();
// Output: hello
Arrow vs Regular functions
Arrow Function | Regular Function |
Arguments object is not available. | Arguments object is available. |
Can not be used as a constructor. | Can be used as constructor. |
Not hoisted | Hoisted |
Short and clean structure | Longer syntax |
Useful for callbacks, small utility functions | General purpose |
Anonymous vs Named functions
Anonymous Function | Named Function |
No identifier, usually assigned to a variable to reuse | Has identifier to reuse |
Difficult to debug, shows anonymous in call stack | Easy to debug |
Not hoisted | Hoisted |
Can’t be called recursively | Can be called recursively |
Callbacks, expressions, IIFE | General purpose |
Subscribe to my newsletter
Read articles from Payal Rathee directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
