Search code examples
javascriptnode.jsvar

why many examples of javascript still using 'var' instead of 'const' and 'let'?


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?


Solution

  • 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.