views:

433

answers:

3

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

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 (UA 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?)

A: 

I think it's supposed to be

let (foo = "bar") {
  // use foo
}

edit ah ok sorry - my example is the statement, you're interested in the "let expression."

Pointy
Both that syntax and the one David posted are mentioned in the Mozilla docs.
Dominic Rodger
@Dominic I don't see that here: https://developer.mozilla.org/en/New_in_JavaScript_1.7#Block_scope_with_let In fact there's also this warning on that page: **Note: When using the let statement syntax, the parentheses following let are required. Failure to include them will result in a syntax error.**
Pointy
The first example under "`let` definitions" reads `if (x > y) { let gamma = 12.7 + y; i = gamma * x; }`, am I missing something?
Dominic Rodger
Ah - figured it out - @Pointy - scroll down to the section marked "`let` statement", the block stuff is further up.
Dominic Rodger
+4  A: 

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

EDIT

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>
Harmen
+5  A: 

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>
CMS
Well; I just updated my answer and then found out that you beat me in a minute ;). +1
Harmen
Thanks, I was using the HTML 5's '<script></script>' without the type attr.
David Murdoch