4 Ways to Declare a Function in JavaScript

4 Ways to Declare a Function in JavaScript


Published at - Oct 27, 2021

I’ll discuss “4 ways to define a function in JavaScript” in this brief blog article.

1. Function Declaration

As seen in the example below, this is the most frequent technique to define a function.

The benefit of this is that you may utilize the function even if it is declared at the bottom of your code.

function addTwoNumbers(num1, num2) {

    return num1 + num2;

}

console.log(addTwoNumbers(1,10));

// outputs: 11

2. Function Expression

A function expression is a function in which a function is assigned to a variable. Before using a function that is assigned to a variable, it must be defined first.

console.log(addTwoNumbers(1,10)); // Error, becayse cant find addTwo Numbers

const addTwoNumbers = function (num1, num2) {

    return num1 + num2;

}

console.log(addTwoNumbers(1,10));

// outputs: 11

3. Arrow Function Expression

Photo by [Tim Mossholder](https://unsplash.com/@timmossholder?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)Photo by Tim Mossholder on Unsplash

This function is similar to a functional expression, except that instead of a writing function, we use arrows =>.

const addTwoNumbers = (num1, num2) => {

    return num1 + num2;

}

console.log(addTwoNumbers(1,10));

// outputs: 11

4. Concise Arrow Function Expression

Concise Arrow Function Expression is a function that allows you to return immediately without having to write a return statement. Note: This only works if it returns a value directly.

const addTwoNumbers = (num1, num2) => num1 + num2;

console.log(addTwoNumbers(1,10));

// outputs: 11

Thank you for reading this article, Don’t forget to follow me.





About author

Harendra
Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.



Related Posts -

VS Code Extensions to Boost Your Productivity 3X
VS Code Extensions to Boost ...

VS Code Extensions to Boost Your Productivity 3X List of top VS Code extens...



Top Tech Skills To Learn In 2022
Top Tech Skills To Learn In ...

Top Tech Skills To Learn In 2022 A list of the best talents you can master ...



What Does It Take to Be a Successful Web Developer?
What Does It Take to Be a S ...

Good day, developers! I’m creating this post to share what I’v...



10 Awesome JavaScript One-Liners
10 Awesome JavaScript One-L ...

Good day, everyone! I have 5 years of experience as a full-stack web develo...



JavaScript (The Language of the Web) Cheat Sheet - Beginner’s essential
JavaScript (The Language of ...

JavaScript is a cross-platform, object-oriented scripting language used to ...



Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile