In this brief tutorial, you will learn about what is JavaScript variables, types of JavaScript variables, variable naming rules and differences between local and global variables.
A JavaScript variable simply means a placeholder of values that are subject to change at runtime.
When we talk of variable, we are referring to a placeholder of values that could either be entered by a user or generated by the JavaScript engine based on the developer specifications.
Like every other programming or scripting languages, variable declaration or naming has some rules that must be strictly adhere to and it is called variable naming rules or convention.
You must have seen someone asking what are the rules for naming a variable in JavaScript or you probably asked this question why delving into programming.
These rules are:
- A variable name must not contain any of the special characters except underscore(_) and dollar sign ($)
- A variable name must not start with numeric value
- A variable must not be ambiguous.
- In JavaScript, a variable name is case sensitive. Var Ab is not same as Var ab.
- A variable name must not contain space e.t.c.
var a;
const b;
var a,b;
var sum;
Generally, there are two types of variables in JavaScript namely local variable and global variable.
What is local variable in JavaScript
A local variable as the name implies can only be access within the function it is declared. Local variables are available for use throughout the function it appears. In other words, it can't be called from another function.
function sum(){
var a=2;
var b=3;
var r= parseInt(a) + parseInt(b);
//result is 5
}
//In the function sum, we declared<b> a,b,r as local variables and as such, can be accessed <br>through out the function. That is why we have r = a + b.
function sub(){
r= parseInt(a) - parseInt(b);
//result is null
}
//The reason our result is null is because we tried accessing variable a,b and r from another function where it is not declared.
I believed you have gotten the idea from the above explanation.
Now, let's move over to global variables.
A global variable is a variable that can be accessed through out the JavaScript file. All functions that are contained in the JavaScript file can access the variable values anywhere.
For more clarification, consider a global variable as a big with many doors yet you have a spare key for all doors. Bro, you can open any of these doors whenever you like. That is the true nature of global variables. They let you access them without restrictions. Lol!
var a=2;
var b=3;
var r;
function sum(){
r = parseInt(a) + parseInt(b);
//result is 5
}
//In the function sum, we declared<b> a,b,r as global variables and as such, can be accessed by all functions. That is why we have r = a + b.
function sub(){
r= parseInt(a) - parseInt(b);
//result is -1
}
//The reason our result is not null this time is because we declared a,b and r as global variables.
I believed you have gotten the idea from the above explanation.
- Global variables are declared outdoide a function. While local variables are declared inside a fun function.
- Global variables can be accessed anywhere. While local variable can only be used within the function it is declared.