+ 2

Why are the arrow functions in ES6 declared always as "const" and can it be done as a "var" or "let?

Arrow functions always const?

28th Nov 2018, 1:07 PM
Panayot Zhaltov
Panayot Zhaltov - avatar
5 odpowiedzi
+ 11
[Arrow Functions:] Arrow functions have two main aspects: their structure and their this binding. They can have a much simpler structure than traditional functions because they don't need the 'function' key word, and they automatically return whatever is after the arrow. For example, var foo = function( a, b ) { return a * b; } let bar = ( a, b ) => a * b; https://www.sololearn.com/post/48576/?ref=app https://www.sololearn.com/Discuss/1600345/?ref=app https://www.sololearn.com/Discuss/1601369/?ref=app [ES6:] Tips & Tricks to make your code cleaner, shorter, and easier to read! https://www.sololearn.com/post/48506/?ref=app [ES6:] Cool stuffs - A big fat Arrow https://medium.com/front-end-hacking/es6-cool-stuffs-a-big-fat-arrow-c01e3d7b5357 [Edited:] Using 'const' Variables declared with 'var' or 'let' can be changed later on in the program, and reassigned. A once a 'const' is initialized, its value can never be changed again, and it can't be reassigned to a different value.
28th Nov 2018, 2:23 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 2
They can be declared as variables too but it is a good practice to declare them as constants.
28th Nov 2018, 1:45 PM
Harmeet Singh Bhatia
Harmeet Singh Bhatia - avatar
+ 1
It's good practice to declare them as consts because this is a way to signify to the reader that they are generally not changed or you don't plan to reassign them to another value later. see this answer for more https://stackoverflow.com/questions/42275425/benefit-of-const-vs-let-in-typescript-or-javascript
29th Nov 2018, 1:22 AM
atlant
+ 1
Yes you can. The use of const keyword is to ensure that the function would not change by any means.
29th Nov 2018, 2:07 PM
Seniru
Seniru - avatar
0
This is due to the hoisting. It forces you to define your functions before using them.
24th Dec 2018, 6:54 AM
Francis Fulgencio
Francis Fulgencio - avatar