Introduction
A JavaScript variable is a name for a storage location. In simple words, they are containers for storing data. In JavaScript, there are two different classes of variables: local variables and global variables. Additionally, you can declare multiple variables with the same var keyword. In a JavaScript program, a variable must first be declared before use. You can place the data you want into them for use like any number you might use for addition. Also, you reach out to the stored data by naming the container.
Variable means things that can vary. In JavaScript, a variable stores data and you can change it later. Continue reading to know more about javascript variables such as the scope of variables examples and uses.
What Are The Javascript Variable Scopes?
Scope means the availability of variables and functions in definite parts of the code. In JavaScript, a variable has two types of scope namely, Global Scope and Local Scope. Let's understand the types of scope in javascript in detail:-
Global Scope
A global variable is declared at the top of a program or outside of a function. In Javascript, you can use a variable without declaring or defining it. You can change the value of a global variable inside a function. If you are using a variable without declaring it, that variable will become a global variable automatically.
Example:
// program to print a text
let x = "Monday";
function day () {
console.log(x);
}
day(); // Monday
In the above example, variable x is declared at the top of a program. It is a global variable. It means you can use the variable x anywhere in the program.
Local Scope
A variable can have a local scope, but you can access it within a function only. Look into the below example for a clear understanding of the Local Scope:
// program for the local scope of a variable
let x = "Merry";
function greet() {
let y = "Christmas"
console.log(x + y);
}
greet();
console.log(a + b); // error
Run Code
Output
MerryChristmas
Uncaught ReferenceError: y is not defined
In the above program, variable x is a global variable and variable y is a local variable. You can access the variable inside the function greet only. Hence, when we try to access variable y outside of the function, an error occurs.
Our Learners Also Read: Top 10 Reasons to Learn JavaScript
When Should JavaScript Var Be Used?
- JavaScript variables must always be declared using var, let, or const.
- From 1995 to 2015, every JavaScript code contained the var keyword.
- 2015 saw the addition of the let and const keywords to JavaScript.
- You must use var if you are running in older browsers.
How To Declare A Variable?
You can declare a variable using 'var', 'let', or 'const' keywords
- Since JavaScript was developed, the 'var' keyword has been used to declare variables. When variables are declared using var, it is unclear and prone to error.
- The let keyword gets rid of var's ambiguity and error. The new and advised method of declaring variables in JavaScript is this one.
- A constant variable is one that, once given a value, cannot be modified; it is declared with the const keyword.
When declaring a JavaScript variable, there are several rules. These rules are also known as identifiers. They are:
- The name must begin with an alphabetical letter, from A to Z, a dollar sign ($), or an underscore (_).
- The first letter can be followed by any number from zero () 0 to nine (9), such as the value 1.
- JavaScript variables are case-sensitive. For instance, the variables b and B are distinct.
The names of each variable in JavaScript must be different. We refer to these special names as identifiers. Some of the unique properties of identifiers are:
- Short names (like x and y) or more descriptive names can serve as identifiers (date, age, etc).
- From numbers, letters, and underscores, to dollar signs all can be used in names.
- All names have to start with a letter.
- In addition, names can start with $ too.
- The case affects names only (p and P will be different variables).
- Names cannot contain reserved terms (like JavaScript keywords).
Naming Conventions For Variables In Javascript
- In JavaScript Variable names are case-sensitive. Therefore, the names Day, DAY, day, and dAy are all regarded as different variables.
- In the names of variables, you can use letters, numbers, or the $ and symbols.
- A variable name cannot begin with a number 0 through 9.
- In JavaScript, a reserved term cannot be a variable name. For example, var, function, or return.
Hoisting JavaScript Variable
Variable Hoisting, often known as var Hoisting, is JavaScript's most significant and distinctive feature. All variable definitions in JavaScript are examined before any other code is run. Thus, all of the variables are declared at the beginning of the code. The name of this feature in Javascript is Hoisting. Depending on where they are declared, the declarations are moved to the top of the global code or the top of a function. Internally, Javascript performs this automatic moving of declarations to the top of the function or global code. However, we don't notice the change.
Quick Facts About Javascript Variables
- The term let can be used to define variables. Global variables are those that are defined without the let or var keywords.
- Variables should be initialized before access. The value of an unassigned variable is 'undefined'.
- Because JavaScript is a loosely typed language, a variable can hold values of any type.
- As discussed above, the variable scope can be either local or global. Global variables can be accessed from anywhere. Local variables, on the other hand, can only be accessed within the same function in which they were declared. In a variable declaration in Javascript, several white spaces and even line breaks are permitted.
- A comma can be used to divide many variable declarations.
- Javascript allows us to keep any kind of value in a variable and alter it at any time.
Conclusion
We have seen the definitions, types and what are scopes of a variable in javascript. Thus we can conclude that in Javascript, values are kept in containe0frs called variables. The terms var and let can be used to declare variables. Variables may be changed or altered at any time and anywhere within their range. Any reserved terms in javascript cannot be used as variable names. There are only two types of variable scopes in JavaScript: global scope and local scope. In JavaScript, constants are values that remain the same throughout the code.
Learn Java to step up your Career!
To know more about Java, join the Full Stack Java Developer Course offered by E & ICT Academy IIT Guwahati. You will get a chance to learn with industry experts!