About declaring functions

Hi everyone!

I’ve been learning javascript for a year now and I’m starting to be quite good at it.
There is one thing that I need to clarify.

let’s compare these two things:

function hello() {
    console.log('hello ,world!')
}

and

const hello = () => {
    console.log('hello, world!')
}

is there a difference between these two appart from the way they are written?
Does one perform better or offer more advantages?
Is it just a matter of taste and habbit?
I personnaly prefer the first one, just because there are less special character to type, so it’s musch easyer and faster. But, if the second one is more efficient for the program, then I would go for it.

Thanks in advance!

Eric

There are quite a few differences, the two are not strictly interchangeable.

You can read a full explanation with examples here:

There is no difference in terms of runtime efficiency so that is not a consideration you need to worry about. The article @Marmiz gave you explains the technical differences and the situations where you can’t use the arrow functions. The big advantage of arrow functions is their conciseness, especially when used as short callback functions. If you want to sort numerically then you would do:

numbers.sort((a,b) => a-b);

The vast majority of programmers are going to prefer this syntax over using the traditional function syntax. You would probably even get strange looks if didn’t use an arrow function here. Yes, there may be a few new characters to type but the amount of characters you have to type is less and visually it is much simpler.

I’ve been programming for a long time and so the traditional form is what I’m most comfortable with, but things change and arrow functions are all the rage now. So I would encourage you to use them wherever they make sense.

Now, if you want to talk about syntax preferences, the fact that you didn’t include semicolons in your code is driving me crazy. Ya, I know JS doesn’t require them (except in very very specific instances) but I’m so old school that my eyes are hurting just looking at your code :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.