368

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?

6
  • 12
    BASIC, invented in 1962, used LET. There might be earlier language examples. Commented Aug 1, 2017 at 23:08
  • 9
    BASIC was invented in 1964, not 1962. See also here. The usage of LET is described on page 7 of the first draft of the manual, dated May 1964, pdf here. Commented Mar 27, 2018 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
    Commented May 25, 2018 at 15:04
  • 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? Commented Dec 9, 2020 at 0:32
  • 1
    @ChristofferBubach well, we have TypeScript for strictly-typed JavaScript... 😁 Commented Aug 5, 2022 at 17:29

8 Answers 8

427

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.

14
  • 123
    And the official Mozilla Documentation page cites this page for this question developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Aug 21, 2017 at 7: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
    Commented Oct 25, 2017 at 1: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
    Commented 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?
    – SamB
    Commented Jun 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.
    – Nacht
    Commented Oct 29, 2019 at 23:42
110

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

3
  • 5
    I 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.
    – duduwe
    Commented Apr 21, 2020 at 13:39
  • You guess? It would be nice to have a better (improved) answer.
    – carloswm85
    Commented Sep 23, 2021 at 23:25
  • 3
    @carloswm85 I guess it would be nice indeed. Commented Sep 25, 2021 at 11:05
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

4
  • 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. Commented Jul 13, 2017 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 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
    Commented Sep 26, 2019 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
    Commented Jan 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. Commented Jan 22, 2020 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"

4
  • 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. Commented 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.
    – Peter Hull
    Commented 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. Commented Feb 25, 2018 at 0:45
  • 1
    You cannot redeclare a let variable. Commented Mar 3, 2020 at 7: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.

2
  • 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. Commented Mar 27, 2018 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
    Commented Mar 24, 2020 at 9: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
  • 1
    I was looking for an answer like this I too doubt it has the general English meaning of "let"
    – Meenohara
    Commented Jun 16, 2020 at 6:26
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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.