What are the variables in JavaScript?
Introduction
Each programming language has a fundamental need to store data in memory for doing some computations on that data. Now to store and access this data, the memory location needs a name, and this name is called “Variable.”
In other words, Variable means anything that can change/ vary. JavaScript includes variables that hold the data value and it can be changed anytime.
Definition
Variables are just the name of the storage location. Moreover, we can also call them as containers that hold some data during the program execution.
According to Wikipedia, Variables in standard JavaScript have no type attached, and any value can be stored in any variable. Starting with ES6, the version of the language finalized in 2015, variables can be declared with let (for a block-level variable), var (for a function level variable) or const (for an immutable one). However, while the object assigned to a const cannot be changed, its properties can.
In other words, Variable means anything that can change/ vary. JavaScript includes variables that hold the data value and it can be changed anytime. It’s also beautifully explained here.
Subsequently, let’s understand the concept in more detail with the help of the following examples:
In the above example, “digit” is a variable which points to a memory location which holds/stores the numeric value “12”. Similarly, “lang” is a variable which points to the memory location which stores the string value “javascript.” So we can say that a variable is just a name to the memory location, where the value will be stored.
In addition to the above, JavaScript uses reserved keyword var to declare a variable. A variable must have a unique name. You can assign a value to a variable using equal to (=) operator when you declare it or before using it.
SYNTAX
Let’s look at its syntax below:
Syntax:
var <variable-name>; var <variable-name> = <value>;
Example: Variable Declaration & Initialization
var one = 1; // variable stores numeric value var two = 'two'; // variable stores string value var three; // declared a variable without assigning a value
In the above example, we have declared three variables using var keyword: one, two, and three. We have assigned values to variables one and two at the same time when we declared it, whereas variable three is declared but does not hold any value yet. Therefore, it's value will be 'undefined'.
Declare Variables in a Single Line
Multiple variables can also be declared in a single line separated by a comma.
Example: Multiple Variables in a Single Line
var one = 1, two = 'two', three;
Declare a Variable without var Keyword
JavaScript allows variable declaration without var keyword. You must assign a value when you declare a variable without var keyword.
For example:
Variable without var Keyword
one = 1; two = 'two';
Point to Remember:
It is Not Recommended to declare a variable without var keyword. It can accidentally overwrite an existing global variable.
The scope of the variables declared without a var keyword becomes global irrespective of where it is declared. Global variables can be accessed from anywhere on the web page.
Visit the Scope of Variables for more information.
Key Takeaways:
1. Variable stores a single data value that can be changed later.
2. Variables can be defined using the var keyword. Variables defined without var keyword become global variables.
3. Variables must be initialized before using them.
4. Multiple variables can be defined in a single line. e.g. var one = 1, two = 2, three = "three";
5. Variables in JavaScript are loosely-typed variables. It can store the value of any data type through out its lifetime.










