| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/let | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2016 9.
let , .
let x = 1;
if (x === 1) {
let x = 2;
console.log(x);
// Expected output: 2
}
console.log(x);
// Expected output: 1
let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];
nameN. JavaScript .
valueN Optional, JavaScript .
let { bar } = foo; // foo = { bar: 10, baz: 12 };
/* 10 'bar' */
let let . let var , var ( ) . let ( ) var .
:
let , let .
( )
let . let var . , var ' ' , .
function varTest() {
var x = 1;
if (true) {
var x = 2; // !
console.log(x); // 2
}
console.log(x); // 2
}
function letTest() {
let x = 1;
if (true) {
let x = 2; //
console.log(x); // 2
}
console.log(x); // 1
}
var let .
var x = "global";
let y = "global";
console.log(this.x); // "global"
console.log(this.y); // undefined
var Thing;
{
let privateScope = new WeakMap();
let counter = 0;
Thing = function () {
this.someProperty = "foo";
privateScope.set(this, {
hidden: ++counter,
});
};
Thing.prototype.showPublic = function () {
return this.someProperty;
};
Thing.prototype.showPrivate = function () {
return privateScope.get(this).hidden;
};
}
console.log(typeof privateScope);
// "undefined"
var thing = new Thing();
console.log(thing);
// Thing {someProperty: "foo"}
thing.showPublic();
// "foo"
thing.showPrivate();
// 1
var . , ( IIFE) .
if (x) {
let foo;
let foo; // SyntaxError
}
switch .
let x = 1;
switch (x) {
case 0:
let foo;
break;
case 1:
let foo; // SyntaxError
break;
}
.
let x = 1;
switch (x) {
case 0: {
let foo;
break;
}
case 1: {
let foo;
break;
}
}
let ( undefined ). ReferenceError .
:
var , var undefined.
" "(Temporal Dead Zone, TDZ) .
function do_something() {
console.log(bar); // undefined
console.log(foo); // ReferenceError
var bar = 1;
let foo = 2;
}
"" , () () . let , .
{
// TDZ
const func = () => console.log(letVar); // OK
// TDZ letVar ReferenceError
let letVar = 3; // letVar TDZ
func(); // TDZ
}
typeoftypeof TDZ let ReferenceError .
console.log(typeof i); // ReferenceError
let i = 10;
, undefined .
console.log(typeof undeclaredVariable); // undefined
ReferenceError .
function test() {
var foo = 33;
if (foo) {
let foo = foo + 55; // ReferenceError
}
}
test();
var foo if . , var foo . foo let foo . (foo + 55) let foo , TDZ ReferenceError .
. let n of n.a for , n.a (let n) n a . n n.a let n TDZ .
function go(n) {
// n n
console.log(n); // Object {a: [1,2,3]}
for (let n of n.a) {
// ReferenceError
console.log(n);
}
}
go({ a: [1, 2, 3] });
let . var .
var a = 1;
var b = 2;
if (a === 1) {
var a = 11; //
let b = 22; // if
console.log(a); // 11
console.log(b); // 22
}
console.log(a); // 11
console.log(b); // 2
var let SyntaxError. var , .
let x = 1;
{
var x = 2; // SyntaxError
}
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-let-and-const-declarations |
varconstlet and constlet and var?This page was last modified on 2026 7 15 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |