If you do not declare a variable (explicitly create it in the current scope) using var , let or const , then (in lax mode) you create an implicit global variable.
Global variables are a fantastic way to allow different functions to overwrite each other's variables (i.e. they make code difficult to maintain).
If you use var , the scope of the variable is limited to the current function (and everything inside it is possible to embed functions).
(The scope const and let and the variables for the current block instead of the function usually make managing variables even easier than var .)
Google Adsense uses global variables because it breaks scripts into two separate parts (one local and one remote). A cleaner approach would be to call the function defined in the remote script and pass parameters as arguments instead of calling them from the global scope.
Modern JS should be written in strict mode , which prohibits implicit global variables (preferring to explicitly declare them at the top level instead, thereby preventing random global variables from being sealed).
Quentin Oct 08 2018-10-10 16:59
source share