Variables In JavaScript

Introduction

 
In the previous chapter, we learned how to enable or disable HTML Anchor Links (HyperLink), using JavaScript and jQuery.
 
In this chapter, we will learn about 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
 
image1
 
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
 
image2
 
There are two types of Variables
  • Local Variable
  • Global Variable

JavaScript Local Variable

 
The local variable is a variable in which a 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
 
image3
 

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
 
image 4
 

Summary

 
In this chapter, we learned about variables in JavaScript, the scope of Variables, and how to declare a variable in JavaScript.
Author
Vijayakumar S
411 3.9k 1.9m
Next » Operators in JavaScript