1_新的变量定义方式.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // js 变量定义方式
  11. var a = 1;
  12. let b = 2;
  13. const c = 3;
  14. // var 定义的变量可以重复定义
  15. // var num1 = 10;
  16. // var num1 = 20;
  17. // console.log(num1); // 20
  18. // let、 const 定义的变量不可以重复定义
  19. // let num2 = 10;
  20. // let num2 = 20;
  21. // var 定义变量相当于放置到window对象上
  22. // var num3 = 10;
  23. // console.log(window.num3); // 10
  24. // let、 const 定义的变量不放置到window对象上
  25. // let num4 = 20;
  26. // console.log(window.num4); // undefined
  27. // let vs const
  28. // let 定义的变量可以修改 (let定义的是变量)
  29. // const 定义的变量不可以修改 (const定义的是常量)
  30. // let 和 const 除了一个是常量一个是变量外其他都一致
  31. // let num5 = 10;
  32. // num5 = "hello";
  33. // console.log(num5); // hello
  34. const num6 = 20;
  35. num6 = "world"; // 报错
  36. </script>
  37. </body>
  38. </html>