Mozilla offers a let in javascript 1.7. The let keyword isn’t support by other browsers yet so using it isn’t generally recomended but it nice to catch up on the latest javascript constructs.
A Parallel
First, a parallel. I’ve coded in Java the last couple of months because of school. During said time, I discovered scoped blocks in Java. A scoped block might be like this:
public void myMethod() { int i = 100, j = 10; { int k = i * j; i = 10; j = 10; } // cannot reference k from here }
If you’re a java programmer, you might never have seen this happen before. Basically, putting floating brackets, as long as they match up, around arbitrary code, it will be in a different scope. k won’t be accessible from the outside of those brackets. I call these structures scoped blocks.
Now back to javascript
let in javascript acts similarly to the scoped blocks mentioned above.
Using let as a block is similar to the scoped block in java. The let-block won’t allow g to be accessed from the outside, x and m are newly bound values, they will only be around in the block scope.
var i = 100, j = 10; let (x = 10+i, m = j + 5) { var g = x + m; }
let might make it easy to bind this to another variable in a certain scope, which could prove useful.
For more information about let, checkout the MDC page.
I knew about the java scope braces, but I didn’t know about the let keyword in javascript.
Is this the same behavior that let has on lisp-like languages? because if it is I finally get it and how I can use it in clojure.