Extra Help on your first JavaScript program
This is similar to creating a web page. Indeed, all JavaScript programs sit inside web pages. There are several editing programs that let you create web pages simply, such as Front Page Extra and Dreamweaver, but here I am going to show you the simplest way.
To run the program, simply double click on the icon. This activates your web browser and loads the web page into it. The program should run and you should see the results.
Suppose you write the following program and store it (with no blank lines before or after it) in a file: <script language="JavaScript"> document.write("This line has an error in it.); </script> Did you spot the error in that program? I forgot to put in the quotation marks after the message in the document.write(). If you try to run the program, you should get a message like the following: ![]() Notice that the browser has reported that the quotation marks were missing ("Unterminated string constant") and that it occurred 48 characters along in line 2. If you have a text editor which tells you which line the cursor is on then finding lines is easy. Notepad doesn't do this, so finding an error on line 2119 is tricky! (One simply has to count the lines!) Be careful if you have Word Wrap turned on - what looks like several lines may only be one, so you may lose count. You might like to turn Word Wrap off before starting to count the lines. The error message only appears if your browser has been instructed to display error messages. If it has been instructed to ignore them, then no error message appears, and all that happens is that the program doesn't show the message - which is very confusing. Here is the way to instruct Internet Explorer to display error messages. (I don't know the equivalent way of doing it in Netscape Navigator):
![]()
This often confuses people. The plus symbol is used normally for mathematical addition (in the sense that 2 + 3 = 5), but also for joining two strings together. If either of the two things surrounding the + sign is a string (variable or constant), then the + sign simply produces a longer string by glueing the two parts together. If both the two things are numbers (variables or constants), then the + signs add them together to produce a number. Perhaps a few examples will make this clear: x = "XXX" + "123"; In this case, "XXX" and "123" are both strings. I know that "123" looks like a number, but it is within quotation marks, so it is really a string. In this case, + glues the two strings together and the variable x is set to "XXX123". document.write(40.5 + "123"); Here we have another use of +, this time in an document.write() statement. One of the things being "added" is a number (the 40.5) and the other is a string (again, the famous "123"). Because they are not both numbers, + cannot add them in the same way as two numbers, so it glues them together and the message produced on the screen is 40.5123
document.write(40.5 + 123 + 5); Here we have two uses of +. They are surrounded entirely by numbers (there are no quotation marks around 123 to turn it into a string), so it performs normal mathematical addition, and displays the answer 168.5. So far we have used just constants, but the same thing applies to variables as well: var x,y; x = 20; y = 100; document.write(x + y); x = "Hello"; document.write(x + y); There are two document.write(x + y) statements, but they don't have the same result. In the first case, the instruction is equivalent to document.write(20 + 100) which produces the message 120
Then the variable x is changed into a string (which is perfectly legal in this language!) Now the instruction document.write(x + y) is working with one string and one number, and produces the message Hello100
|