Variables in JavaScript

Variables are used to store information in when writing code, that can be manipulated and used later on in the code. JavaScript has loosely/dynamically typed variables, which means you do not have to specify the data type. This means if you create a variable that is a number, you don’t have to specify that it is an integer variable, and it also means you can change the data type of a variable within a program. A variable can start as an integer and then be reassigned to a String.

The below code shows a variable called ‘a’ being declared and assigned a value. The ‘var’ keyword is used to declare (create) a variable which is followed by the name of the variable. Then the single equals ‘=’ operator is used to assign a value to a variable. As you can see, you can declare a variable and initialise it (assign a value) in one line of code. Due to JavaScript’s dynamic typed nature, we can take a variable that is a number and reassign it to a String variable. String variables are wrapped in double quotes and act as words or letters, even if the String contains numbers they’re still treated as words.

Using the arithmetic addition operator on numbers and Strings works differently. When you add two numbers together you get the sum of those numbers, when you add anything to a String then you get the concatenation of the two variables. Concatenation of two variables is like putting one variable on the end of the other, like adding words to the end of a sentence.

Below is a code snippet that shows how the addition operator works with numbers and strings in JavaScript. First there are three variables, two numbers and a String. Then the variable called ‘d’ is assigned the value of adding the variable ‘a’, the number 3, and the variable ‘b’, the number 6. The value of the variable ‘d’ will be the sum of 3 and 6 which is the number 9. The value of the variable ‘e’ will be the String value ’39’. This is because the addition of ‘a’ and ‘c’ contains a String variable, therefore it is will be a String concatenation rather than a normal mathematical addition.

Booleans are another type of variable available in JavaScript. Booleans can be ‘true’ or ‘false’. Boolean can be used to manage the state of certain areas of code, such as if a switch is on or off (true or false). The code below shows how to create boolean variables. Notice how the values are not wrapped in double quotes, because they’re not Strings they are boolean variables.

The final variable type is called ‘null’. A null variable contains nothing, and therefore has no value. The below code shows a variable that is not initialised, this variable has a null value, and contains nothing. You can also assign the null value to a variable, and make it clear that is contains nothing. Logging out the value of ‘a’ will result in the string “undefined” and logging out the variable ‘b’ will result in the string value “null”;

To summarise, variables can be used to store information to be used later in a program. They can be manipulated, or assigned a result from a function such as an addition or concatenation. Also, when adding String variables the result will be a concatenation rather than a normal mathematical sum, even if only one of the variables is a String.

Leave a Reply