Articles → JavaScript → Variables In Javascript

Variables In Javascript






What Is A Variable?





How To Declare A Variable?




var author_name;



Example


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

Try It




Rules Of Creating A Variable


  1. Variable name should not be a keyword
  2. Variable names should always start with an alphabet or underscore (_)
  3. Variable names are case-sensitive

Global And Local Variable






<html>
<head>
    <title>Variable example</title>
    <script language="JavaScript">
        var global_variable = "This is the global variable";

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

        function ClickMeAgain() {
            alert(global_variable);
        }
    </script>
</head>
<body>
    <form>
        <input type="button" name="btnComments" value="Click Me" onclick="javascript:ClickMe();">
    </form>
</body>
</html>

Try It




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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250