In JavaScript, functions can also be defined using expressions, known as function expressions. These functions can be anonymous (unnamed) and are assigned to variables.
functionExpressions.js
let add = function(a, b){ return a + b; } let a = 5; let b = 3; let sum = add(5, 3); console.log(`Sum of ${a} and ${b} is ${sum}`);
Output
Sum of 5 and 3 is 8
What Does This Function Do?
1. Stores the Function: The function is not named directly but is assigned to the variable add. This means you can call the function using the variable name add.
2. Adds Two Numbers: When you call add, you pass two arguments to it, which correspond to the parameters a and b. The function will add these two numbers together and return the sum.
No comments:
Post a Comment