Variables in JavaScript
When you write a program, you often need to remember and use different pieces of information to get things done. You can think of variables as containers that hold these pieces of information temporarily.
Let's say you want to calculate the area of a rectangle. In math, you multiply the length by the width to find the area.
area = length x width
Although you might be able to calculate the area in your head, when writing a program, you need to be very specific. You have to break down the process into four steps:
- Remember the value of the width.
- Remember the value of the height.
- Multiply the width by the height.
- Give back the result.
To remember these values, you use variables. Variables are like short-term memory for your program. Once the program finishes, it forgets the information stored in variables.
The term "variable" comes from the fact that the data stored in them can change each time the program runs.
Regardless of the specific values you give to width and height, the program will always be able to calculate the area by multiplying them.
Declaring a variable
Before you can use a variable, you have to let the program know that you want to use it. This is called declaring a variable, and it involves creating the variable and giving it a name.
In programming, when you declare a variable, you are essentially introducing it to the program and assigning it a name.
If the variable name consists of more than one word, it is commonly written in camelCase. This means the first word is written in all lowercase, and any subsequent words start with a capital letter.
Declaring a Variable
Assigning a value
After creating a variable, you can choose what information you want to store in it. In programming, this process is called assigning a value to the variable.
Assigning a Variable
Updating a variable
After assigning a value to a variable, you can also change that value later in the program if needed.
To update a variable, you simply use the variable name, an equals sign, and the new value you want to assign to the variable.
For example, let's say the initial value of a variable called quantity
is 5. Then, somewhere in the code, something happens that allows us to handle a larger quantity, so we update the value of quantity
to 10.
Here's how it looks in code:
let quantity = 5; /* Some other processing might happen here, and as a result, the program might need to change these values. */ quantity = 10;
Rules for naming a variable
- The name of a variable should start with a letter, dollar sign ($), or underscore (_). It should not begin with a number.
- The name of a variable can include letters, numbers, dollar sign ($), or underscore (_). Avoid using a dash (-) or a period (.) in the variable name.
- You cannot use special words or reserved words as variable names. These special words have specific meanings in the programming language. For example, "let" is a reserved word used to declare a variable.
- Variable names are case sensitive, meaning that
score
andScore
would be considered different variables. However, it's not a good practice to create two variables with the same name but different letter cases. - Choose a variable name that describes the type of information it stores. For instance, you can use
firstName
to store a person's first name,lastName
for their last name, andage
for their age. - If your variable name consists of more than one word, use a capital letter at the beginning of each word after the first word. For example, instead of using
firstname
, it is recommended to usefirstName
(this is known as camel case).
Reinforce what you’re learning
Once you've read the article, you understand how things work, but that's not the same as being able to use it by yourself.
My aim isn't just for you to comprehend what's taught in the article, but for you to feel confident using it on your own once you've grasped it.
To help you do that, I've put together exercises. These will assist you in practicing what you've learned and identifying any areas where you might have some gaps in your understanding.
Solve These Exercises
Get my free, weekly JavaScript tutorials
Want to improve your JavaScript fluency?
Every week, I send a new full-length JavaScript article to thousands of developers. Learn about asynchronous programming, closures, and best practices — as well as general tips for software engineers.
Join today, and level up your JavaScript every Sunday!
Thank you, Taha, for your amazing newsletter. I’m really benefiting from the valuable insights and tips you share.