Difference between JavaScript Function Declaration and JavaScript Expression

JavaScript functions allow us to execute numerous operations, make important decisions, carry out calculations, and improve the interactivity of our web pages. In this article, we will explore the differences between "function declaration" and "function expression." Both concepts utilize the keyword function, which is their commonality. The key difference lies in the fact that a function declaration includes a name for the function, whereas a function expression does not possess a designated name.

Declaration of Function

  • A function is declared using a function keyword in a function declaration, also referred to as a function statement.
  • A function name is required in the function declaration.
  • Functions are stand-alone structures that are not grouped inside functional blocks, so function declarations do not require variable assignments.
  • They run ahead of all other codes.
  • The function in the function declaration is accessible Both before and after the function definition.

Syntax

The syntax outlined below is employed for declaring a function.

Example

functionName(parameter1, prameter2){
//write functions code
}

Example:

The subsequent example illustrates the JavaScript syntax used for declaring an arithmetic operation.

Example

<!DOCTYPE html>
<html>
<head>
<title> Function expression in javascript </title>
</head>
<body style = "background-color:beige;">
<h2> difference between javascript Function expression and javascript function declaration </h2>
<h4> JavaScript Anonymous Function declaration </h4>
<p> We can create and declare the function without the function name. We can use the let variable for the function expression. </p>
<b id = "data_view"> </b>
<br>
<b id = "data_view1"> </b>
<script>
//create the variable with the 'let' keyword for a function expression
function cal_Substraction (x, y) {
let c = x - y;
return c;
}
document.getElementById("data_view").innerHTML = "Subtraction value: " + cal_Substraction(10, 4);
function cal_add(x, y) {
let c = x + y;
return c;
}
document.getElementById("data_view1").innerHTML = "Addition value: " + cal_add(10, 4);
</script>
</body>
</html>

Output:

The output presents the definition of the function along with its execution in a non-identified manner.

Function Expression

  • Without the function name, a function expression is comparable to a function declaration.
  • Variable assignments allow for the storage of function expressions.
  • Function expressions load and run once the program interpreter gets to the code line.
  • Only after the function definition is the function in the function expression accessible.

Syntax:

The syntax provided below is utilized for a function expression.

Example

Var function_variable = function(parameter1, prameter2){
//write functions code
}

Example:

The subsequent example demonstrates a JavaScript expression.

Example

<!DOCTYPE html>
<html>
<head>
<title> Function expression in javascript </title>
</head>
<body style = "background-color:beige;">
<h2> difference between javascript Function expression and javascript function declaration </h2>
<h4> JavaScript Anonymous Function Expression </h4>
<p> We can create and declare the function without the function name. We can use the let variable for the function expression. </p>
<b id = "data_view"> </b>
<br>
<b id = "data_view1"> </b>
<script>
//create the variable with the 'let' keyword for a function expression
let cal_Substraction = function (x, y) {
let c = x - y;
return c;
}
document.getElementById("data_view").innerHTML = "Subtraction value: " + cal_Substraction(10, 4);
let cal_add = function (x, y) {
let c = x + y;
return c;
}
document.getElementById("data_view1").innerHTML = "Addition value: " + cal_add(10, 4);
</script>
</body>
</html>

Output:

The output demonstrates the function expression in an unnamed format along with its execution.

Comparison between Function Declaration and Expression

The table below illustrates the distinctions between function declarations and function expressions.

Function Declaration Function Expression
A function declaration needs a function name for the coding. A function expression works the same as a function declaration but doesn't need the function name.
The declaration of a function does not need the assignment of variables. Variable assignments allow for the storage of function expressions.
They run ahead of all other codes. Function expressions load and run once the program interpreter gets to the code line.
It is possible to access the function in the function declaration both before and after the function definition. Only after the function definition is the function in the function expression accessible.
Declarations of functions are raised. Expressions for functions are not lifted.
SyntaxFunction_name(parameter1, prameter2){//write functions code} SyntaxVar function_variable = function(parameter1, prameter2){//write functions code}

Conclusion

The declaration and expression of functions in JavaScript play a crucial role in defining various functions and their operations. Both developers and students utilize function declarations and expressions based on specific functionalities and their individual needs.

Input Required

This code uses input(). Please provide values below: