6

Such as cubejs:

<script>
  var cubejsApi = cubejs(
    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTIzOTk5MjcsImV4cCI6MTU1MjQ4NjMyN30.SOO-A6GfGH7ar3EoeBb0cjj10BVxO3ffjvmqQziXIZA',
    { apiUrl: 'http://localhost:3000/cubejs-api/v1' }
  );
 ...
</script>

Such as the example of vue or nodejs Should I use 'var' nowadays?

Dan
  • 59,490
  • 13
  • 101
  • 110
radiorz
  • 1,459
  • 4
  • 18
  • 36
  • 2
    "*Should I use 'var' nowadays?*" no. – VLAZ Jan 11 '21 at 08:16
  • 3
    Because going back to update every single code example on the internet using `var` hasn't happened yet! – Dan Jan 11 '21 at 08:17
  • 2
    New stuff? -> Use `let`/`const`; Old stuff? -> If it works, then don't touch it. If you're going to refactor the code anyway, then this might be another point on the list. – Andreas Jan 11 '21 at 08:20
  • 1
    [What are some reasons to use var instead of let in javascript?](https://stackoverflow.com/questions/41031342) and [Can I completely stop using var?](https://stackoverflow.com/questions/50335497) – adiga Jan 11 '21 at 08:30
  • Thanks for your answer. I use express-generator to generate a new project.and it still use 'var' like this: ``` var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); ``` – radiorz Jan 11 '21 at 08:30
  • 1
    @Andreas Google's coding style guide (which I promote every time my employers think the company need a coding standard) specifically has a section that says that you should not edit old code unless you have a bug to fix. The main reasoning is that big changes for something trivial pollutes repo history and makes reasoning about why the code changed much harder when debugging – slebetman Jan 11 '21 at 08:32
  • @Andreas I interpreted your comment as "don't change old code" and therefore I agreed with you with an anecdote about Google's coding standard also agreeing with your comment. If I interpreted your comment wrong and you did not mean "don't change old code" then I take back my comment. But I certainly was not disagreeing with you. On the contrary I was adding evidence that your opinion is regarded as correct by a lot of professionals – slebetman Jan 12 '21 at 01:39
  • @slebetman Then I understood your comment the wrong way. Sorry... :D – Andreas Jan 12 '21 at 11:17
  • Your question should never have been closed. – Andrew Howard Apr 07 '23 at 11:03

2 Answers2

7

Still a year or two back, Babel was still transpiling let and const to var, albeit, if it find two variables or constant declared with let and const respectively across different blocks in the same function, it would be given different names, to avoid clashes.

The main reason for transpiling to var was, few browser weren't updated to support ES6 syntax. Currently, most of the browsers support ES6/ES7 codes without transpiling, Firefox even supports some of the 2021 feature. You can check the compatibility here.

The reason why there are many codes and examples still use var is because, not every dev has gone through their repos and updated the code.

So should you use var now?

No! Unless there's really, really, reeaaally a specific reason for you to use var, use let or const. Even if you find a reason to absolutely use var, perhaps blame your coding style and change.

Pushkin
  • 3,336
  • 1
  • 17
  • 30
  • 1
    The proclamation that `var` is inferior and that your style is bad if you use `var` is absurd and hubristic. `var` is still useful if you want to declare variables in blocks like `if` and `try` blocks so you don't have to declare those variables at the top like a C++ caveman. – B T Jan 19 '23 at 15:31
  • That, that is exactly what I mean by bad coding practice. @BT. This cavemen practice improves readability and predictability that leads to better DX when you're working in a team – Pushkin Jan 21 '23 at 07:20
  • 1
    That's just like, your opinion man. I disagree that its bad coding practice. Functions are often substantially more readable when variables can be declared in the same line as they're initialized, vs having a ton of empty `let`s running around up at the top - especially when linters urge people to declare each empty variable on its own line. Its just noise that spreads the meaningful information in a function apart. Its anti readable, and teams vs not teams have nothing to do with it. – B T Jan 24 '23 at 21:32
1

const and let is better in the majority of cases, but there are still a few cases where var is more correct.

For you question directly, var is probably still used for legacy reasons. It is supported in all versions of JavaScript and it would be a bother to change every example on the internet.

The only real advantage to var is it's compatibility. If you are writing for old platforms like .HTA tools or you must support older platforms like old phones or simply old browsers, then var is your only real option.

Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • 3
    "*but there are still a few cases where var is more correct.*" I've yet to find those cases. I've asked a lot and the the only answer I was really given was "var can be used when you want to re-declare a variable without an error". While *true*, the response to that is that you *shouldn't* be redeclaring a variable. If you do, you have incorrect flow of your application and you're using `var` as a band-aid to avoid correcting it. So, other than redeclaration, when is `var` *more correct*? EDIT: to clarify, when is `var` more correct when you can also use `let`/`const`? – VLAZ Jan 11 '21 at 08:28
  • @VLAZ `var` is more correct if you don't know the capabilities of the engine. There are fine arguments against `var` on modern websites, but the alternatives are non-functional in `.HTA` tools and on old platforms. As i say in my answer: Legacy Reasons. I suppose there is an argument about simplicity for new users too. Limiting the amount of `keywords` they have to know to get started. I'm not against `let`/`const`, especially on the modern web, but to say there is no use-case for `var` is simply wrong. – Emil S. Jørgensen Jan 11 '21 at 08:44
  • 1
    As I clarified *when you can use `let`/`const`*. It should be self-evident that if you cannot use them, then you can only rely on `var`. But if you actually have alternatives, I don't see any practical advantage. Even simplifying things for beginners is a bad reason - `var` is just *more complex*, so it's easier to get into a weird situation with it. [For example](https://stackoverflow.com/questions/1451009/javascript-infamous-loop-issue). If you truly want to reduce the things beginners need to learn, then just tech them `let`. Assuming `const` is such a huge mental burden. – VLAZ Jan 11 '21 at 08:49
  • @VLAZ Whoops, just saw your edit. When `let`/`const` is available they are the better alternatives and they should be used on the modern internet. If you know that is where your code will run, then no worries. But if you have something that absolutely must run on anything that might access it, `var` might still be the better keyword. – Emil S. Jørgensen Jan 11 '21 at 08:59
  • Note that you can still use Babel in many cases to transpile the code. Thus you'll be able to use `let`/`const` even if the target environment doesn't support them. Babel will make sure to preserve the semantics of your code. e.g., `function foo(x) { if (true) { let x = 5;} return x; }` will not reassign the parameter value. Whether or not Babel will be desirable for a given project can vary but it does mean there is less reason to use `var` (among other things) and have less issues with the code overall. – VLAZ Jan 11 '21 at 09:14
  • @VLAZ Babel is an awesome program and i agree that transpiling is the better option of actual development. But by that logic there is no reason to write JavaScript over a compiled language like TypeScript at all.. Again, in exclusively modern engines use modern keywords. If you need to support the old, then you need to use the old. Letting a compiler write `var` for you doesn't change the need for `var`. And it especially doesn't negate the need to know WHY `var` is needed in that context. – Emil S. Jørgensen Jan 11 '21 at 09:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227141/discussion-between-emil-s-jorgensen-and-vlaz). – Emil S. Jørgensen Jan 11 '21 at 09:28
  • `var` is still useful for hoisting outside blocks like `if` and `try` blocks so you don't have to declare those variables at the top like a C++ caveman. – B T Jan 19 '23 at 15:24