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?
8 Answers
Let is a mathematical statement that was adopted by early programming languages like Scheme and Basic. Variables are considered low level entities not suitable for higher levels of abstraction, thus the desire of many language designers to introduce similar but more powerful concepts like in Clojure, F#, Scala, where let
might mean a value, or a variable that can be assigned, but not changed, which in turn lets the compiler catch more programming errors and optimize code better.
JavaScript has had var
from the beginning, so they just needed another keyword, and just borrowed from dozens of other languages that use let
already as a traditional keyword as close to var
as possible, although in JavaScript let
creates block scope local variable instead.
-
123And the official Mozilla Documentation page cites this page for this question developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Aug 21, 2017 at 7:13
-
11It's also nice that
len('var') === len('let')
, meaning that your declarations line up nicely in your editor either way. I haven't found anything yet to suggest this was deliberate, but it can either be a) visually pleasing and more readable, or b) horribly annoying to debug if you mix the two (which seems like a bad idea anyways, but I've seen it done).– brichinsOct 25, 2017 at 1:18 -
18In other words,
let
has been traditionally used to describe a constant, as inlet C = the speed of light
so Javascript decided to use let to describe a variable with nonstandard scoping and at the same time introduce another keyword for constants. Mar 9, 2018 at 20:41 -
7@fijiaaron: Nonstandard scoping how? Isn't whole-function-regardless-of-what-block-you-declared-it-in kind of odd to start with?– SamBJun 4, 2019 at 22:41
-
6@fijiaaron yep you're just wrong here. that's not a good use of "let" in science either, you dont generally use it for constants because those variables everyone knows. c is the speed of light. we dont have to "let" it be. we say "let x be the distance to the center" when it is not a constant at all.– NachtOct 29, 2019 at 23:42
I guess it follows mathematical tradition. In mathematics, it is often said "let x be arbitrary real number" or like that.
-
5I prefer this answer more as it is brief and quite relatable as we all went through algebra, then probably should be followed by explanation like what the others are pointing out.– duduweApr 21, 2020 at 13:39
-
-
3
Adding to exebook's response, the mathematics usage of the keyword let also encapsulates well the scoping implications of let
when used in Javascript/ES6. Specifically, just as the following ES6 code is not aware of the assignment in braces of toPrint
when it prints out the value of 'Hello World'
,
let toPrint = 'Hello World.';
{
let toPrint = 'Goodbye World.';
}
console.log(toPrint); // Prints 'Hello World'
let as used in formalized mathematics (especially the writing of proofs) indicates that the current instance of a variable exists only for the scope of that logical idea. In the following example, x immediately gains a new identity upon entering the new idea (usually these are concepts necessary to prove the main idea) and reverts immediately to the old x upon the conclusion of the sub-proof. Of course, just as in coding, this is considered somewhat confusing and so is usually avoided by choosing a different name for the other variable.
Let x be so and so...
Proof stuff
New Idea { Let x be something else ... prove something } Conclude New Idea
Prove main idea with old x
-
22I think this is really useful. The accepted answer is almost a bit misleading, as it talks about the value not being able to be changed, which is not what
let
is about at all.let
is all about scope. Jul 13, 2017 at 14:04 -
1Just a thought:
let
makes me think of something like this going on in the mind of the code writer, "Ok, for just this moment,let foo = bar
, but then it can go back to its original value or cease to be. This is neatly illustrated in the examples for Block Scope and Redeclaring Variables in this W3Schools presentation oflet
. Again, not pretending to bring in a scientific answer of some sort, but sharing this more as a mnemonic device to remember when I want to uselet
.– AlainSep 26, 2019 at 19:59 -
1I think it's worth being slightly more precise here, for the sake of conceptual clarity. Your example ES6 code doesn't "ignore the assignment in braces". It performs the assignment as instructed, on a new variable
toPrint
that only exists within the block, and then dutifully throws that variable away when the block ends. And upon exit, we're back in the outer scope, where the innertoPrint
no longer exists, soconsole.log(toPrint)
refers to the outertoPrint
. It's not that anything's ignored, it's just thatlet
variables have finite lifetimes defined by their scope.– FeRDJan 13, 2020 at 16:15 -
1@FeRD, excellent point. I've made an edit to the above to remove the usage of the word "ignores" as it obfuscates the point. Jan 22, 2020 at 20:23
It does exactly what the var
does with a scope difference. Now it can not take the name var
since that is already taken.
So it looks that it has taken the next best name which has a semantic in an interesting English language construct.
let myPet = 'dog';
In English it says "Let my pet be a dog"
-
7While the origination of let may be linguistic and mathematical, the close-to-home reference is undoubtedly the LET statement in all versions of the BASIC programming language, one that many of the authors of ES6 would have started out learning if they're over 40 yo. Apr 10, 2017 at 14:50
-
8@wide_eyed_pupil Just FYI: as an over-40 (though not an author of ES6) who grew up on BASIC, none of the communities I was involved with used LET in common practice; we just assigned variables, as Python does now (e.g.
A$="HELLO WORLD"
) The interpreters involved included Rockwell AIM 65 BASIC, Atari Basic, MBASIC 5 on CP/M, Applesoft BASIC, and even BASCOM, the MS BASIC compiler on CP/M. VAX BASIC did have LET, but didn't require it, as I recall. Memory was tight back then, and the 3 or 4 extra characters of program text per statement made a difference, especially in "big" programs. Jan 7, 2018 at 23:09 -
@PeterHull That's interesting, I did my first coding on pencil marked cards attached to a Digital CPU, then mainframes at the old man's research lab and a Digital PDP8 at school. never once saw people use LET without actually using the statement, not in books I read either. Mind you I was encouraged to code in FORTRAN and PASCAL as soon as i got any good, basic being as bad as it was, not even having GOSUB (i.e. reusable functions) in the versions I used. Feb 25, 2018 at 0:45
-
1
The most likely possibility is that it was the most idiomatic choice. Not only is it easy to speak, but rather intuitive to understand. Some could argue, even more so than var
.
But I reckon there's a little more history to this.
From Wikipedia:
Dana Scott's LCF language was a stage in the evolution of lambda calculus into modern functional languages. This language introduced the let expression, which has appeared in most functional languages since that time.
State-full imperative languages such as ALGOL and Pascal essentially implement a let expression, to implement restricted scope of functions, in block structures.
I would like to believe this was an inspiration too, for the let
in Javascript.
-
This answer refers to the lambda calculus 'let expression', which is perhaps not what the OP asked. And it appears to be later than
LET
in BASIC, from 1964. Mar 27, 2018 at 16:14 -
1That entire article is one of several written basically single-handedly by Wikipedia user Thepigdog back in 2014. I'll be judicious and just say that they're all written from a very narrow perspective, so the (utterly unreferenced) claim quoted should be interpreted as saying that Scott's paper "introduced" the let expression to lambda calculus, in the same way that ES6 introduced
let
to JavaScript. Not completely untrue, as far as it goes, but without any real significance. (And not in any way ruling out the possibility that Scott's use of "let" was also inspired by BASICLET
.)– FeRDMar 24, 2020 at 9:15
It could also mean something like "Lexical Environment Type or Tied".. It bothers me that it would simply be "let this be that". And let rec wouldn't make sense in lambda calculus.
-
1I was looking for an answer like this I too doubt it has the general English meaning of "let" Jun 16, 2020 at 6:26
Let uses a more immediate block level limited scope whereas var is function scope or global scope typically.
It seems let was chosen most likely because it is found in so many other languages to define variables, such as BASIC, and many others.
I think JavaScript's indebtedness to Scheme is obvious here. Scheme not only has let, but has let*, let*-values, let-syntax, and let-values. (See, The Scheme Programming Language, 4th Ed.).
((The choice adds further credence to the notion that JavaScript is Lispy, but--before we get carried away--not homoiconic.))))
LET
. There might be earlier language examples.LET
is described on page 7 of the first draft of the manual, dated May 1964, pdf here.const
is a constant, or immutable (read-only) object reference where the object itself is still mutable. Eg. After declaration/assignconst foo = ['bar']
,foo.push('bat')
still would be legal, butfoo = ['bar', 'bat']
is not. But that's too much typing.