What are JavaScript Variables?
Understanding JavaScript Variables
Variable are the JavaScript containers in which data in multiple form are stored, in another term they are just containers only. They are one of the fundamental core feature of JavaScript for their diverse use in the scripting respectively. They have some unique identifiers, types, rules, usage that we are going to study with this important topic one by one.
In the following example a, b and c are the variables with different values
var a = 2; var b = 3; var c = a + b;
Identifiers for Variables in JavaScript
All JavaScript variable have unique set of rules or identifiers which must be followed to use them for getting specific results.
- Var names can have letters(a-z), digits(0-9), underscores(_), and dollar signs($)
- First character must be defined with a character only.($ and _ are also allowed)
- They can hold all data types, perform operations, functions and more in multiple form and use them
- Like any other language there are few JavaScript reserved words which cannot be used as name for variables
- Javascript Variables are case sensitive i.e Small ‘a’ and Capital ‘A’ are two different variables completely.
How to Declare JavaScript Variables?
JavaScript Variables are declared within <Script> tag with Var word accompanied by their specific name. For e.g. we have named a Var with building but without any value thus technically undefined.
Var building;
So with value it has to be define i.e. in a specified way
building = "apartments"; or combine them together var building = "apartments";
JavaScript Variable Examples
Redeclare
Multiple variable
Undefined
Arithmetic
JavaScript Variable Types
Based on the accessibility and scope JavaScript has two types.
- Local Scope
- Global Scope
JavaScript Global Variable
These variables are declared outside the function and have accessible anywhere inside within the script.
JavaScript Local Variable
Variables that are defined inside a function and are not accessible outside it are termed as Local Variables in JavaScript. A local variable is then only local scope and hence limited accessibility.