DEV Community

Baljeet Singh
Baljeet Singh

Posted on • Edited on

3

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")

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay