javascript中使用到的变量,尽量在当前作用域的头部集体声明,变量声明应在一条声明语句中完成:只使用一个命令(var/let/const),各个变量名用逗号隔开。执行一条语句的速度高于执行多条语句的速度。
    注:引用变量,以及常量尽量使用 const ,而普通变量则尽可能使用 let

    反例

    1. function func(){
    2. var count = 5;
    3. var color = 'blue';
    4. var value = [1, 2, 3];
    5. var now = new Date();
    6. var students = {
    7. name: '张三',
    8. num: '13356477'
    9. };
    10. //do something...
    11. }
    12. {
    13. let count = 5;
    14. let color = 'blue';
    15. let value = [1, 2, 3];
    16. let now = new Date();
    17. let students = {
    18. name: '张三',
    19. num: '13356477'
    20. };
    21. }
    22. const count = 5;
    23. const color = 'blue';
    24. const value = [1, 2, 3];
    25. const now = new Date();
    26. const students = {
    27. name: '张三',
    28. num: '13356477'
    29. };

    正例

    1. function func(){
    2. var count = 5,
    3. color = 'blue',
    4. value = [1, 2, 3],
    5. now = new Date(),
    6. students = {
    7. name: '张三',
    8. num: '13356477'
    9. };
    10. //do something...
    11. }
    12. {
    13. let count = 5,
    14. color = 'blue',
    15. value = [1, 2, 3],
    16. now = new Date(),
    17. students = {
    18. name: '张三',
    19. num: '13356477'
    20. };
    21. }
    22. const count = 5,
    23. color = 'blue',
    24. value = [1, 2, 3],
    25. now = new Date(),
    26. students = {
    27. name: '张三',
    28. num: '13356477'
    29. };