What are the variables in Javascript? How to use them in the program? – Learn Javascript Online!

In the previous post, I have used the variable but I didn’t explain properly. I told you that I will be explaining them soon in my next session. Now the time has arrived…

Let get together to inspect the Javascript variables. Is it really hard to understand? Let’s check it out…

The basic unit of a programming language is a variable, which is used to hold the values. Without it, you can’t hold values. And processing and displaying will obviously get out of the way. Because, if you are not able to store the value then how can you process it.

Variable is a memory allocation to hold a value. Are you thinking that how much it can store?

Okay, let me tell you that it depends on upon the datatypes. You can get the total storage and storage type idea from that. Later I will be explaining that too. Just hold your seat to get friendly with these concepts.

When we do anything in our life, some rules and regulations are there. In the same way, Javascript has some rules and regulations you have to follow. Are you excited to know them? Just go ahead you will meet them.

  1. Name should start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
  2. After the first letter, we can use digits (0 to 9), e.g. pooja93.
  3. JavaScript variables are case sensitive, so be careful about that. E.g. x and X are different variables.

Javascript has two types  of variable, which I have listed below:

  1. Local Variable
  2. Global variable

Now I am going to tell you that what are these two types of variables? And I will also show you an example of each. So let’s get started with it, without making the delay.

Local variable

Are you not getting the idea from the name? No? No problem if you are not getting the idea.

Let me tell you about it. A local variable is a variable which is created and called inside the same function or block. It’s not accessible for other functions. Let me show you one little example.

Best website to Learn Javascript with Rahul Rahi

Javascript Local Variable Execution Example

Code of the above output is here:

<!DOCTYPE html>
<html>
  <head>
     <title>Example of Javascript Local Variable</title>
     <script type="text/javascript">
          function mynameis() {
                   var name = "My name is Rahul Rahi.";
                   var next_line = "<br/>"
                   document.getElementById('print_name').innerHTML=name;
                   }

          function pick_local_var(){
                   var NAME = "My name is ***** ****";
                   document.write(NAME);
                   document.write("I can't be accessed", name);
                   }
     </script>
  </head>
     <body>
          <label id="print_name"></label><br/>
            <button onclick="mynameis()"> Click me to know my name!</button><br/>
            <button onclick="pick_local_var()"> Click me to access local variable</button>
     </body>
</html>

In the above program, I have shown you that how to use local variable and what is that. In the above program, I have created two functions in which I have used a variable locally in a function and also accessed the local variable in the other function too, which is not printing the value. That’s what local variable exactly mean.

Now it times for global variable.

Javascript Global variable

Javascript global variable means the declaration outside of the function. But you can also access the local variable as a global variable. Are you asking me how? Just go ahead  and check it out.

Javascript Local Variable Execution Example

Javascript Local Variable Execution Example


<!DOCTYPE html>
<html>
  <head>
  <title>Example of Javascript Local Variable</title>
  <script type="text/javascript">
       var name = "My name is Rahul Rahi."; // global variable
       var NAME = "My name is ***** ****"; //global variable
          function mynameis() {
                 document.getElementById('print_name').innerHTML=name;
                 }

          function pick_local_var(){
                 document.write(NAME); // global variable
                 document.write('<br/>');
                 document.write("Now I can be accessed", name); // global variable now
                 document.write('<br/>');
                 }
  </script>
  </head>
        <body>
        <label id="print_name"></label><br/>
          <button onclick="mynameis()"> Click me to know my name!</button><br/>
          <button onclick="pick_local_var()"> Click me to access local variable</button>
        </body>
</html>

Now we have one important question that how to access local variable as a global variable? Is it really possible?

Yes, dear, it is possible with window. Have a look on the following example.

<html>
     <body>
        <script type="text/javascript">
            function m(){ 
                         window.value=100;
                      } 
            function n(){ 
                         alert(window.value);
                    }
             n();
        </script>
     </body>
</html>

Leave a Reply

CONTACT ME