Questions tagged [let]

In Lisp-like and functional languages, introduces a list of local variables, each (possibly optionally) with its initial value.

In Lisp-like and functional languages, introduces a list of local variables, each (possibly optionally) with its initial value. It is illegal to refer to any of the new variables while calculating their initial values ( lets you do that), though in Haskell it is legal (its let is actually letrec).

LET is a special form in Common Lisp, Scheme, Clojure and other Lisp dialects which introduces a list of local variables as pairs of name-value bindings, for use within its body. For instance, in this expression:

(let ((variable1 (+ 1 1)))
  variable1)

variable1 gets bound to the 2 value, and the whole expression returns 2 as a result.

728 questions
6164
votes
40 answers

What is the difference between "let" and "var"?

ECMAScript 6 introduced the let statement. I've heard that it's described as a local variable, but I'm still not quite sure how it behaves differently than the var keyword. What are the differences? When should let be used instead of var?
TM.
  • 108,298
  • 33
  • 122
  • 127
364
votes
8 answers

Why was the name 'let' chosen for block-scoped variable declarations in JavaScript?

I understand why var takes that name - it is variable, const - it is a constant, but what is the meaning behind the name for let, which scopes to the current block? Let it be?
Vitaly Zdanevich
  • 13,032
  • 8
  • 47
  • 81
350
votes
7 answers

Are variables declared with let or const hoisted?

I have been playing with ES6 for a while and I noticed that while variables declared with var are hoisted as expected... console.log(typeof name); // undefined var name = "John"; ...variables declared with let or const seem to have some problems…
Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
196
votes
3 answers

What is the temporal dead zone?

I've heard that accessing let and const values before they are initialized can cause a ReferenceError because of something called the temporal dead zone. What is the temporal dead zone, how does it relate to scope and hoisting, and in what…
joews
  • 29,767
  • 10
  • 79
  • 91
144
votes
5 answers

Haskell: Where vs. Let

I am new to Haskell and I am very confused by Where vs. Let. They both seem to provide a similar purpose. I have read a few comparisons between Where vs. Let but I am having trouble discerning when to use each. Could someone please provide some…
user295190
127
votes
6 answers

v8 JavaScript performance implications of const, let, and var?

Regardless of functional differences, does using the new keywords 'let' and 'const' have any generalized or specific impact on performance relative to 'var'? After running the program: function timeit(f, N, S) { var start, timeTaken; var…
sean2078
  • 5,131
  • 6
  • 32
  • 32
86
votes
4 answers

In Haskell, when do we use in with let?

In the following code, the last phrase I can put an in in front. Will it change anything? Another question: If I decide to put in in front of the last phrase, do I need to indent it? I tried without indenting and hugs complains Last generator in…
McBear Holden
  • 5,741
  • 7
  • 33
  • 55
83
votes
2 answers

Why let and var bindings behave differently using setTimeout function?

This code logs 6, 6 times: (function timer() { for (var i=0; i<=5; i++) { setTimeout(function clog() {console.log(i)}, i*1000); } })(); But this code... (function timer() { for (let i=0; i<=5; i++) { setTimeout(function clog()…
user2290820
  • 2,709
  • 5
  • 34
  • 62
80
votes
7 answers

Use if else to declare a `let` or `const` to use after the if/else?

I'm not sure why but it seems that I can't call the let or const variables if I declare them in an if/else statement. if (withBorder) { const classes = `${styles.circularBorder} ${styles.dimensions} ${styles.circularPadding} row…
Johhan Santana
  • 2,336
  • 5
  • 33
  • 61
72
votes
3 answers

Let vs. Binding in Clojure

I understand that they're different since one works for setting *compile-path* and one doesn't. However, I need help with why they're different. let creates a new scope with the given bindings, but binding...?
Carl
  • 887
  • 1
  • 7
  • 6
70
votes
2 answers

Purpose of "let expression" (LetExpr) in the Java compiler?

The Java compiler seems to have support for let expressions in com.sun.tools.javac.tree.* (look for LetExpr). One comment in JCTree even mentions some syntax (let int x = 3; in x+2) which of course is not accepted by the grammar of the language and…
soc
  • 27,983
  • 20
  • 111
  • 215
64
votes
5 answers

Do let statements create properties on the global object?

In JavaScript, var declarations create properties on the global object: var x = 15; console.log(window.x); // logs 15 in browser console.log(global.x); // logs 15 in Node.js ES6 introduces lexical scoping with let declarations that have block…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
55
votes
4 answers

Lazy Var vs Let

I want to use Lazy initialization for some of my properties in Swift. My current code looks like this: lazy var fontSize : CGFloat = { if (someCase) { return CGFloat(30) } else { return CGFloat(17) } }() The thing is that once the…
YogevSitton
  • 10,068
  • 11
  • 62
  • 95
44
votes
4 answers

How to "let" in lambda expression?

How can I rewrite this linq query to Entity on with lambda expression? I want to use let keyword or an equivalent in my lambda expression. var results = from store in Stores let AveragePrice = store.Sales.Average(s => s.Price) …
Reza Owliaei
  • 3,293
  • 7
  • 35
  • 55
43
votes
5 answers

In JavaScript, why should I usually prefer 'const' to 'let'?

Why most of the time should I use const instead of let in JavaScript? As we know if we use const then we can't reassign value later. Then why not use let instead of const?
Mohammed Saimon
  • 563
  • 1
  • 5
  • 10
1
2 3
48 49