Aditya
Aditya

Reputation: 425

Why don't we use keywords like "let", "var" and "const" in function parameters in javascript?

function myFunction(let name){
    console.log("hello "+name);
}

myFunction("adi");

The above code produces an error. Why?

Upvotes: 8

Views: 2666

Answers (2)

sçuçu
sçuçu

Reputation: 3070

It is the way language works, and how its syntax is defined. However there must be a reason about it.

I can reason a little bit about it and share my reason.

There are two scope types in JavaScript,

  1. Function scope,
  2. Block scope

var defines a variable with Function scope whereas let and const define block scope variables.

Function parameters are in function scope, which makes sense, at least with the current mindset of the language since it does not make sense to pass a parameter to a function to be in scope in block. This would arise questions regarding ambiguities of the scope the parameters belong to, that would be overcome by having another syntax to maybe label parameters and blocks in a way. With another mindset of language designing that could be possible and even useful. However, the current state of the language is not like that and it makes sense in itself.

Also historically there was only var. let and const came way later than var, hence there was only function scope in the language and so the parameters had to be either in the global scope or in the function scope and it has designed to be in the function scope which is not unusual and very common in ALGOL-like languages. When let and const came one reason they came was having a block level scope other than function blocks, like if conditions, for etc. So they did not applied to the function parameters which is usually more useful when all visible in the their function.

Just to clarify what I meant about the hypothetical alternative scoping in an alternative universe, or parallel universe, JavaScript, I want to construct an example with yet another questions arisen to be answered:

function f(x:a, x:b, y:b) { // parameters' scope labeling syntax
    scope a: if(x > 1 && !y) return x + 1; // block's scope labeling syntax
    scope b: return x * y;

}

f(3:a, 9:b, 0:b) // returns 4
f(3:a, 9:b, 1:b) // returns 27

Upvotes: 2

Barmar
Barmar

Reputation: 782775

let and const are relatively new features in the language, they were added in EcmaScript 6. Prior to this, all variables were either global or local to the function. In the function body you declare local variables with var, but parameters are automatically local so there's no need for a keyword to distinguish them from global variables.

When let and const were added to the language, there was no need to change the way parameters are declared. The distinction between let and var is not useful: let is for declaring block-scoped variables, but the block for a function parameter is the same as the whole function. It might be useful to be able to declare a function parameter const, so you can't reassign it, but I guess the designers didn't feel it was useful enough to make this change to the syntax (they already made it more complex by adding destructuring and default values).

Upvotes: 4

Related Questions