Every programming languages come with basic building blocks; variables are one of those blocks. Variables are the storage compartments that make dreams come true. In this chapter, we’ll be discussing a variable’s purpose and scope.

Purpose Of Variables

A variable’s purpose is to store values in your program. Variables act as an identifier or convenient nickname for a value. A variable will represent that value so long as it’s not changed. Here’s a quick example.

var mynumber = 2;
console.log(mynumber); //2
mynumber = 3;
console.log(mynumber); //3

This example demonstrates the fundamental idea; “mynumber” represents 2 then 3. Furthermore,  “mynumber” can be anything we want. This comes with clear benefits when writing code. Now, I think you can guess what those clear benefits are before looking at the next statement.

If you guessed reusability and flexibility you’re correct; variables allow programmers to reuse values in code, reducing the amount of work you have to do. Now, not all variables are created equal; variables have scopes.

Variable Scope

As mentioned above, all variables have a scope. “Scope” is the area in the program where the variable exists. Scope also applies to when certain variables and functions are available for you to use; In most cases, the code near the top is created first and should be a rule of thumb for understanding when and where code exists.

An understanding of scope is extremely important to not just variables, but functions which will be introduced later. In JavaScript, there are three variable keywords(reserved words that have special meaning to the language), var, let, and const. Each keyword follows a different scope rule. These different scope rules are block and function scope.

Block Scope

Block scope is when you surround your variables in curly brackets; this includes functions also. Variables created with let and const fall into this category. The difference between let and const is that const can’t be reassigned to an another value. Now, here’s an example of both keywords.

const a = 3;
let b = 70;
console.log(a, b); //3 70
console.log(a, b); //Undefined error....

As you can see in this example, you don’t have access to the variable outside of the curly brackets. Which leads me the last keyword, var and function scope.

Function Scope

Function scope is when your variables only exist within a function; curly brackets will not limit the scope of the variable. Variables created with the var keyword fall into this category. Let’s use an example to compare this to block-scoped variables. Here’s an example.

var a = 3;
function Test() {
var b = 10;
console.log(b); //10
console.log(a); //3
console.log(b); //b is not defined...

In this example, we create both a variable a and b, but as you can see a is available even outside of the curly brackets. Within the Test function, b is not available outside of the function’s scope. Now, an important thing to note is that variables created with var can be useful if you want a wider scope. However, use let and const in most circumstances for more fine-tuned controls.

An understanding scope will be paramount when writing code for your game. This should be the basis for your understanding of coding your game. Variables are the backbone of all your game code and will make it easier to script for your game.

%d bloggers like this: