Adam Matan
Adam Matan

Reputation: 136519

Javascript: Should an argument passed to a function be defined as 'let', 'const' or 'var'?

Consider the following code snippet:

// Set 'token' to have string value or null

if ("nextForwardToken" in event.queryStringParameters) {
  var token = event.queryStringParameters.nextForwardToken;
} else {
  var token = null;
}

getData(token, callback);

The token parameter for the function getData may either be null or a string.

The code block on the caller side defines the variable token and sets it to the right value.

What's the right way to quantify the token variable on the caller side? Should it be let, const or var?

Upvotes: 1

Views: 2981

Answers (3)

jfriend00
jfriend00

Reputation: 708206

Your question is a bit confusing.

You are asking about the "caller" side, but the side of the code that is calling the getData() function clearly defines the token variable as var already right there in your code. You could use let if you wanted to narrow the scope to block scope if you defined let token before the if statement. You can't use let in place of var in your code because that would define the variable only within your if block, but you want to use it outside that block.

Using let:

So, you could use let like this:

let token;
if ("nextForwardToken" in event.queryStringParameters) {
  token = event.queryStringParameters.nextForwardToken;
} else {
  token = null;
}
getData(token, callback);

If this was not already at the top level function scope, then using let would narrow the scope to just the parent block.

Using const:

You can't really use this structure with const because const can only be defined once and must contain an initializer. So, if you wanted to use const, then you could do this:

const token = "nextForwardToken" in event.queryStringParameters ? event.queryStringParameters.nextForwardToken : null;
getData(token, callback);

Of course, you could even do this without the temporary variable at all:

getData("nextForwardToken" in event.queryStringParameters ? event.queryStringParameters.nextForwardToken : null, callback);

Inside of getData():

How you define (const, let or var) whatever you pass to getData() has no effect at all on how the function argument works inside of getData(). So, if your question was really about that, then it doesn't matter inside of getData(). Function arguments in both ES5 and ES6 are always mutable (like var or let) and that is not something that is currently changeable in the language. They are separate variables that are assigned the values you passed them, thus they don't inherit any const attributes from what you passed and their scope is defined by the new function definition so their scope has nothing to do with whether the passed variable was const, let or var.

So, if you have your getData() function declared as:

function getData(theToken) {

}

Then, theToken is a mutable argument and there is no way to make it const. Since it is essentially declared at the top level of the function, there would really be no difference between var and let so it doesn't really matter between those as the scope is already the whole function and it's already essentially hoisted.


In ES6, there are interesting work-around possible such as this:

function getData() {
    const [theToken] = arguments;

    // access theToken here as const

}

One could still override that by accessing arguments[0] directly, but that would certainly have to be intentional, not accidental.

What's the right way to quantify the token variable on the caller side? Should it be let, const or var?

It's like either var or let. Since it's at the top scope of the function and already hoisted, there really is no difference. If you mentally want to pick one, then say it's like var since it is always function scoped.

Upvotes: 2

HuyTran
HuyTran

Reputation: 153

Why you don't write your code simply like this:

// Set 'token' to have string value or null
let token = null;
if ("nextForwardToken" in event.queryStringParameters) {
  token = event.queryStringParameters.nextForwardToken;
}

getData(token, callback);

Upvotes: 1

attempt0
attempt0

Reputation: 834

It should be var, using let and const will make the variable block scope and only exists in the if block. Using const is also unessessary excepts you would use it and would not change it in the future.

Upvotes: 2

Related Questions