Variables in JavaScript

Variables are identifiers for the value which means there will one unique variable of that name. JS variables are used as containers to store the value.

You can name these variables and store values in them, to refer to the value use variable name. Before using a variable, you must declare it.

Example Of Declaration of Variables

<!DOCTYPE html>
<html>
<head></head>
<body>
   <h3>Declaration of Variables in JavaScript</h3>
   <script>
      var a;      //declaration of variable
      var b;
      var c, d;   //declaration of two variables on same line
   </script>
</body>
</html>

You can also declare multiple variables on a single line by putting commas in between. When you store a value inside a variable it’s called a variable initialization.

You can do store value inside a variable at the time of declaration or at any other point you want to declare it. As variables are dynamic you can change the variable value at any point in the program.

Example Of Initialization of Variables

<!DOCTYPE html>
<html>
<head></head>
<body>
   <h3>Initialization of Variables in JavaScript</h3>
   <script>
   var a; 
   var b = 78; //variable initialization at the time of declaration
   a = 89;    //variable declaration
   </script>
</body>
</html>

You only need to use the ‘var’ keyword at the time of declaration no need to use it after the initialization of a variable. But if you declare a variable it will not lose its value.

JS is an untyped language, which means that you don’t have to assign a data type to every variable. If ‘a’ is being used to store an integer value that the same variable can be used to use string value.

Variable Scope

The scope of a variable is a region of the program in which the variable is used. Like if you are declaring the variable outside the function it becomes globally available and any other function can use it, but if you declare it inside the function, I become locally available which means it is only available to that function. We will see more explanation in the following,

JS has two types of scope

  • Global variable: A global variable can be defined anywhere in the program.
  • Local Variable: A local variable is limited to a function.

It will be only visible within the function it was declared. function parameters are local to that function.

Example Of Global And Local Variable

<!DOCTYPE html>
<html>
<head></head>
<body>
   <h3>Global and Local variable in JavaScript</h3>
   <p id="local"></p>
   <script>
      var a = 50.23;  //global variable
      function printvar() {
         var b = "local variable";   //local variable
         document.write(b);
      }
      document.getElementById("local").innerHTML = printvar();</script>
</body>

</html>

Output:

Global and Local variable in JavaScript
undefined
local variable

Rules to write the variable name

  • Names can be made up of letters, digits, underscores, and dollar signs.
  • The name should begin with a letter
  • Names can also begin with a dollar sign ($) or an underscore (_).
  • JS is case sensitive so a and A are different.
  • Reserved words cannot be used as names.

List of reserved words

Variables in JavaScript
Do you know?
Variables in JavaScript This article is contribute by Rushikesh Tayade If you want share your knowledge on Study4Geeks then go to login section.