2

I am changing my es5 code to es6 in my Angular 1.6 file(s) so started using let and const instead of var but quite confused about named function expression whether it should be const or let ?

es5

var foo = function () { ... }

es6

let foo = () => { ... }

or

const foo = () => { ... }

does mixing let and const within same js file for named function does impact the performance or we should stick with var?

In my understanding, functions are the things that always unchangeable in one JS file so we can use const.

Please give some useful explanation.

8
  • Performance questions on JS are almost always answered as "measure it" because it entirely depends between JS versions and engines. Aug 23, 2017 at 17:35
  • and how one measure it? any perf tool/technique as I am not so hardcore JS developer
    – xkeshav
    Aug 23, 2017 at 17:37
  • @loganfsmyth Since this is Angular, he is most likely writing in TS, and most likely transpiling to ES5, and assuming that's the case there obviously are no performance implications.
    – user663031
    Aug 23, 2017 at 17:40
  • sorry, forgot to mention but I am using Angular 1.6, so not using TS.
    – xkeshav
    Aug 23, 2017 at 17:42
  • @loganfsmyth ...and depends on time, because it probably will change with the next version of engines.
    – user6445533
    Aug 23, 2017 at 17:46

1 Answer 1

5

The rules for const vs. let are the same whether you are assigning a function or any other value. If you don't intend or want to change the value (nearly always the case for functions), use const. However, notice that there are differences in hoisting behavior, although it is unlikely that would affect you.

Any performance difference will be negligible.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.