Cover by Marijn Haverbeke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Program Structure

One-line programs are not very interesting. When you put more than one statement into a program, the statements are executed, predictably enough, one at a time, from top to bottom. This program has two statements; the first one asks the user for a number, and the second one shows the square of that number:

var theNumber = Number(prompt("Pick a number", ""));
alert("Your number is the square root of " + (theNumber * theNumber));

The function Number converts a value to a number, which is needed in this case because the result of prompt is a string value. There are similar functions called String and Boolean that convert values to those types.

Conditional Execution

Sometimes, you do not want all the statements in your program to always ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required