How to Declare a Function in JavaScript?

Estimated read time 1 min read

You can declare a function in JavaScript using the function keyword followed by the function name, a list of parameters in parentheses (), and the function body enclosed in curly braces {}. The function body contains the statements that are executed when the function is called.

Here’s an example of a simple function that takes two arguments and returns their sum

function add(a, b) {
  return a + b;
}

You can also declare a function using an anonymous function expression and assign it to a variable:

let add = function(a, b) {
  return a + b;
};

And you can also use arrow function syntax introduced in ES6:

let add = (a, b) => {
  return a + b;
};

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply