364

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?

Shog9
  • 156,901
  • 35
  • 231
  • 235
Vitaly Zdanevich
  • 13,032
  • 8
  • 47
  • 81
  • 12
    [BASIC](https://en.wikipedia.org/wiki/BASIC), invented in 1962, used `LET`. There might be earlier language examples. – David R Tribble Aug 01 '17 at 23:08
  • 9
    [BASIC](https://en.wikipedia.org/wiki/BASIC) was invented in ***1964***, not 1962. See also [here](http://www.dartmouth.edu/basicfifty/basic.html). The usage of `LET` is described on page 7 of the first draft of the manual, dated May 1964, [pdf here](http://www.dartmouth.edu/basicfifty/basicmanual_1964.pdf). – Joseph Quinsey Mar 27 '18 at 16:10
  • More specifically `const` is a constant, or immutable (read-only) object reference where the object itself is still mutable. Eg. After declaration/assign `const foo = ['bar']`, `foo.push('bat')` still would be legal, but `foo = ['bar', 'bat']` is not. But that's too much typing. – user982671 May 25 '18 at 15:04
  • Isn't it simply the English verb "let" (as in, "let it be so")? Why would this be in question? – 2540625 Jul 26 '20 at 17:28
  • 1
    Yay, yet another feature nobody asked for or needed. You get the same thing with IIFE. Javascript did exactly what it needed to do over 15 years ago, now they are just competing with the latest and greatest keywords and construct for no god damn reason. Personal favorites include the class construct in a supposedly prototyped object model. Strict typing and optional types any day now, eh? – Christoffer Bubach Dec 09 '20 at 00:32
  • 1
    @ChristofferBubach well, we have TypeScript for strictly-typed JavaScript... – Gwyneth Llewelyn Aug 05 '22 at 17:29

8 Answers8

425

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.

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
exebook
  • 32,014
  • 33
  • 141
  • 226
  • 122
    And the official Mozilla Documentation page cites this page for this question https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/let – Sarath Chandra Aug 21 '17 at 07:13
  • 11
    It'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). – brichins Oct 25 '17 at 01:18
  • 18
    In other words, `let` has been traditionally used to describe a constant, as in `let 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. – fijiaaron Mar 09 '18 at 20:41
  • 1
    I disagree that `let` is close to `var`. Also Scala does not use `let` so I don't see how it is relevant. – Aluan Haddad Apr 11 '18 at 03:32
  • 7
    @fijiaaron: Nonstandard scoping how? Isn't whole-function-regardless-of-what-block-you-declared-it-in kind of odd to start with? – SamB Jun 04 '19 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. – Nacht Oct 29 '19 at 23:42
  • @Nacht: Nope, c, like other symbols, can refer to just about anything. If you remember the Pythagorean theorem, c _is_ the length of the hypotenuse. Even π is used for many things other than a value near 3, such as permutations or [probability distributions](https://math.stackexchange.com/questions/2670108). And people _do_ say "Let c be the speed of light," or "Let c be the length of the hypotenuse," just to make sure the author and the reader are on the same page, so to speak. Read some math papers. You'll see that "let" is absolutely a standard way to bind a symbol to a meaning. – Matt Jul 11 '20 at 20:43
  • @Matt hmm... at this point I have no idea why I objected so strongly to fijiaaron... maybe I didn't read him very carefully and thought he was saying `let` is used for constants in javascript? at any rate, I am plenty familiar with "let x be so and so" as I actually mentioned in my comment. I only objected to it being used for constants. Maybe that still happens though, and I really do need to read more math papers. – Nacht Jul 12 '20 at 03:50
  • @Nacht: Ah, fijiaaron was interpreting the answer as saying that there is a tradition (from Clojure, F#, Scala) that `let` is for constants. This supposed tradition would make it seem like JavaScript is doing things backwards. But of course there is no such tradition, and the answer could have been clearer about that. Also, the final word "instead" was presenting a contrast with `var`, not with tradition. Again, easy to misread as saying that block-level scoping is nonstandard. But of course it is standard. With these misreadings, fijiaaron's minor anti-JavaScript rant is more understandable. – Matt Jul 12 '20 at 10:26
  • The first paragraph's main point seems to be that constants are a higher level of abstraction and more powerful than variables. This seems highly dubious. – Matt Jul 12 '20 at 10:27
  • 1
    The upvotes on @fijiaaron's comment indicate that many people understand this answer to claim that `let` is traditionally used for constants. This is _not_ the case. As a programming construct, `LET` first appeared in 1964 BASIC, which had no immutables at all. That was a decade before Scheme. The reason `let` is used for immutables in Scheme/Clojure/Scala is just because immutables are so common in functional languages. As for F#, being a multi-paradigm language it uses `let` for immutable _and_ mutable variables. In mathematics, "let" is used for any type of variable definition or property. – Matt Jul 12 '20 at 10:31
  • The second paragraph's point, that they just needed another keyword, and `let` is a traditional one, is right on target. – Matt Jul 12 '20 at 10:39
  • Math has actually been around longer than BASIC – fijiaaron Aug 25 '20 at 23:13
  • 1
    It seems like a poor choice, anyway. Sure its "traditional" but it has little intuitive meaning compared to var and const. Surely a word like "local" or "block" would have made it extremely obvious? – O'Rooney Nov 03 '22 at 22:59
106

I guess it follows mathematical tradition. In mathematics, it is often said "let x be arbitrary real number" or like that.

Tigran Saluev
  • 3,351
  • 2
  • 26
  • 40
81

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

Evan Brooks
  • 906
  • 6
  • 5
  • 22
    I 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. – jinglesthula Jul 13 '17 at 14:04
  • 1
    Just 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](https://www.w3schools.com/js/js_let.asp) of `let`. 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 use `let`. – Alain Sep 26 '19 at 19:59
  • 1
    I 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 inner `toPrint` no longer exists, so `console.log(toPrint)` refers to the _outer_ `toPrint`. It's not that anything's ignored, it's just that `let` variables have finite lifetimes defined by their scope. – FeRD Jan 13 '20 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. – Evan Brooks Jan 22 '20 at 20:23
40

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"

Charlie
  • 22,886
  • 11
  • 59
  • 90
  • 7
    While 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. – wide_eyed_pupil Apr 10 '17 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. – Peter Hull Jan 07 '18 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. – wide_eyed_pupil Feb 25 '18 at 00:45
  • 1
    You cannot redeclare a let variable. – Pedro Lobito Mar 03 '20 at 07:54
11

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.

Community
  • 1
  • 1
oxalorg
  • 2,768
  • 1
  • 16
  • 27
  • 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. – Joseph Quinsey Mar 27 '18 at 16:14
  • 1
    That 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 BASIC `LET`.) – FeRD Mar 24 '20 at 09:15
3

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.

1

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.

OG Sean
  • 971
  • 8
  • 18
1

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.))))

Eric H.
  • 495
  • 5
  • 5