Latest posts

10/recent/ticker-posts

Variable declaration in Javascript | How to declare variables in Javascript | Types of variables in Javascript | How to create variables in javascript

 Variable Declaration In JavaScript

Every Programming language has variables to store the perticular values in memory space.
Like that, JavaScript also supports variable declaration.

What is an JavaScript Variable ?
A JavaScript variable is simply a name of storage location.

There are two types of variables are present in JavaScript,


1) Local Variables
2) Global Variables


JavaScript Local Variables : 

This types of variables are declared in functions to store the values,
This types of variables are only accessible within the function or block only.

Example :

 <script>
  function vari()
  {
    var a = 10; // Local Variable 
  }
 </script>
Here we are declared a variable a with value 10,
Varible a is declared in within a function called vari().
The variable a are only accessible for function vari( ). we cannot able to use this variable in another function or in any other block of code.

JavaScript Global Variables : 

This type of variables are accessible from any function. A variable declared outside the function or declared with window object is known as Global Variable.

Example :
 <script>
   var a = 200;
   var b = 300;
   function add()
   {
      var c = a + b;
      document.write("Addition of A + B is : "+c);
   }
 </script>
Here we are declared two variables a and b.
This varables are declared in a script and outside the function, so we can easily able to call them in any function or any block of code.
To use those variables we created a function called add( ).
In that, we are created a local variable c, and in variable c we are stored the addition of variable a and b.
After that we are, printed the output of variable c.




Rules to declare the Variables:

1) Name of the Variable should be start with any lette, we cannot able to declare a variable which has name started with a number.
 var abc = 10;  ✓
 var 123 = 10:  ✕
2) We cannot able to declare an variable like that,
 var abc = xyz; ✕

3) Variable name can contains only letters, digits, underscore, and doller sign.
 var abc = 10; ✓
 var a1 = 5; ✓
 var a2 = 10; ✓
 var x_y = 10.5; ✓
 var _dec = 50; ✓
 var $ab = True; ✓
4) Variable name are case sensitive.
 var a = 5; ✓
 var A = 10; ✓
 
5) 4 ways to declare a Variable,
    Using keyword var
 var a = 15; ✓

    Using keyword let
 let a; ✓

    Using keyword const
 const a = 10; ✓

    Without using any keyword
 a = 5; ✓

Program which shows ate use of Local and Global Variables in a single script.








Here we have created a script, in that we have declared 3 Global vriables called a, A, b.
After that we have created 2 functions, add( ), and sub( ).
First function add( ) will shows the addition of Global Variables a, A, b.
For that we are created a local variable c in function add( ), which stores the addition of variable a, A, b.

Second function sub( ) will shows the substraction of Gblobal variables a, A, b.
For that we are created a Local Varaible c in function sub( ), which stores the subtraction of variable a, A, b.

To call the function add( ) and sub( ) we are created a 2 Buttons Addition and Subtraction, by using onClick attribute of Button tag, we have called those 2 functions.



Hope it will help you to know that, how to create or declare the variables in JavaScript !

What we have learned in this article :

1) What is an Variable ?
2) Types of Variables in JavaScript
3) What is an Local Variable
4) What is an Global Variable
5) Rules to declare the Variables in JavaScript
6) Program that shows the use of Local and Global Variables.

Subscribe our site to learn HTML - JavaScript in easy and simle language !




Also Visit


Post a Comment

0 Comments