What are the Three Variables in JavaScript?
There are three ways to declare a variable and give it a value in modern JavaScript:
- var
- let
- const
A variable is a container for a value, such as a string or a number we might use in a statement or as part of a sum.
var
Back when JavaScript was first created, this was the only way to declare variables.
When declaring variables in earlier versions of JavaScript, the var keyword was used instead of let:
var name = “Andrew”;
The current method of declaring variables, let, has a number of benefits over var.
For instance, var permits re-declaration of variables with the same name, whereas let generates an error.
It is recommended to use let instead of var, when declaring variables.
let
The let keyword was introduced in ES6 (2015). A block scoped variable in JavaScript is declared with the let keyword. Variables defined using the let keyword are block scoped in contrast to those declared using the var keyword, which is often used to declare a variable in JavaScript and is handled as a standard variable.
Variables are created using the let keyword like this. Creating a variable in JavaScript is called “declaring” a variable:
let name;
name is the name of the variable
After creating the variable, we can initialise it with a value:
name = 'Tom';
The equal sign (=
) is used to assign a value to our variable.
We can also assign our variable a value during creation, like this:
let name = 'Tom';
let age = 34;
Remember, that we need to enclose text values in quotes.
After initialising a variable, we can output its value, using a method called console.log:
let name = 'Tom';
console.log(name);
Variables can change their value during the program.
let age = 34;
age = 24;
console.log(age);
Updating the value of a variable can be done as many times as needed.
const
The const keyword was introduced in ES6 (2015). Constants are similar to variables and are declared using the const keyword:
const color = 'green';
console.log(color);
Constants must have a value when declared and they cannot change their value.
Constants are very useful. If you use const, it tells anyone looking at your code that this name will never be assigned to a different value.
Things to note about Constants:
- Variables defined with const cannot be Redeclared
- Variables defined with const cannot be Reassigned
- Variables defined with const have Block Scope
Conclusion
In conclusion, the three variables in JavaScript are var, let, and const.
Var is a globally scoped variable declaration that can be updated and re-declared within its scope.
Let and const are block-scoped declarations that allow for better control over variable assignment and mutation. Let can have its value reassigned but cannot be redeclared within its scope.
While const allows for neither reassignment nor redeclaration.
These three variable declarations provide JavaScript developers with a range of options to choose the most appropriate type of variable depending on their requirements, making code more readable and efficient while ensuring proper resource allocation. Selecting the right type of variable based on the specific use-case requires an understanding of each option’s features and behaviours to ensure high-quality results in programming projects.
Further reading
If you want to learn more about JavaScript variables, MDN Web Docs is a wonderful resource – Storing the information you need — Variables
If you liked this article, then please share. You can also find me on Twitter for more updates.