Investors Hangout Stock Message Boards Logo
  • Home
  • Mailbox
  • Boards
  • Favorites
  • Whats Hot!
  • Login - Join Now!
All About Computers SEO
Posted On: 10/06/2012 4:52:20 PM
Post# of 261
Avatar
Posted By: nerd

Creating Variables and Using the JavaScript Alert() Function


Let's create a simple HTML document that we can use to test our variables:


Learn How to Code Part I: Variables and Basic Data Types
(Right click this link and choose to save the document to download a copy of this skeleton HTML file.)


You're going to want to have a better-defined HTML document when you're actually writing code, but for our purposes this will work just fine. Save the code above as a file called myscript.html (or anything you want that ends in .html and doesn't contain spaces or special characters) and open it up in your web browser. You'll see absolutely nothing other than "My Script" in the title bar. We still have more work to do. First, let's declare a variable inside of the script tag:


myVariable = 5;

Here we've just declared a number. Let's look at other variable types we can declare


myNumber = 5;
myString = "Hello world!";
myBoolean = true;

That gives us a number, a string, and a boolean. Now let's take that myString variable and actually do something with it:


myNumber = 5;
myString = "Hello world!";
myBoolean = true;
alert(myString);

You'll notice I've added the line alert(myString); . This calls a built-in JavaScript function (we'll learn more about these later) called alert() which creates a pop-up dialogue box for users to interact with.



What is "Hello world!" all about?


Writing a simple program that says "Hello world!" is generally the first thing every programmer does when they're learning how to code. It's not necessary, but it's sort of a tradition and an initiation into the club.



You never really want to use these in practice because—as any internet user likely knows—alert boxes are very annoying, but they make for a good way to test your code, to make sure it works, while you're writing. The parenthesis following alert allow you to provide alert with data it might need. Not all functions will require that you give it information, but alert needs to know what to alert the user. In this case, we gave it myString, so the user will receive a popup notification that says "Hello world!" Try this with your other variables to get popups with a number and a boolean value.


Why give alert() a variable and and not just give it the contents of the variable? Well, if you said alert("Hello world!") you'd get the same result in this example, but variables are called variables because they vary . The idea is that the contents, or values, of these variables will change as users interact with the programs you write.


(0)
(0)









  • New Post - Investors HangoutNew Post

  • Public Reply - Investors HangoutPublic Reply

  • Private Reply - Investors HangoutPrivate Reply

  • Board - Investors HangoutBoard

  • More - Investors HangoutMore

  • Keep Post - Investors HangoutKeep Post
  • Report Post - Investors HangoutReport Post
  • Home - Investors HangoutHome
  • Mailbox - Investors HangoutMailbox
  • Boards - Investors HangoutBoards
  • Favorites - Investors HangoutFavorites
  • Whats Hot! - Investors HangoutWhats Hot!
  • Settings - Investors HangoutSettings
  • Login - Investors HangoutLogin
  • Live Site - Investors HangoutLive Site