JavaScript > Introduction

Variable in JavaScript

How to declare a variable in JavaScipt?


Variables are case sensitive and must start with an alphabhet letter or a underscore (_).

<script>
        var firstName;
        var lastName = "Funda";
        myName = "Sheo";

function MyFunction(){
var a = 20;
b = 30;
} </script>

In the above code snippet

  • The 1st line declares the firstName variable.
  • The 2nd line declares the last name variable and assign value.
  • 3rd line assigns a value to undeclared variable that automatically creates a valid variable and the scope of this variable would be thorugh out the page.
In function MyFunction
  1. a variable scope is within MyFunction only and we will not be able to access its value outside this function
  2. b variable scope will be complete page, whose value can be accessed anywhere in the page, even within other function
 Views: 6037 | Post Order: 3



Write for us






Hosting Recommendations