<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>变量</title> <script> /* * var * - 没有块级作用域 * let * - 有块级作用域 * const * - 和let类似,具有块级作用域,但是它只能赋值一次 * - 使用场景: * 1. 对于一些常量可以使用const声明 * 2. 对于一些对象(函数)也可以使用const声明 * 这样可以避免对象(函数)被意外修改 * */ { let a = 10; } // console.log(a); for(let i=0; i<5; i++){ console.log('循环内部-->', i); } // console.log('循环外部-->', i); (function (){ if(false){ var b = 33; } })(); if(false){ let b = 33; } // console.log(b); const c = 44; // c = 'hello'; const PI = 3.14; // console.log(c); const obj = {name:'孙悟空'}; obj.age = 18; const fn = function (){ }; console.log(obj.age); </script> </head> <body> </body> </html>