1

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?

2
  • Did you try what happens when you replace var in that code with either let or const? Commented Jul 12, 2017 at 5:56
  • Sure, including the ternary operator. Just wanted to know if there's an idiomatic JS way to declare an argument, set its value conditionally, and pass it to a function. Commented Jul 12, 2017 at 6:15

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Excellent profound answer, thanks a lot. Does the way I use var make sense, or should I declare the variable first and then assign? Any idiomatic JS recommendation?
@AdamMatan - It's generally not good to repeat a var definition. While JS allows it, it tends to lead someone reading your code to think you are declaring a new variable (when in fact, it may already exist) and it implies block scoping (though var is function scoped). So, better to put var token before the if statement and just assign the variable (the same way I did in my let example).
Perfect. Thanks!
What about assignment without a quantifier? Simply token =?
@AdamMatan - Never assign to a variable that has not been declared. That creates an implicit global which is really bad coding style that can lead to programming accidents. The most common one is to refer to i in a for loop without any declaration and then you wonder why a function your for loop breaks when you call a function from it (that also does the same thing and they trounce each other's use of i). So, always explicitly declare it var, const or let. And, even better, run in strict mode where it's an error if you assign to a variable without declaring it first.
2

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.

1 Comment

And const suffers from the same scoping issue as let.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.