JavaScript Basics Part 2 Seattle WA

In the last article, we wrote a simple script that involved variables, alerts, prompts, string concatenation and some basic arithmatic operators. In this week's installment, we learn all about the 'if' and 'else' statements.

Local Companies

Cypress Consulting
(206) 281-8240
100 S King St
Seattle, WA
Campbell & Flores
206-583-8395
1111 3rd Ave
Seattle, WA
Crosscut
206-382-6137
1411 4th Ave
Seattle, WA
AT&T
(206) 682-0062
1501 4th Ave.
Seattle, WA
Speakeasy , Inc.
(206) 384-4750
1201 Western Ave.
Seattle, WA
X-BAR
(206) 264-XBAR
219 1st Ave. S, #420
Seattle, WA
Vantage Communications
(206) 817-9980
701 5th Ave.
Seattle, WA
Clearwire LLC
206-447-6969
600 Pine St
Seattle, WA
WhitePages, Inc.
(206) 973-5100
1301 5th Ave
Seattle, WA
GCI Advantage Video
(888) 439-4224
1520 4th Ave.
Seattle, WA

provided by: 
Originally published at Internet.com


This is the second in a 10-part series thaat will introduce you to the JavaScript language. In the last article, we wrote a simple script that involved variables, alerts, prompts, string concatenation and some basic arithmatic operators. To test the script we wrote, you can click here. If you don't fully understand any of these concepts, you should read, or re-read the first article. If you tested the script we wrote in the last article, you might have noticed that the result you got from the prompt needs some validation. When the script asks you how many apples you would like to eat, you could enter a number greater than 5, a number less than 0, or something that isn't even a number at all. In each of these cases we would like to inform our user that what they entered is not valid.

Since there are only 5 apples in our script, that is the most apples that our user can take. So we will start by checking to see if the number they entered is greater than 5. var apples = 5; alert('There are currently ' + apples + ' apples!'); var eat = prompt('How many apples would you like to eat?', '1'); var eaten = parseInt(eat); if(eaten > 5){ alert('Sorry, but there are only 5 apples. You can not eat ' + eaten + ' apples!'); } else { apples -= eaten; alert('Now there are only ' + apples + ' apples!'); } Test It The main concepts that we are introducing here are the "if" and "else" statements. If and Else statements are fairly simple to understand. If you look at the code above, you could say "If the user selected more than 5 apples to eat, tell them that there are not that many apples. Otherwise, let them eat as many apples as they entered". The basic syntax for an if / else statement is: if(condition){ // code to execute if condition is true } else { // code to execute if condition is false } You should notice the open and close brackets, { and } in the above code. The open bracket tells the code where to start a certain block of code, and the close bracket tells the code where to end. So anything between { and } is executed as part of your if statement. You should notice that the close bracket for the if statement is immediately before the keyword "else". The else statement then has its own set of brackets and thus its own block of code to execute. The slashes, //, in the above example tell our code that we have a comment. A comment is part of your code that is not executed. It is generally used to describe functionality of the actual code so that you do not have to read through code to figure out what it is doing. If, for example, you had a very lengthy piece of code that validated inputs on a form, it would be a good idea to place a comment saying something like "The following code validates the user input for the contact form". This way if anyone else looks at your code, or if you yourself look at your code several months down the line, you know what it does right away. There are two ways to write a comment in JavaScript. The first, as you have already seen, is with //. Anything following a // on a line is considered to be a comment, and is thus ignored when your code is executing. The other way is with /* and */. If you place these in your code, anything between them is ignored. // this is a one line comment. /* if you need a longer comment, as is often the case, it is usually a good idea to use a "block comment". This comment is a block comment, and the entire thing is ignored when your code is executed. */ For shorter scripts, comments are not always necessary. When your code gets longer, however, they become a necessity. Programmers will very often have to look through thousands of lines of code trying to

Author: Mark Kahn

Read article at Internet.com site

Featured Local Company

X-BAR

(206) 264-XBAR
219 1st Ave. S, #420
Seattle, WA
http://www.xbarit.com/