Jump to content

JavaScript: Difference between revisions

Line 98: Line 98:


===<code>var</code> vs <code>let</code>, <code>const</code>===
===<code>var</code> vs <code>let</code>, <code>const</code>===
* <code>var</code> is the old way of declaring variables. It has function-scope rather than block-scope. Note that <code>var</code> can also be redefined without errors.
* <code>var</code> is the old way of declaring variables. It has function-scope rather than block-scope. Note that <code>var</code> can also be redefined which can hide errors.
* <code>let</code> and <code>const</code> are post-ES6 ways which have block-scope.
* <code>let</code> and <code>const</code> are post-ES6 ways which have block-scope.


These days you should never use <code>var<code>.   
These days you should never use <code>var<c/ode>.   
The Google [https://google.github.io/styleguide/jsguide.html JS Style Guide] and [https://google.github.io/styleguide/tsguide.html TS Style Guide] suggest using <code>const</code> by default.
The Google [https://google.github.io/styleguide/jsguide.html JS Style Guide] and [https://google.github.io/styleguide/tsguide.html TS Style Guide] suggest using <code>const</code> by default.