a guest May 15th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. A note on variables usage in JavaScript
  3. Scope is the way in which variables interact with each other. If we misunderstand the scope of our code, we risk our program acting in a manner we didn’t intend it to. A variable set at the global scope level is one that will be recognized throughout the program, variables are said to be global if they have been declared outside of a function. A variable is said to be a local/block variable if it has been declared within a function. The local variable is intended for use within that function only and will not be used again in the program unless called upon, however a global variable will still be in use.
  5. Global variables are to be avoided because of the unintended side-effects they have on a program. A side-effect will reach outside its scope and alter values in the parent scope – which are unintended side-effects. This leads the code to becoming ‘indeterminate’ whereby a function will return a different value at a different time – leading to bugs and difficulty in handling errors. Functions that don’t have any side effects are known as ‘pure functions’ – they are determinate and will return the same value all the time. For these very reasons it’s important that we aim to keep all functions ‘pure’ and steer clear of global variable use.
  7. One way to steer clear of global variable use is the implementation of JavaScript’s ‘strict-mode.’ Strict mode will only let us declare variables with either ‘var,’ ‘let’ or ‘const.’ If we don’t then an error will be returned. There are two ways to implement strict-mode: 1. declaring “use strict” at the top of the code, so it acts globally or 2. declaring within a function, so it’s only recognized locally
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand