Function Expression allows us to create an anonymous function which doesn’t have any function name which is the main difference between Function Expression and Function Declaration. A function expression can be used as an IIFE (Immediately Invoked Function Expression)which runs as soon as it is defined. A function expression has to be stored in a variable and can be accessed using variableName. With the ES6 features introducing Arrow Function, it becomes more easier to declare function expression.
Syntax for Function Declaration:
function functionName(x, y) { statements... return (z) };
Syntax for Function Expression (anonymous) :
let variableName = function(x, y) { statements... return (z) };
Syntax for Function Expression (named) :
let variableName = function functionName(x, y)
{ statements... return (z) };
Syantax for Arrow Function:
let variableName = (x, y) => { statements... return (z) };
Note:
- A function expression has to be defined first before calling it or using it as a parameter.
- An arrow function must have an return statement.
Below examples illustrate the funcion expression in JavaScript:
Example 1: Code for Function Declaration.
Javascript
<script> function callAdd(x, y){ let z = x + y; return z; } console.log("Addition : " + callAdd(7, 4)); </script> |
Output:
Addition : 11
Example 2: Code for Function Expression (anonymous)
Javascript
<script> var calSub = function(x, y){ let z = x - y; return z; } console.log("Subtration : " + calSub(7, 4)); </script> |
Output:
Subtration : 3
Example 3: Code for Function Expression (named)
Javascript
<script> var calMul = function Mul(x, y){ let z = x * y; return z; } console.log("Multiplication : " + calMul(7, 4)); </script> |
Output:
Multiplication : 28
Example 4: Code for Arrow Function
Javascript
<script> var calDiv = (x, y) => { let z = x / y; return z; } console.log("Division : " + calDiv(24, 4)); </script> |
Output:
Division : 6
Recommended Posts:
- Difference between function expression vs declaration in JavaScript
- JavaScript | function* expression
- JavaScript | async function expression
- How to prevent overriding using Immediately Invoked Function Expression in JavaScript ?
- JavaScript | RegExp (x|y) Expression
- JavaScript | class expression
- JavaScript | RegExp [abc] Expression
- JavaScript | RegExp [^abc] Expression
- JavaScript | RegExp [0-9] Expression
- JavaScript | RegExp [^0-9] Expression
- How to validate URL using regular expression in JavaScript?
- How to check for IP address using regular expression in javascript?
- How to detect whether a device is iOS without using Regular Expression in JavaScript?
- Convert user input string into regular expression using JavaScript
- JavaScript yield* expression
- JavaScript SyntaxError - Invalid regular expression flag "x"
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


