The Computer Gal Logo - Laptop with coffee and plants
Learning about Pre-written JavaScript Functions with an Alert Box
If you would like to receive weekly emails with articles like this one, email Nora

JavaScript is commonly used on websites. If you have ever wondered what JavaScript looks like, here is a very simple example to try, with an explanation of the code.

For some strange reason, most programming classes and books have you write your first program to put "Hello World" on the screen. We can have HTML put words on the screen just fine; so our first JavaScript won't do that. Instead, it will say, "Welcome to Java Class" or whatever you would like it to say in a little alert box.

Create an HTML file with the following code. (If you have never created a web page, there is a link at the bottom of this page to help you.)

<html>
<head>
  <title>My First JavaScript</title>
</head>

<body>
  <script language="JavaScript">
    alert("Put your message here");
   </script>
</body>
</html>

Save your file in your folder in a subfolder called Programming. Then open the file from a browser. If you typed it correctly, you should have an alert box. You have just written your first JavaScript!

Basic Programming Skills in this Bit of Code

  1.  Your JavaScript is mixed right in with the HTML.
  2.  The first thing you do is tell the browser to get ahold of the JavaScript parser.
    <script language="JavaScript">
  3.  Notice that JavaScript begins with <> tags, just like HTML tags. That is because the browser reads HTML and needs to understand that what is coming is JavaScript. </script> tells the browser that the rest of the code isn't JavaScript. It's just like closing a regular HTML tag.
  4.  Most programming and scripting languages have large collections of pre-written programming that you can use in your own programming. These bits of code are called functions or methods, depending on the language.

    Here we are using the function alert(). Functions are marked with () in most languages. The parts inside () are called parameters or arguments. Each parameter or argument is a piece of information the function needs to work. In this case, JavaScript needs to know what you want the alert box to say.
  5.  Notice the " " around the message. " " tells most programming languages that the characters inside is what is called a String. Computers store different types of information differently. For example, numbers are usually stored differently than letters. The " " tell JavaScript to hold that information as a String. A String is one data type.
  6.  There is a ; at the end of the line that creates the alert box. A semicolon marks the end of one thing the computer has to do in many programming and scripting languages. The part before the ; is called a statement. Most programming languages can't work if you forget the ;. However, JavaScript is more forgiving. You can just type the next statement on the next line, and JavaScript will understand that it's another command.
  7. Try a change in your code. Copy your alert statement right beside the first one. Take out the ; between them and change the message in the second alert(). Preview your file and see what happens. Then add the ; back in and preview your file. Third move the second alert to the next line and see what happens.

Related Articles

  1. Using a Text Editor and a Browser to Create a Web Page
  2. What is JavaScript?
  3. Using a Pre-defined Function in JavaScript
  4. Variables, Math, Concatenation and Overloaded Operators
  5. Using a Prompt Box and Outputting to the Screen
  6. Basics about Forms
  7. Adding JavaScript to a Form
  8. JavaScript Rollover Images in Dreamweaver 8
Nora McDougall | Missoula, Montana 59801 | 406.253.4045 | Contact Nora
© 2010, Nora McDougall-Collins