A variable is a “named storage” for data. We can use variables to store goodies, visitors, and other data.
(Eg: let message (The statement above declares a variable name.)
message = ‘Hello’; // store the string
The string is now saved into the memory area associated with the variable
let message = ‘Hello!’; // define the variable and assign the value
alert(message); // Hello!)Put in a pen
To be concise, variable declaration and assignment can be combined in a single line
Multiple variables can also be declared in one line
a=1, b=2, name=’John’;
For the sake of readability of code variable declaration should be on multiple lines.
let user = ‘John’;
let age = 25;
let message = ‘Hello’;
let user = ‘John’,
age = 25,
message = ‘Hello’;
You can use either method to name multiple variables.
- The name must contain only letters, digits, or the symbols $ and _.
- The first character must not be a digit.
Examples of valid names:
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.
– the dollar sign ‘
To declare a constant (unchanging) variable, use const instead of let:
const myBirthday = ‘18.04.1982’;
Variables declared using const are called “constants”. They cannot be reassigned. An attempt to do so would cause an error:
const myBirthday = ‘18.04.1982’;
myBirthday = ‘01.01.2001’; // error, can’t reassign the constant!
When a programmer is sure that a variable will never change, they can declare it with const to guarantee and clearly communicate that fact to everyone.
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known prior to execution.
Such constants are named using capital letters and underscores.
For instance, let’s make constants for colors in so-called “web” (hexadecimal) format:
const COLOR_RED = “#F00”;
const COLOR_GREEN = “#0F0”;
const COLOR_BLUE = “#00F”;
const COLOR_ORANGE = “#FF7F00”;
// …when we need to pick a color
let color = COLOR_ORANGE;
alert(color); // #FF7F00
COLOR_ORANGE is much easier to remember than “#FF7F00”.
It is much easier to mistype “#FF7F00” than COLOR_ORANGE.
When reading the code, COLOR_ORANGE is much more meaningful than #FF7F00.
capital-named constants are only used as aliases for “hard-coded” values.
eg: const COLOR_RED = “#F00”;
A variable name should have a clean, obvious meaning, describing the data that it stores.
In a real project, most of the time is spent modifying and extending an existing code base rather than writing something completely separate from scratch. When we return to some code after doing something else for a while, it’s much easier to find information that is well-labeled. Or, in other words, when the variables have good names.
Some good-to-follow rules are:
- Use human-readable names like userName or shoppingCart.
- Stay away from abbreviations or short names like a, b, c, unless you really know what you’re doing.
- Make names maximally descriptive and concise.
Examples of bad names are data and value.
Such names say nothing. It’s only okay to use them if the context of the code makes it exceptionally obvious which data or value the variable is referencing.
- Agree on terms within your team and in your own mind. If a site visitor is called a “user” then we should name related variables currentUser or newUser instead of currentVisitor or presentVisitor
Reuse or create?
And the last note. There are some lazy programmers who, instead of declaring new variables, tend to reuse existing ones.
Such programmers save a little bit on variable declaration but lose ten times more on debugging.
- We can declare variables to store data by using the
let– is a modern variable declaration.
var– is an old-school variable declaration. Normally we don’t use it at all, but we’ll cover subtle differences from
letin the chapter The old “var”, just in case you need them.
const– is like
let, but the value of the variable can’t be changed.
- Variables should be named in a way that allows us to easily understand what’s inside them