[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/let [Back]  [Original]

let - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

let

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2016 9.

let , .

In this article

let x = 1;

if (x === 1) {
  let x = 2;

  console.log(x);
  // Expected output: 2
}

console.log(x);
// Expected output: 1

js
let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];

nameN

. JavaScript .

valueN Optional

, JavaScript .

.

js
let { bar } = foo; // foo = { bar: 10, baz: 12 };
/* 10   'bar'   */

let let . let var , var ( ) . let ( ) var .

const let ( ) window .

"let" .

: let , let . ( )

let . let var . , var ' ' , .

js
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 .

js
var x = "global";
let y = "global";
console.log(this.x); // "global"
console.log(this.y); // undefined

let .

js
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) .

SyntaxError .

js
if (x) {
  let foo;
  let foo; // SyntaxError
}

switch .

js
let x = 1;
switch (x) {
  case 0:
    let foo;
    break;

  case 1:
    let foo; //   SyntaxError
    break;
}

.

js
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) .

js
function do_something() {
  console.log(bar); // undefined
  console.log(foo); // ReferenceError
  var bar = 1;
  let foo = 2;
}

"" , () () . let , .

js
{
  // TDZ    
  const func = () => console.log(letVar); // OK

  // TDZ  letVar  ReferenceError

  let letVar = 3; // letVar TDZ 
  func(); // TDZ  
}

TDZ typeof

typeof TDZ let ReferenceError .

js
console.log(typeof i); // ReferenceError
let i = 10;

, undefined .

js
console.log(typeof undeclaredVariable); // undefined 

TDZ

ReferenceError .

js
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 .

js
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 .

js
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 , .

js
let x = 1;

{
  var x = 2; //   SyntaxError
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-let-and-const-declarations


Web Proxy Viewer  |  New URL  |  Original Page