Variables in JavaScript
Tags: javascript, beginners, programming
Published: 2022-11-09 19:32:15

As for creating a program, we need to perform some operations. To perform those operations we need some values. Here come the _variables_.

## What are variables?

_Variable_ is a thing programming language uses to store some piece of information under a specific name. Variable has a **name**, by which it may be accessed and which cannot be changed after its declaration, and a **value** - which can be changed. So basically, by creating a variable we retain some part of memory, saving some value in it, and accessing it through its name.

This was something general. Now, let’s dig into JavaScript variables. 

To create a variable we use the special keyword **var**. This indicates, that what comes after this expression will be the **name** of a newly created variable. So, as I mentioned, there we put the name of the variable. By doing so we already created a variable and gave a name to it. However, you can go ahead and assign a **value** to it at the time of creation by using the **=** operator, which is used to assign values to variables. The **=** operator assigns what comes after it, to what was before it. By using the keyword **var** you can create multiple variables (and also assign them!). See the code below for a better understanding.

```

var variableName; // you may stop here as your variable 
                 //has already been declared

var otherVariable = 10; //in this example we assigned 10 to 
                        //"otherVariable", which means that 
                        //otherVariable noe holds the value of 10


var number1 = 1, number2 = 2;

var name1, name2, name3;
```

## What to name my variable?

You do have some restrictions when it comes to naming variables. The name of the variable _cannot start with a number_, but it can include numbers. You _cannot use reserved words_, aka keywords, which are the ones we use to tell the language some specific things(like we used **var** to tell it to create a variable). Names cannot include spaces, or special characters other than **$** and **underscore( _ )** . Also, you cannot have multiple variables with the same name, unless you work in and out of **scopes**. (Will talk about these later)

Trust me you absolutely do not need some crazy and funny names for your variables. They must be meaningful. You will start off with small projects, and here, you will most probably be able to remember which variable was used for what reason. Nevertheless, in a bit bigger projects you will start forgetting what was “aaaCuteNameee” used for. So, make it a habit to choose variable names wisely. Take this as friendly advice from me :)


## Can the values that are given to a variable change?

Absolutely! When you assign a value to a variable, you simply tell the computer, that this name means its value. In our example, **otherVariable** is 10, so whenever I write the name otherVariable, I mean 10. Any value can be assigned to a variable, anytime, just the same way we did with **otherVariable**, which is, to write the name, then the operator **=**, then the value we wish to assign. 

## !Note.
 When you have already declared the variable (wrote var name; in your code) you no longer need to declare it before assigning the new value.

## Some notes on variable declaration in JavaScript

The keyword **var** is not the only keyword in JavaScript used for initializing variables. There is also **const** which creates a variable, you can assign a value to it, but you cannot change it afterward. 
As you will see in further exploration of the language, there are local and global variables. For now, just remember that there is also the keyword **let**, which also creates a variable, but in this case local only, while **var** may as well create a global variable.