If you would like to receive weekly emails with articles like this one, email Nora
This is a simple JavaScript exercise that will show you the difference between working with a "hardcoded value" and using a variable.
Open your previous file and Save As AlertWithVariables.html
The first script:
<body>
<script language="JavaScript">
alert("Hello");
</script>
</body>
</html>
Take out the " " around Hello. Test the script in both IE and Mozilla.
Above the alert() line, type var Message = "Hello". Does it work now?
NOTES:
A word or a group of words with " " is called a String.
alert() is a function. It requires a string as a parameter or argument.
When you removed the " " around Hello, you changed the argument from a string to a variable, but there was no variable defined. So, JavaScript couldn't do what you expected.
var word; is called declaring a variable. You are telling the computer to reserve some memory space that you will fill.
="Hello"; is defining a variable. You are placing a value in the memory space.