When writing code, it is possible to use the same expression at different points. You could easily copy and paste that expression to different points, and it would work. Alternatively, you could store that expression in a container with a name and call that container at any point you need that expression.
Variables are named containers that help you store expressions you will need at different points in your code. Anytime you call the name of the variable, the expression within it is fetched and sent back.
3 Quotes
“One special thing about variables is that they can contain just about anything — not just strings and numbers. Variables can also contain complex data and even entire functions to do amazing things. You'll learn more about this as you go along.” - MDN docs
“For instance, the variable message
can be imagined as a box labeled "message"
with the value "Hello!"
in it. We can put any value in the box. We can also change it as many times as we want. When the value is changed, the old data is removed from the variable.” - JS info
“JavaScript is a dynamically typed language so the type of variables is decided at runtime. Therefore there is no need to explicitly define the type of a variable.” - GeeksforGeeks
2 code samples
// sample 1
var message = 'Hello';
// sample 2
// define the variable and assign the value
let message = 'Hello!';
alert(message); // Hello!
1 exercise
x = 7
y = 2
z = x % y
console.log(z) // output?9