Variable in JavaScript
Variable are basically used to hold the value on specific storage location.
In JavaScript, There are two types of Variable :
- Local Variable
- 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 :
- var a= 11;
- var _name="rana";
Invalid Variable Declaration :
- var 1223=90;
- var *xx=320;
Example of JavaScript Variable :
- <script>
- var a = 20;
- var b = 40;
- var c=a+b;
- document.write(c);
- </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.
- <script>
- function variable(){
- var a=10;//local variable
- }
- </script>
OR
- <script>
- If(12<14){
- var z=20;//JavaScript local variable
- }
- </script>
Watch video based on Variable in Javascript
No comments:
Post a Comment
Share your views after read this post