Variables in JavaScript are commonly declared by using a var
keyword, followed by a variable name, assignment operator (equals sign =
), and a corresponding value.
JAVASCRIPT
1var number = 21;
2console.log(number); // prints 21
This will display the value 21
, and this is verified by printing the variable value on the console.
In ES6, the variable declaration keywords let
and const
were introduced. We covered these keywords briefly in the last lesson. var
has some weaknesses in terms of scope, and let
and const
are now preferred. For a deep dive of the difference between the three, check out this article.
JAVASCRIPT
1let number = 21;
2console.log(number); // prints 21
Let's test your variable knowledge with some short questions!