DEV Community 👩‍💻👨‍💻


Posted on

What are variables?

It took me a while to understand what variables mean in JavaScript. An analogy that resonated with me was to think of a variable as a storage box. The name assigned to a variable would be the sticky-label on the box; and the value of the variable would be what's actually inside the box. So:

Variable = storage box
Name of the variable: sticky-label on the box
Value = the contents of the box

Once that was clear, I could begin thinking of it in 'technical' terms and understood that variables store values.

Declaring Variables

Variables are declared by using the statements let and const. You might have heard of var being used but it's not so common anymore. In the simplest (maybe a bit too simple, ha) terms possible:

let means that the value of the variable can vary, whereas const means that the value of the variable is fixed and can't be changed.

Why are variables important?

I'm only a couple of weeks into learning JS, but that's long enough to realise that we can't do much in JS without variables! It's just an integral part of this programming language. Also, variables prevent us from writing repetitive code. In the English language we use pronouns (he, she, they, etc...) to avoid repeating ourselves. In the same way, we use variables to avoid repetition in our code.

There are other reasons for why and most importantly how variables are used but that is beyond the scope 😏 of this brief post.

Top comments (0)

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!