var a = ‘foo‘
console.log(window.a) // foo
let b = ‘bar‘
console.log(window.b) // undefined
const c = ‘baz‘
console.log(window.c) // undefined
console.log(a) // undefined
var a = ‘foo‘
console.log(b) // test4.html:14 Uncaught ReferenceError: Cannot access ‘b‘ before initialization
let b = ‘bar‘
console.log(c) // test4.html:14 Uncaught ReferenceError: Cannot access ‘c‘ before initialization
const c = ‘baz‘
if (true) {
var a = ‘foo‘
let b = ‘bar‘
}
console.log(a) // foo
console.log(b) // Uncaught ReferenceError: b is not defined
if (true) {
var a = ‘foo‘
const c = ‘baz‘
}
console.log(a) // foo
console.log(c) // Uncaught ReferenceError: c is not defined
var a = ‘foo‘
var a = ‘bar‘
console.log(a) // bar
let b = ‘foo‘
let b = ‘bar‘
console.log(b) // test4.html:34 Uncaught SyntaxError: Identifier ‘b‘ has already been declared
const c = ‘foo‘
const c = ‘bar‘
console.log(c) // Uncaught SyntaxError: Identifier ‘c‘ has already been declared
const person = {}
person.name = ‘maoxiaoxing‘
console.log(person) // {name: "maoxiaoxing"}
person = {name: ‘king‘} // test4.html:45 Uncaught TypeError: Assignment to constant variable.
const a = [‘foo‘]
a.push(‘bar‘)
a[2] = ‘baz‘
console.log(a) // ["foo", "bar", "baz"]
原文:https://www.cnblogs.com/bejamin/p/14223160.html