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.
- Create an HTML file called JavaScriptString.html
- Type this inside the <body> tag.
<script language="JavaScript">
alert("Hello");
</script>
</body>
- Take out the " " around Hello. Open the file in both IE and Mozilla.
- Above the alert() line, type var Message = "Hello".
- Change alert ("Hello") to alert(Message);. Open again in your browsers.
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.
- var Message ="Hello"; is defining a variable. You are placing a value into the memory space you declared (set aside) earlier.
Related Articles
- Using a Text Editor and a Browser to Create a Web Page
- What is JavaScript?
- Using a Pre-defined Function in JavaScript
- Variables, Math, Concatenation and Overloaded Operators
- Using a Prompt Box and Outputting to the Screen
- Basics about Forms
- Adding JavaScript to a Form