Web Platform Javascript # Variables II You can think of a variable as a named signpost, which points to a particular place in the computer's memory. This has a few implications worth remembering: 1. the variable is not the data itself 2. several variables can point to the same data 3. if there are no signposts to data, it is lost ```js let touchCoordinateX = 20; let subjectLine2 = subjectLine; // Both point to the same data ``` If the user pressed on the screen, we want to work with the _x_ and _y_ coordinates of the process. If we're writing an email sending app, we might want # More than 'var' With the ES6 version of Javascript (supported by all browsers), there are more ways of defining a variable beyond the `var` keyword. As you may have realised, variables declared with `var` can be used in any old way - they can be changed and in some situations accessible outside of the scope they were defined in. ## const `const` allows you to declare variables which won't be reassigned. Meaning the following will throw an exception on the second line: ```js const name = "John"; name = "Bob"; ``` For objects, you're still able to change their value. The following is fine: ```js const person = { name: "John", age: 98 } person.name = "Bob"; ``` But again, you'll get an error if you then try: ``` person = { name: "Alice", age: 76 } ``` ## let For the most part, `let` should be your new `var`. Like `var`, variables can be reassigned, however you get one useful benefit: variables declared with `let` are only accessible inside the scope they were declared in. This is really how variables _should_ work, and how they work in most other computer languages. No problem with this: ```js let name = "John"; name = "Bob" ``` Let's focus on how it differs from `var`. In the following, `i` is accessible outside of the loop, even though it's declared inside of it: ```js for (var i=0; i<10;i++) { ... } console.log("i ended up at: " + i); // 10 ``` If we change it to `i` to be declared with `let` however: ```js for (let i=0; i<10;i++) { ... } console.log("i ended up at: " + i); // ReferenceError: i is not defined ``` This leads to much safer, more understandable code.