当前哪些浏览器支持 JavaScript 的“ let”关键字?

我正在开发一个应用程序,不用担心 Internet Explorer 问题,我正在研究一些在 a + 级浏览器中出现的特性,这些特性不在 IE 1中。

我希望使用的特性之一是 JavaScript 的 let 关键字

我似乎无法在 Firefox 3.6中使用他们的“ let”示例(用户代理字符串: Mozilla/5.0(Windows; U; Windows NT 5.1; en-US; rv: 1.9.2) Gecko/20100115 Firefox/3.6(。NET CLR 3.5.30729).执行 let foo = "bar"时得到 SyntaxError: missing ; before statement

那么,哪些浏览器支持 let 关键字? (或者我做错了什么?)

61360 次浏览

Internet Explorer and Opera don't support let on any browser version, Firefox since version 2.0 and Safari since 3.2.

See this JavaScript version table on Wikipedia.

I just found out that you need to define whether you use JavaScript 1.7 or not. So your code will be:

<script type="application/javascript;version=1.7"> ... </script>

EDIT: let and const are supported by all modern browsers and are part of the ECMAScript 2015 (ES6) specification.

Basically if you don't need to support anything below IE11, let and const are safe to use nowadays.

On IE11 there's a small quirk with let when used with for loops, the variable is not bound to the for block as you would expect, it behaves as var did...

See also: let and const support.


Old and outdated answer from 2010: Those extensions are not ECMA-Standard, they are supported only by the Mozilla implementation.

On browser environments you should include the JavaScript version number in your script tag to use it:

<script type="application/javascript;version=1.7">
var x = 5;
var y = 0;


let (x = x+10, y = 12) {
alert(x+y + "\n");
}


alert((x + y) + "\n");
</script>

There is partial support in Internet Explorer 11 (for scope is incorrect) and full support in all current browsers (ECMAScript 6 compatibility table: let).

Just an update: Chrome now supports let but only if you declare the "use strict"; directive.

A great deal of time has passed since this question was first asked: the 'let' and 'const' keywords have been introduced in ECMAScript 2015 (ES6). Search for 'let' or 'const' in this awesome ES6 compatibility table: https://kangax.github.io/compat-table/es6/

As of April 2017:

  • All up-to-date major browsers such as Chrome, Firefox, and Edge support the ES2015 (aka "ES6") let keyword.

  • iOS Safari did not support let until OS 10 (e.g, OS 9 did not).

  • Some older browsers, such as IE9-IE11, support an early version of let but don't support the semantics defined by ES2015 (particularly in relation to declarations in the headers of for loops). So it's not a syntax error, and it does declare the variable, but it doesn't work the way it's supposed to. For instance, in a correct implementation, the following logs 0, 1, and 2; on IE9-IE11, it logs 3, 3, 3:

     for (let i = 0; i < 3; ++i) {
    setTimeout(function() {
    console.log(i);
    }, i * 100);
    }
    

  • Obsolete browsers such as IE8 do not support it at all.