Variable means anything that can vary. In JavaScript, a variable stores the data value that can be changed later on.
Use the reserved keyword var
to declare a variable in JavaScript.
var ; var = ;
A variable must have a unique name. The following declares a variable.
var msg; // declaring a variable without assigning a value
Above, the var msg;
is a variable declaration. It does not have any value yet. The default value of variables that do not have any value is
undefined.
You can assign a value to a variable using the =
operator when you declare it or after the declaration and before accessing it.
var msg;
msg = "Hello JavaScript!"; // assigned a string value
alert[msg]; // access a variable
//the following declares and assign a numeric value
var num = 100;
var hundred = num; // assigned a variable to varible
In the above example, the msg
variable is declared first and then assigned a string value in the next statement. The
num
variable is declared and initialized with a numeric value in the same statement. Finally, the hundred
variable is declared and initialized with another variable's value.
Multiple Variables Declaration
Multiple variables can also be declared in a single line separated by a comma.
var one = 1, two = 'two', three;
White Spaces and Line Breaks in Variable Declaration
JavaScript allows multiple white spaces and line breaks when you declare a variable with var keyword.
Please note that the semicolon ; at the end is optional.
Loosely-typed Variables
C# or Java has strongly typed variables. It means a variable must be declared with the data type that specifies the type of data a variable will store.
JavaScript is a loosely typed language. It means it does not require a data type to be declared. You can assign any literal values to a variable, e.g., string, integer, float, boolean, etc.
var myvariable = 1; // numeric value
myvariable = 'one'; // string value
myvariable = 1.1; // decimal value
myvariable = true; // Boolean value
myvariable = null; // null value
Variable Scope
In JavaScript, a variable can be declared either in the global scope or the local scope.
Global Variables
Variables declared out of any function are called global variables. They can be accessed anywhere in the JavaScript code, even inside any function.
Local Variables
Variables declared inside the function are called local variables to that function. They can only be accessed in the function where they are declared but not outside.
The following example includes global and local variables.
var greet = "Hello " // global variable
function myfunction[]{
var msg = "JavaScript!";
alert[greet + msg]; //can access global and local variable
}
myfunction[];
alert[greet];//can access global variable
alert[msg]; //error: can't access local variable
Learn global and local scope in JavaScript for more information.
Declare Variables without the var Keyword
Variables can be declared and initialize without the var
keyword. However, a value must be assigned to a
variable declared without the var
keyword.
The variables declared without the var
keyword becomes global variables, irrespective of where they are declared. Visit Variable Scope in JavaScript to learn about it.
It is Not Recommended to declare a variable without var
keyword because it can accidentally overwrite an existing global variable.
function myfunction[]{
msg = "Hello JavaScript!";
}
myfunction[];
alert[msg]; // msg becomes global variable so can be accessed here
Variable Names in JavaScript
- Variable names are case-sensitive in JavaScript. So, the variable names
msg
,MSG
,Msg
,mSg
are considered separate variables. - Variable names can contain letters, digits, or the symbols $ and _.
- A variable name cannot start with a digit 0-9.
- A variable name cannot be a reserved keyword in JavaScript, e.g. var, function, return cannot be variable names.
- Variable stores data value that can be changed later on.
- Variables can be defined using var keyword. Variables defined without var keyword become global variables.
- Variables must be initialized before accessing it.
- JavaScript allows multiple white spaces and line breaks in a variable declaration.
- Multiple variables can be defined in a single line separated by a comma.
- JavaScript is a loosely-typed language, so a variable can store any type value.
- Variable names are case-sensitive.
- Variable names can contain letters, digits, or the symbols $ and _. It cannot start with a digit 0 - 9.
- Variables can have local or global scope. Local variables cannot be accessed out of the function where they are declared, whereas the global variables can be accessed from anywhere.
Want to check how much you know JavaScript?