Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Global and Local Variables in Javascipt

1/3/2016

0 Comments

 
Whenever I come back to pure Javascript it's a bit uncomfortable because Javascript has some different rules from most language about defining variables. This post is meant to be a quick refresher on defining variables in Javascript. 

Javascript Scope

Remember that scope of the variable refers to where in the code it has access to the variable. In Javascript we cannot use access modifiers like public or protected as in other languages. 

Local Javascript Variables

Variables that are defined inside of a function are only available to be used inside of that function. These are called local variables, and in general they are good to use because they get garbage collected once the function ends (sort of). Local variables would make a good choice should only be used on temporary variables, intermediate calculations, and things that aren't needed later in the program. Here's an example where material is a local variable:
// code here can not use material

function myFunction() {
    var material = "sand";

    // code here can use material

}
Remember, this is pure javascript so don't go trying to tell the JS interpreter that this is a String (even though it is) or use data types when defining your variables!

Global Javascript Variables

Just define a variable outside of a function but inside of the class, and it will be available inside of all function in that class. In the example below, the variable material is now available throughout the entire class:
var material = "sand";

// code here can use material

function myFunction() {
    
    // code here can use material

}

Automatically Global Variables

Although this has undoubtably, caused a lot of face-palming over the years, Javascript has an interesting little "feature" that occurs when you omit the var keyword. If you assign a value to a variable that has not been declared without the var keyword then it becomes global automatically. Don't ask me why; that's just how it is! If you really hate it that much, switch to Typescript, but it is what it is. Here's an example where the material variable automatically takes global scope:
// code here can use material

function myFunction() {

    material = "sand";
    // code here can use material

}

Sharing a "Public Javascript Variable" with Another Class

Once you start getting familiar with Javascript and are itching to build a more than a few functions you'll want to put on your OOP hat and break the code into separate files. I would say separate classes, but up until ES6 there were no concept of classes in Javascript. For that reason I'm going for an old-school route of defining a Java file with it's main function. Let's say we have a class, CountManager, and it needs to read the value of the variable age​ in a class called Person. 
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021