Variables In JavaScript

Introduction

 
In this article we will cover the basic concepts of JavaScript, the most important being variables and data types.
 

Variable

 
A Variable is an identifier or a name that is used to refer to a value. It is media to store the information to be referenced and manipulated in a computer program. Using the key of a variable in JavaScript “var”, the extension is important (.js).
 
Syntax
  1. var <variable-name>;      
  2. var <variable-name> = <value>;    
Note
 
In the example, the number 10 is assigned to “a”. Use only the special characters  (__) underscore and ($) symbol, don't use other special characters like @, *, %, #. JavaScript is case sensitive.
 
In this example the variable name ‘a’ is uppercase,
  1. var a=10;  
  2. document. Write(A);  //varible name is lowercase
Error - ‘a’ is not found
 
Example 1
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Java Script</title>  
  5. </head>  
  6. <body>  
  7.     <h4>Variables in JavaScript</h4>  
  8.     <script>  
  9.         var a = "30";  
  10.         document.write(a);  
  11.     </script>  
  12. </body>  
  13. </html>
Output
 
 
Example 2
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Java Script</title>  
  5. </head>  
  6. <body>  
  7.     <h4>Variables in JavaScript</h4>  
  8.     <script>  
  9.         var name = "vijay";  
  10.         document.write(name);  
  11.     </script>  
  12. </body>  
  13. </html> 
Output
 
 
There are two types of Variables
  • Local Variable
  • Global Variable

JavaScript Local Variable

 
The local variable is a variable in which variable can be declared within the function, and it can be used only inside that function. So, variables with the same name can be used in different functions.
 
Example
 
//local Variable
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Java Script</title>  
  5. </head>  
  6. <body>  
  7.     <h4>JavaScript</h4>  
  8.     <script>  // local variable
     
  9.         function article (){  
  10.         var name = "Variables in JavaScript";  //inside the function declare a variable
     
  11.         document.write(name);  
  12.     }  
  13.     article();  
  14. </script>  
  15. </body>  
  16. </html> 
Output
 
 

Global Variable

 
The global variable is a variable in which variables can be defined outside the function. So, variables with the same name cannot be used in different functions.
 
Example
 
//Global Variable
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Java Script</title>  
  5. </head>  
  6. <body>  
  7.     <h4>JavaScript</h4>  
  8.     <script>  //global variable
     
  9.         var articlename = "variables in JavaScript";  //outside the function declare a variable
     
  10.         function article ()   
  11.         {  
  12.         document.write(articlename);  
  13.         }  
  14.         article();  
  15. </script>  
  16. </body>  
  17. </html> 
Output
 
 

Summary

 
In this article, we looked at how to declare a variable in JavaScript. In my next article, we will learn what the data types in JavaScript are. I hope this article is very useful for you.
 
Thanks for reading.