Login form



Javascript. Syntax. Basics. Loops, variables, conditions.
Declaring Variables

You do this using the var statement. The only time you must use the var statement is when declaring variables that are local to a function. At all other times, using the var statement to declare variables before their use is a recommended practice.
The following code examples are of variable declaration:
var mim = "A man, a plan, a canal, Panama!"; 

var ror = 3;       

var nen = true;       

var fif = 2.718281828       

JavaScript is a case-sensitive language, so naming a variable myCounter is different from naming it MYCounter. In addition, variable names, which can be of any length, must follow certain rules:

The first character must be a letter (either uppercase or lowercase) or an underscore (_), or a dollar sign ($).
Subsequent characters can be letters, numbers, underscores, or dollar signs.
The variable name can't be a reserved word.


Some examples of valid variable names:
_pagecount
Part9
Number_Items

Some invalid variable names:

99Balloons // Starts with a number.
Smith&Wesson // Ampersand (&) is not a valid character for variable names.

As JavaScript is a loosely-typed language, variables in JavaScript technically have no fixed type. Instead, they have a type equivalent to the type of the value they contain. It is possible, under some circumstances, to force the automatic conversion (or coercion) of a variable or a piece of data into a different type. Numbers can easily be included in strings, but strings cannot be included directly in numbers, so explicit conversion functions, parseInt() and parseFloat(), are provided:
var theFrom = 1;

var theTo = 10;

var doWhat = "Count from ";

doWhat += theFrom + " to " + theTo + ".";

After this code is executed, the doWhat variable contains "Count from 1 to 10." The number data have been coerced into string form.
var nowWhat = 0;
nowWhat += 1 + "10";  // In this case, because "10" is a string,
// the "+=" operator concatenates.


After this code is executed, the nowWhat variable contains "0110". The following steps are followed to arrive at this result:
Look at the types of 1 and "10". The "10" is a string, the 1 is a number, so the number is coerced into a string.
As the values on either side of the + operator are both strings, do a string concatenation. This results in "110"
Look at the types of the values on either side of the +=. nowWhat contains a number, and "110" is a string, so convert the number to a string.

As there are now strings on either side of the += operator, do a string concatentation. This results in "0110".
Store this result in nowWhat:
var nowThen = 0;
nowThen += 1 + parseInt("10");        // In this case, "+=" performs addition.


After this code is executed, the nowThen variable contains the integer 11.

Using Conditional Statements

The following examples demonstrate syntaxes you can use with if and if...else statements. The first example shows the simplest kind of Boolean test. If (and only if) the item between the parentheses evaluates to true, the statement or block of statements after the if is executed:
if (newShip)
smash(champagneBottle,bow);

if (rind.color == "deep yellow " && rind.texture == "large and small wrinkles")
{
theResponse = ("Is it a Crenshaw melon? <br> ");
}

var theReaction = "";

if ((lbsWeight > 15) || (lbsWeight > 45))

{

theReaction = ("Oh, what a cute kitty! <br>");

}

else

theReaction = ("That's one huge cat you've got there! <br>");


Using for Loops

The for statement specifies a counter variable, a test condition, and an action that updates the counter. Just before each time the loop is executed (this is called one pass or one iteration of the loop), the condition is tested. After the loop is executed, the counter variable is updated before the next iteration begins.
If the condition for looping is never met, the loop is never executed at all. If the test condition is always met, an infinite loop results. While the former may be desirable in certain cases, the latter rarely is, so take care when writing your loop conditions:
var howFar = 11; 
for(var icount = 1; icount < howFar; icount++)  {        // Counts from 1 through 10 in this case.
}


Using while Loops

The while loop is very similar to a for loop. The difference is that a while loop does not have a built-in counter variable or update expression. If you already have some changing condition that is reflected in the value assigned to a variable, and you want to use it to control repetitive execution of a statement or block of statements, use a while loop:

var theCount = 42;        // Initialize the counter variable.
while (theCount >= 1)  {
}
theCount--;        // Update the counter variable.


 
[ BBC news ][ Yahoo news ][ Linux guru ][ Webmaster ACE ]