Navbar

Friday 21 June 2019

Variable in JavaScript

Variable in JavaScript
                                                                                                                    Back to JavaScript Home

Variable are basically used to hold the value on specific storage location.

In JavaScript, There are two types of Variable : 
  1. Local Variable
  2. Global Variable
There are some rule that must be follow when you create any variable.
  • Variable name must start with a letter (a to z or A to Z), underscore(_) or ($) dollar sign.
  • After first letter specified in variable name you can use digit (0-9). Ex : data1
  • JS variable are case sensitive means a and A, both are different.
Valid Variable Declaration : 
   

  1. var a11;  
  2. var _name="rana";  

Invalid Variable Declaration : 

  1. var  1223=90;  
  2. var *xx=320;  

Example of JavaScript Variable : 

  1. <script>  
  2. var a = 20;  
  3. var b = 40;  
  4. var c=a+b;  
  5. document.write(c);  
  6. </script> 

Output : 60


Local Variable
If you declared any variable inside function or block then it is local variable because that variable can be used inside block or function only.It can not be used outside of the function or block.
  1. <script>  
  2. function variable(){  
  3.       var a=10;//local variable  
  4. }  
  5. </script>  

OR
  1. <script>  
  2.   If(12<14){  
  3.      var z=20;//JavaScript local variable  
  4.    }  
  5. </script> 


Watch video based on Variable in Javascript


No comments:

Post a Comment

Share your views after read this post