Javascript

Javascript Variables

One of the most important concepts in Javasript, as with most programming languages, is the ability to define variables. A variable stores data. Some types of variables can later be changed, based on logic we use in our code. Let's take a look at how variables work in Javascript.

Defining Variables

There are two main types:

  • Constants - these cannot be updated later, and contain constant data.
  • Variables - as the name suggests, these can be changed.

We can define these two main types of variable in our code using the keywords let, var and const.

Variables

Var

The var keyword was the first way to define variables introduced to Javascript. As with all other ways to define variables, var follows this pattern:

javascript Copy
var myVariable = 'some value';

Above, the keyword var is used to say we are setting a variable called myVariable, and that the value of that variable is some value. Since we used quotation marks, Javascript will assume this is a string.

Let

The let keyword is a new addition to Javascript. It is the same as var, with the only difference being it has block scope. We will cover logical statements and functions later, but this example may help to show what this means:

javascript Copy
let myVariable = 'some value'; if(myVariable == 'some value') { myVariable = 'some other value'; // Within this 'block', myVariable now is equal to 'some other value' } // Outside the block, myVariable is equal to 'some value'

Updating Variables

As we showed in the last example, we can update variables by simply referring to them again and changing their equal to value. This will only work for var and let, however, since const variables cannot be changed.

javascript Copy
let myVariable = 1; // myVariable is 1 myVariable = 2; // Now myVariable is 2

Note, if you try to define a variable twice with the same name, using var or let both times, Javascript will give you an error. You can only define a variable once, and after that, you must update it as shown above.

Constants

Constants are defined using the const keyword. As mentioned before, these cannot be changed. They are defined in the same way as other variables, i.e.:

javascript Copy
const myConstant = 1;

The above refers to a constant which cannot be changed or updated.

Dynamic Typing in Javascript

In other programming languages, it is necessary to define what type the variable is when defining it. In Javascript, the type is assumed based on how you enter the data. This is called dynamic typing. Some examples are shown below:

javascript Copy
let i = "1"; // this is a string, since we used quotation marks let j = 1; // this is an int, since we used no quotation marks let k = true; // this is boolean, since we used no quotation marks let l = "true"; // this is a string, since we used quotation marks

As you can see, whether you use quotation marks or not can really affect your code. If you use quotation marks around a number, for example, it will be a string, and if you tried to add this "string" to a number later, it wouldn't work! So understanding these types is really important.

Last Updated Sunday, 13 June 2021
Click to Subscribe Subscribed

Subscribe

Subscribe to stay up to date with our latest posts via email. You can opt out at any time.

Not a valid email