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.
14 Comments
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).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.I guess it follows mathematical tradition. In mathematics, it is often said "let x be arbitrary real number" or like that.
3 Comments
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 Comments
let is about at all. let is all about scope.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.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.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 Comments
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.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 Comments
LET in BASIC, from 1964.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.)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 Comment
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.LETis described on page 7 of the first draft of the manual, dated May 1964, pdf here.constis 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.