r/node • • Apr 14 '24

JavaScript Functions - 4 Ways

Post image
208 Upvotes

79 comments sorted by

View all comments

Show parent comments

2

u/n8rzz Apr 15 '24

Why do you say that? Genuinely curious.

1

u/Freecelebritypics Apr 15 '24

I am a total amateur so feel free to ignore me! I just don't know why I'd ever need to write function AND const on the same line.

1

u/n8rzz Apr 15 '24

One reason this may be useful is because it prevents hoisting. A second reason is that you could conditionally assign a function to a variable for use later. A third, though not as common today, is it allows you to extend a function by adding more functions to its prototype. You rarely see this today because we have classes. But that’s how we’d fake classes years ago.

2

u/Freecelebritypics Apr 15 '24

I've certainly used the second scenario before, now you mention it. Though I haven't found a situation where I'd want to prevent hoisting yet.

And yeah, proper classes have been around since I start learning JS.

Thanks for the explanation.