JavaScript functions

Benny Kang
2 min readNov 19, 2021

--

This post is for myself to remember the different types(declarations, expressions, arrow)of functions syntax and their difference.

Function is a piece of codes that we can reuse over and over again in your code. It is a bit more like a variable but the difference is a variable holds value and a function holds one or more complete lines of code.

// Function Declarations

Example of function declarations

// Function Expressions

Example of function expressions

This is basically a function without a name. This function without a name is an expression and an expression produces a value so we use that value and store it into a variable then, that variable now holds the function value.

In JavaScript, function is a value just as a number or a string or a boolean.

another big difference between these two functions is that you can call function declarations before they are defined in the code like this

Call before its defined

but you cannot call function expressions before they are defined

Call before its defined
Error

then, you get this initialization error.

This is because of a process called hoisting but I will go deep into that in the future post.

// Arrow functions

Simple as that

A third type of functions is called Arrow functions. This is good for simple one liner functions and it does not even have to write return keyword and it works the same as the other functions but if the function is more complicated with more parameters then there is no advantage of using this arrow functions.

It is more of personal preferences what function type to use besides the arrow function do not get a ‘This’ keyword.

--

--

No responses yet