let const var
const
解构赋值
模板字符串
简化对象
案例let
<script>
//声明变量
let a;
let b,c,d;
let e=100;
let f=123,g='ilovejs',h=[];
console.log(a,b,c,d,e,f,g,h);
//特点
//1,变量不能重复声明
let girl='lisa';
console.log(girl);
// let girl='f4'; //Uncaught SyntaxError: Identifier 'girl' has already been declared
// console.log(girl);
//2,块级作用域
//if else while for
{
let boy='jose';
console.log(boy);
}
// console.log(boy); //1-1let.html:28 Uncaught ReferenceError: boy is not defined
if (e !==null) {
let hi=2;
console.log('e不是null');
} else{
console.log('e是null');
}
// console.log(hi); //1-1let.html:37 Uncaught ReferenceError: hi is not defined
// console.log(typeof(hi));
//3,不存在变量提升
// console.log(i);
// let i=2; //1-1let.html:40 Uncaught ReferenceError: i is not defined
//
//4,不影响作用域链
{
let school='好好学习';
function fn(){
console.log(school);
}
fn();
}
</script>
案例const
<script>
//声明变量
const SCHOOL='好好学习';
console.log(SCHOOL);
//1,一定要赋值
// const A; //Uncaught SyntaxError: Missing initializer in const declaration
//console(A);
//2,一般常量使用大写(潜规则)
//3,常量的值不能修改,不能重复声明
//const SCHOOL='天天向上';//Uncaught SyntaxError: Identifier 'SCHOOL' has already been declared
//console.log(SCHOOL);
// SCHOOL='天天向上';//1-2const.html:19 Uncaught TypeError: Assignment to constant variable.
//console.log(SCHOOL);
//4,块级作用域
{
const PLAYER='UZI';
}
//console.log(PLAYER);//1-2const.html:25 Uncaught ReferenceError: PLAYER is not defined
//5,对于数组和对象的元素修改,不算做对常量的修改,不会报错.
const TEAM=['UZI','ASD','ASW'];
console.log(TEAM);
TEAM.push('MMM');
console.log(TEAM);
</script>
</body>