Variables in JavaScript

In this tutorial, we will check variables in JavaScript. If you learnt any programming language before then you would have an idea what these variables are. For those who never heard this word before variables are simply named of storage locations. Meanwhile working on any program we store some values in different memory locations and variables simply reference to these memory locations and allow us to fetch values from these memory locations.

There are some rules as other languages while writing the identifiers of these variables.

  • Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
  • After first letter we can use digits (0 to 9), for example value1.
  • JavaScript variables are case sensitive, for example name and Name are different variables.

Some valid variable names.

<script>
var x = 10;  

var min_age="18;  

var name="Owlbuddy";
</script>

Difference Between Local and Global Variables:

Local Variables: 

Variables which we declare inside a block or inside a function are known as a local variable and where these variable are declared only that function or block can use these variables.

<script>  
function show(){  
var name="owlbuddy";//local variable  
}  
</script>  

Global Variables:

Variables which we declare outside of the block or any function are known as global variables and these can use from anywhere in code

<script>  
var name="owlbuddy";//gloabal variable  
function show1(){  
document.writeln(name);  
}  
function show2(){  
document.writeln(name);  
}   
</script>  
Spread the love