June 2018
Beginner
288 pages
6h 31m
English
Prior to ES6, JavaScript required var to define variables. If we forget to define a variable explicitly before assigning to it, we’ll accidentally define a global variable. The ’use strict’; directive saves us from that error. In short, all variables should be defined before their first use. However, var is not the right choice, as we’ll see here.
var does two things poorly. First, it does not prevent a variable from being redefined in a scope. Second, it does not have block scope. Let’s explore these two issues with examples.
It’s poor programming practice to redefine a variable in the same scope as that often leads to errors in code. Here’s an example where a variable max is redefined.
| 1: | 'use ... |
Read now
Unlock full access