Microsoft® JScript Writing JScript Code |
JScript Tutorial Previous Next |
Like many other programming languages, Microsoft JScript is written in text format, and is organized into statements, blocks consisting of related sets of statements, and comments. Within a statement you can use variables, immediate data such as strings and numbers, and expressions.
A JScript code statement consists of one or more items and symbols on a line. A new line begins a new statement, but it is a good idea to terminate your statements explicitly. You can do this with the semicolon (;), which is the JScript termination character.A group of JScript statements that is surrounded by braces ({}) is called a block. Blocks of statements are used, for example, in functions and conditionals. In the following example, the first statement begins the definition of a function, which consists of a block of five statements. The last three statements, which are not surrounded by braces, are not a block and are not part of the function definition.aBird = "Robin"; var today = new Date();function convert(inches) { feet = inches / 12; // These five statements are in a block. miles = feet / 5280; nauticalMiles = feet / 6080; cm = inches * 2.54; meters = inches / 39.37; } km = meters / 1000; // These three statements are not in a block. kradius = km; mradius = miles;
A single-line JScript comment begins with a pair of forward slashes (//). A multiline comment begins with a forward slash and asterisk in combination (/*), and ends with the reverse (*/).aGoodIdea = "Comment your code thoroughly."; // This is a single-line comment. /* This is a multiline comment that explains the preceding code statement. The statement assigns a value to the aGoodIdea variable. The value, which is contained between the quote marks, is called a literal. A literal explicitly and directly contains information; it does not refer to the information indirectly. (The quote marks are not part of the literal.) */ // This is another multiline comment, written as a series of single-line comments. // After the statement is executed, you can refer to the content of the aGoodIdea // variable by using its name, as in the next statement, in which a string literal is // appended to the aGoodIdea variable by concatenation to create a new variable. var extendedIdea = aGoodIdea + " You never know when you'll have to figure out what it does.";
The equal sign (=) is used in JScript to indicate the action of assigning a value. That is, a JScript code statement could sayIt means "Assign the value 3 to the variable anInteger," or "anInteger takes the value 3." When you want to compare two values to find out whether they are equal, use a pair of equal signs (==). This is discussed in detail in Controlling Program Flow.anInteger = 3;
A JScript expression is something that a person can read as a Boolean or numeric expression. Expressions contain symbol characters like "+" rather than words like "added to". Any valid combination of values, variables, operators, and expressions constitutes an expression.var anExpression = "3 * (4 / 5)"; var aSecondExpression = "Math.PI * radius * 2"; var aThirdExpression = aSecondExpression + "%" + anExpression; var aFourthExpression = "(" + aSecondExpression + ") % (" + anExpression + ")";