Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Why do many files in the lib directory use var sometimes and let other times? #28009

Closed
MilesZew opened this issue Jun 1, 2019 · 2 comments

Comments

@MilesZew
Copy link

MilesZew commented Jun 1, 2019

  • Version: 12.3.1

While looking through the node.js source code, I have found quite a few var declarations. In the same files I have also found let declarations. let declarations are almost always considered better, and can always be used in place of var declarations. I was thinking about fixing this and creating a PR but wanted to know if there was a specific reason for using var sometimes and let other times. Scope could be a reason, but could be transformed like this:

if(/*...*/) {
    var foo = 'bar';
}

//is the same as
let foo;
if(/*...*/) {
    foo = 'bar';
}
@renanbastos93
Copy link

The var is a variable global than when you try declaring a new variable with the same name will throw an error. Just the variable of the type let respect a scope.

@Trott
Copy link
Member

Trott commented Jun 1, 2019

Most of the files in question were written before let and const existed. Changing them en masse to let and const would be considerable churn which risks the introduction of subtle bugs, all for no benefit to the end user.

Many people have opened pull requests to do this and we always close them. Instead, we migrate slowly over time when nearby code is being modified for other reasons. I'm not a fan of that approach either, to be honest, but it has been the sense of the maintainers for a long time that that is the best way to handle this.

An additional reason that we did not convert to let previously is that there were situations where let was significantly less performant than var. I do not believe that is the case anymore, although the potential that it could still be lurking might be another reason some maintainers or reluctant to do this.

Speaking personally, I'm not strongly opposed to doing this, but I'm also not enthused.

Hope this answers your question. I'm going to close this, although you and anyone else should feel free to continue to comment and/or re-open it if this answer does not suffice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants