Articles → JavaScript → Variables In Javascript

Variables In Javascript

  1. What are variables? How to use it JavaScript application?
  2. Rules for creating variables.
  3. Global variables and local variables.

      <title>Variable example</title>
      <script language="JavaScript">	
         function ClickMe()
             var alert_text = "Hi, this is the variable tutorial";
         <input type="button" name="btnComments" value="Click Me" onclick="javascript:ClickMe();">

  1. Variable name should not be a keyword. In the above example if we rewrite the function like
  2. function ClickMe() {
        var new = "Hi, this is the variable tutorial";

  3. Variable name should always start with alphabet or underscore(_). Let us see what will happen if we change the variable name from alert_text to 00alert_text.
  4. function ClickMe() {
        var 00alert_text = "Hi, this is the variable tutorial";

  5. Variable names are case senstive so alert_text and Alert_text are 2 different variables.

Global And Local Variable

  1. A local variable is visible within the function where it is defined.
  2. A global variable is visible through out the page.

var global_variable = "This is the global variable";

function ClickMe() {
    var local_variable = "This is the local variable";

function ClickMeAgain() {

Posted By  -  Karan Gupta
Posted On  -  Wednesday, December 1, 2010


Your Email Id  
Query/FeedbackCharacters remaining 250