Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 809
Vitaly Zdanevich Avatar asked Jun 20 '16 07:06

Vitaly Zdanevich


People also ask

What is the reason to choose the name let as a keyword in JavaScript?

let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

What is block scoped variable in JavaScript?

Block scoped variables: A block scoped variable means that the variable defined within a block will not be accessible from outside the block. A block can reside inside a function, and a block scoped variable will not be available outside the block even if the block is inside a function.

Is Let block scoped?

let is block-scoped Hence when you try to access b outside of if block, an error occurs (as shown above in the program). Note: The variables declared inside a function will be function scoped for both var and let .

Is Let block scope in JavaScript?

Block Scope ES6 introduced two important new JavaScript keywords: let and const . These two keywords provide Block Scope in JavaScript.


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

like image 198
exebook Avatar answered Oct 12 '22 21:10

exebook


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

like image 27
Tigran Saluev Avatar answered Oct 12 '22 23:10

Tigran Saluev