What browsers currently support JavaScript's 'let' keyword?
Asked Answered
K

7

90

I'm developing an app and don't have to ever worry about Internet Explorer and was looking into some of the features present in A+ grade browsers that aren't in Internet Explorer1.

One of these features I wanted to play around with is JavaScript's let keyword

I can't seem to get any of their 'let' examples to work in Firefox 3.6 (User-Agent string: 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)). I get SyntaxError: missing ; before statement when executing let foo = "bar".

So, what browsers support the let keyword? (Or am I doing something wrong?)

Komatik answered 1/3, 2010 at 15:26 Comment(6)
While and understand and agree your not-caring-if-it-works on IE, you should consider that the "let" keyword is mozilla-only, so even if it's ok to say "IE doesn't support it", the proper way to say it, would be "only mozilla supports it". Just like we avoid IE-only tags, or keywords, you should avoid and *-only keywords.Shingles
Hugo, you need to keep in mind that HTML, CSS, JS isn't a web-only platform anymore. The code happens to be code that will never run in any version of IE, or any webbrowser, for that matter. :-)Komatik
What on earth is Apple trying to do.. it's 2015 and no signs of let support in their pathetic browser.Clinometer
PLEASE NOTE Many of the older answers (including the accepted answer) have been overtaken by events. All major up-to-date browsers support the ES2015 (aka "ES6") let keyword, but as of this writing support is fairly new in some circles (only iOS 10 Safari, for instance).Euroclydon
I suggest changing your accepted answer to one of the now-correct ones. Yes, the currently-accepted one was correct at the time, but it's been incorrect for several years now, and the purpose of SO is to be a repository of knowledge, not a historical archive. :-)Euroclydon
How was this question asked in 2010? I thought let and const were introduced in 2015 with es6?Musclebound
W
66

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>
Wholesale answered 1/3, 2010 at 15:39 Comment(6)
Well; I just updated my answer and then found out that you beat me in a minute ;). +1Humbuggery
Thanks, I was using the HTML 5's '<script></script>' without the type attr.Komatik
Note that the let keyword is now in the ECMAscript 6 (draft) standard.Scarface
Can be enabled in Chrome 19+ via "Experimental Javascript features" flag and in node.js using --harmony.Clinometer
Is ;version=1.7 still required in Firefox? I tried let in the console and it worked.Sayer
Seems like this version specification is deprecated now. If I try to specify version, Chrome is not loading that javascript.Subversive
E
34

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.

Euroclydon answered 6/4, 2017 at 16:28 Comment(0)
S
25

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

Sayer answered 25/8, 2013 at 19:1 Comment(1)
While IE11 does "support" the let keyword (in that it doesn't throw a SyntaxError), its implementation does not conform to the spec - the variable is not properly block-scoped. (also note that there is not and will never be any IE past 11)Gentilis
H
12

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>
Humbuggery answered 1/3, 2010 at 15:33 Comment(2)
Seems like this version specification is deprecated now. If I try to specify version, Chrome is not loading that javascript.Subversive
The information regarding to browser version support is outdated.Demean
D
6

Apparently Internet Explorer 10 in Edge mode supports let, per JavaScript Version Information.

Delanos answered 7/7, 2013 at 20:27 Comment(0)
L
3

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/

Later answered 10/6, 2016 at 13:4 Comment(0)
P
2

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

Propositus answered 8/6, 2015 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.