Logo

JavaScript Variables

A variable is a “named storage” for data. We can use variables to store goodies, visitors, and other data.To create a variable in JavaScript, use the let keyword.

We can easily grasp the concept of a “variable” if we imagine it as a “box” for data, with a uniquely-named sticker on it.
For instance, the variable message can be imagined as a box labeled "message" with the value "Hello!" in it:

The statement below creates (in other words: declares) a variable with the name “message”:

Now, we can put some data into it by using the assignment operator =:

The string is now saved into the memory area associated with the variable. We can access it using the variable name:

We can also declare multiple variables in one line:

Variable naming

There are two limitations on variable names in JavaScript:
The name must contain only letters, digits, or the symbols $ and
The first character must not be a digit.

When the name contains multiple words, camelCase is commonly used. That is: words go one after another, each word except first starting with a capital letter: myVeryLongName. What’s interesting – the dollar sign '$' and the underscore '_' can also be used in names. They are regular symbols, just like letters, without any special meaning.