# Function Declaration vs Function Expression in JavaScript: A Beginner’s Guide

In this article, we'll explore the difference between function declarations and function expressions in JavaScript. First, let's define a function: it's a reusable block of code you can call multiple times. For example, instead of rewriting the same logic to add two numbers, multiply them, or compute a power every time, you can put that logic in a function and reuse it. Rewriting the same code breaks the DRY (Don't Repeat Yourself) principle, so we use functions to avoid repetition and make code easier to maintain.

Changes made

*   Rewrote the opening sentence from "In this article, we are going to a journey on the understanding of the function declaration and expression." to "In this article, we'll explore the difference between function declarations and function expressions in JavaScript." — fixes grammar and clarifies scope.
    
*   Replaced "A function is a reusable piece of code which is use again and again." with "it's a reusable block of code you can call multiple times." — improves grammar and concision.
    
*   Replaced the informal/unclear example "Let me give you an assignment write a program in which you have add two number, multiple number and power of the same function" with a concise, correct example: "For example, instead of rewriting the same logic to add two numbers, multiply them, or compute a power..." — fixes grammar and clarifies intent.
    
*   Condensed repetitive sentences about rewriting code into a single clear statement about the DRY principle.
    
*   Replaced "DO NOT REPEAT YOURSELF principle" with "DRY (Don't Repeat Yourself) principle" — uses the standard acronym and proper casing.
    
*   Improved final sentence from "that is why we have function." to "so we use functions to avoid repetition and make code easier to maintain." — clearer and more professional.
    
*   Fixed general punctuation, capitalization, and spacing issues throughout.
    

### Function

A function is a reusable piece of code which is use again and again. Let me give you an assignment write a program in which you have add two number, multiple number and power of the same function

```javascript
let num1 = 10;
let num2 = 5

const result = num1 + num2

const result2 = num1 * num2

const result3 = Math.pow(num1, num2)
```

In this code if we need to you use addition. we need to write it again and again. for now this code is just a one line code but suppose if the code is of 100 line so you have to write that piece of code again and again . And at this point of time you break a biggest principle of the programming which is DO NOT REPEAT YOURSELF principle. that is why we have function.

### Function Declaration

Declaration simply mean define function with the `function` keyword and write the logic of the function inside the codeblock. Function declaration are hoisted. A function declaration has a function Name.

```plaintext
function functionName() { set of Instruction OR function logic }
```

some of the example of the function

```javascript
// this is function declaration

function addTwoNum(num1, num2){
    return num1 + num2
}

function mulTwoNum(num1, num2){
    return num1 * num2
}

function powerNum(num1, num2){
    return Math.pow(num1, num2)
}

const result = addTwoNum(10, 20)
result = mulTwoNum(10, 14)
result = powerNum(16, 4)
```

* * *

### Function Expression

A function expression is declare with the variable name and cannot be hoisted and it is also declared with the function keyword but does not have a function name.

```plaintext
const variableName = function (param1, param2) {
    statement of the code 
}
```

some of the example are listed here

```javascript
const add = function(num1, num2) {
    console.log(num1 + num2)
}

add(5, 4)
```

* * *

### Function Declaration V/S Function Expression

| **Function Declaration** | **Function Expression** |
| --- | --- |
| A function declaration must have a function name. | A function expression is similar to a function declaration without the function name. |
| Function declaration does not require a variable assignment. | Function expressions can be stored in a variable assignment. |
| These are executed before any other code. | Function expressions load and execute only when the program interpreter reaches the line of code. |
| The function in function declaration can be accessed before and after the function definition. | The function in function expression can be accessed only after the function definition. |
| Function declarations are hoisted | Function expressions are not hoisted |
| **Syntax:** function geeksforGeeks(paramA, paramB) { // Set of statements } | **Syntax:** var geeksforGeeks= function(paramA, paramB) { // Set of statements } |

* * *

### Reference

Geeksforgeeks: [https://www.geeksforgeeks.org/javascript/functions-in-javascript/](https://www.geeksforgeeks.org/javascript/functions-in-javascript/)
