Articles → JavaScript → Variables In Javascript
Variables In Javascript
What Is A Variable?
How To Declare A Variable?
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
- Variable name should not be a keyword
- Variable names should always start with an alphabet or underscore (_)
- Variable names are case-sensitive
Global And Local Variable
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);
}