DEV Community

Cover image for Function declaration vs Function expression
CravenCraven
CravenCraven

Posted on

Function declaration vs Function expression

Function declaration vs Function expression

In JavaScript, a function is a reusable block of code that performs a specific task or set of tasks. It's like a recipe: you define the steps to be taken, and then you can reuse that recipe (function) whenever you need to perform those steps.

Function declaration

A function declaration creates a Function object. Each time when a function is called, it returns the value specified by the last executed return statement, or undefined if the end of the function body is reached.

Let's break this down in more simple terms

To use a function you must first declare it. In javascript, the function is declared by using the function keyword.

Image description

Here are the steps to declare a function in javascript.

  1. Start function with the function keyword
  2. Then write the name of the function with a space. The naming convention of function is the same as a variable naming convention.
  3. Then give arguments to the function enclosed in parentheses and separated by commas like (parameter1, parameter2, ...)
  4. Then define the function body by curly braces {...}
  5. At the end of the function use return keyword to return any object or value.

Function expressions

Function expression is similar to function but the difference is function name. Let's take a deeper dive into this. In order for use function expression we will need to use the following with a name

Let vs Const vs Var

Let
Use let when the variable needs to be reassigned or its value will change.
It's a good choice for variables that are expected to have different values at different points in your code.

Example:

let counter = 0;
counter = 1; // Valid

Enter fullscreen mode Exit fullscreen mode

Const
Use const when the variable should not be reassigned. It creates a constant reference to a value.
It's particularly useful for declaring values that are meant to remain constant throughout the program.

Example:

const PI = 3.14;
Enter fullscreen mode Exit fullscreen mode

Var
var is less commonly used due to its function-scoping behavior.

Example:

var x = 10;

Enter fullscreen mode Exit fullscreen mode

In the world of JavaScript, variable declarations play a crucial role in managing data. Whether you're declaring a constant that holds an unchanging value, using let for variables that might evolve, or opting for var in specific scenarios, understanding when to use each is key.

In a nutshell:

Use const for values that remain constant throughout your program.

Turn to let when your variable's journey involves reassignment.
Consider var cautiously, as its function-scoping behavior might not always align with modern best practices.

Remember, the choice between let, var, and const depends on the scope and mutability requirements of your variables. By mastering these declarations, you pave the way for more robust and readable JavaScript code.

Happy coding! 🚀

Top comments (0)