Sitemap

JavaScript Functions — Function Definitions

1 min readAug 22, 2024

--

In JavaScript functions can be declared in multiple ways, Let’s have a look

Anonymous Functions and Function Expressions

Function without a name are called anonymous functions.

JavaScript allows declaration of anonymous functions and function expression where functions are defined with expression.

//This is a anonymous function 
function (){

}

There is no way to call the above declared function.

To use an anonymous function we can declare the function using expression.

const addNumbers = function (num1,num2){
return num1+num2;
}
console.log(addNumbers(2,3));

We will discuss more about function expression in call back functions.

Get Bollavaram Harshavardhan Reddy’s stories in your inbox

Join Medium for free to get updates from this writer.

Arrow functions

Arrow Functions are introduced in ES6, arrow functions provides a concise syntax for writing function expressions.

const addNumbers =()=>{
return num1+num2;
}
console.log(addNumbers(2,3));

In the above example function key word is replaced with an arrow =>

IIFE (Immediately invoked function expression)

A function can call itself in JavaScript

(function (){

console.log("Code Executed");

})();

When you execute the above code the anonymous function will execute automatically and print “Code Executed”.

--

--

Bollavaram Harshavardhan Reddy
Bollavaram Harshavardhan Reddy

Written by Bollavaram Harshavardhan Reddy

Software Engineer - 3+ years experienced