DEV Community

Baljeet Singh
Baljeet Singh

Posted on • Updated on

FOUR DIFFERENT WAYS TO WRITE FUNCTIONS IN JAVASCRIPT

Functions are the basic building blocks of the Javascript. A function in Javascript is a code block that performs a specific task or set of tasks.
There are basically 4 (four) different ways we can define functions in Javascript.

  • Function Declaration
  • Function Expression
  • Arrow Function Expression
  • Concise Arrow Function Expression
// Function Declaration
function square(x) {
    return x * x;
}

// Function Expression
const square = function(x) {
    return x * x;
}

// Arrow Function Expression
const square = x => {
    return x * x;
}

// Concise Arrow Function Expression
const square = x => x * x;
Enter fullscreen mode Exit fullscreen mode

If you prefer you can take a look at the video tutorial,

Top comments (2)

Collapse
 
thomasjunkos profile image
Thomas Junkツ

Thank you! Nice writeup!

Would you mind, elaborate a bit?
Where are the differences besides different ways of declaration?
What is the scope / when could it be referenced?
If you use a function expression, does hoisting play into that?

Collapse
 
wakeupmh profile image
Marcos Henrique • Edited

Thank you for this post!
If you put "javascript" term after the code delimiter the js syntax will be highlighted, like that:

console.log("I AM highlighted")