[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/block [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Baseline Widely available

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

{ }

Implemented in JavaScript 1.0
ECMAScript edition ECMA-262 1st edition

In this article

{
  _1
  _2
  ...
  _n
}

_1, _2, _n

ifforwhile

var

var Execution Context

js
var x = 1;
{
  var x = 2;
}
alert(x); // outputs 2

2 var

let const

letconst

js
let x = 1;
{
  let x = 2;
}
console.log(x); // logs 1

function

function function Execution Context function var function

js
function foo() {
  {
    var a = "var";
    {
      let a = "let";
      console.log(a); // let
    }
  }
  console.log(a); // var
}
foo();

Specification
ECMAScript 2027 LanguageSpecification
# sec-block


Web Proxy Viewer  |  New URL  |  Original Page