In javascript should I use const instead of var whenever possible?

后端 未结 3 1113
北荒
北荒 2021-02-20 05:31

If creating a reference to an object, and the reference is not going to change (even though the object will), is it better to use const instead of var?

For example:

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-20 06:11

    const is used for a variable constant, i.e. once declared, it's value is not supposed to change, whereas var can be changed as per the will.

    Use of const and var is solely on your will. For me it goes as follows

    • If I'm 100% sure that the value of a variable is not going to change and it's not going to re-declared, I'll define it as a const.
    • In all other cases I'll use var.

    Mind it, re-declaring const will immediately throw an error.

提交回复
热议问题