Example 1 — Multiple parameters
Pass more than one argument.
JavaScript
function add(a, b) { return a + b; }
console.log(add(2, 3));How It Works
- a and b receive 2 and 3.
- return sends 5 back to the caller.
A function declaration creates a reusable block of code with a name. You call it later with parentheses. Declarations are hoisted, so you can call the function before its line in the same scope.
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Ada"));Pass more than one argument.
function add(a, b) { return a + b; }
console.log(add(2, 3));Declarations can be called earlier in the same scope.
console.log(double(4));
function double(n) { return n * 2; }Expressions are not hoisted the same way.
const f = function (n) { return n + 1; };
console.log(f(1));CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful