Warning: Undefined variable \$platform in /var/www/lineshjose.com/wp-content/themes/linesh/inc/lib/php-browser-detection.php on line 126
Introduction to JavaScript Programming - Part 3 | LineshJose.Com # Introduction to JavaScript Programming – Part 3

Introduction to JavaScript Programming – part 3:

Continuing from Introduction to JavaScript Programming – part 2.

In the previous note, we’re discussed about JavaScript Strings and Arrays. In this part, we are going to discuss JavaScript Mathematics, JavaScript Assignment Operators and JavaScript Comparison Operators.

### JavaScript Mathematics

You can perform mathematical calculations with JavaScript using normal mathematical notation as shown below.

```nNumA = 9;
nNumB = 4;
nNumC = nNumA + nNumB;
nNumD = nNumA * nNumB;
nNumE = nNumA / nNUmB;```

However, there are a few mathematical operators that you may not be familiar with. The modulus operator (% ) returns the remainder of a division rather than the quotient. The value in nNumC below will be 2.

```nNumA = 12;
nNumB = 5;
nNumC = nNumA % nNumB;```

The negation operator (-) returns the same result as multiplying the value by -1. The value in nNumB below will be -8;

```nNumA = 8;
nNumB = -nNumA;```

JavaScript provides the increment operator (+ + ) to increase value by one, and the decrement operator to decrease a value by one. The value in nNumB below will be 4. The value in nNumC below will be 2.

```nNumA = 3;
nNumB = + + 3;
nNumC = --3;```

Placing the increment or decrement operator in front of the variable means to increment (or decrement) the value first before using it. Placing the increment or decrement operator in after the variable means to use the value first, then increment (or decrement) it.

In the example below, the value of nNumB below will be 5, the value nNumA a will be 5. The value of nNumD below will 4, the value of

```nNumC will be 5.
nNumA = 4;
nNumB = + + A;
nNUmC = 4;
nNUmD = A+ + ;```

JavaScript Assignment Operators
“Assignment” is when the code actually places a value into a variable. For example, the equal sigh below is used to assign the value 2 to the variable nNum.

`nNum = 2;`

If you want to confuse non-programmers, you can use special shorthand assignment operators that perform a mathematical operation and assign a value at the same time. For example, the code below assigns the value of nNum + 2 to nNum. The value of nNum below will be 5.

```nNum = 3;
nNum + = 2;```

The code below does exactly the same thing.

```nNum = 3;
nNum = nNum + 2;```

Assignm ent Operators Used by JavaScript

```nNum -= 3; nNum = nNum - 3;
nNum * = 3; nNum = nNum * 3;
nNum / = 3; nNum = nNum / 3;
nNum % = 3; nNum = nNum % 3;```

### JavaScript Comparison Operators

“Comparison” operators compare the values of two variables or statements and return the result of the comparison as either true or false.

OperatorComparison
>greater than
<less than
= =equal to
< =less than or equal to
> =greater than or equal to

The result of the comparison can be used to control the flow of the program. The if / else structure is the most commonly used control structure.

```nPrice = 29.00;
nHighest = 32.00;
if(nPrice < nHighest)
{
}
else
{
}```

In the code shown above, the program would print “buy it.” because the value of nPrice is lower than the value of nHighest. The comparison operator (< ) returns true, and the code inside the “if” block is executed. If the value of nPrice was higher than the value of nHighest, the result of the comparison would be false and the program flow would drop down to the else statement. The program would print “don’t buy it.”

The “if” structure is frequently used without the “else” block as shown below. In this case, if the value of nPrice was higher than the value of nHighest, the result of the comparison would be false and the program flow would drop-through, not printing anything.

```nPrice = 29.00;
nHighest = 32.00;
if(nPrice nHighest);```

This structure executes the statement inside the brackets, decrementing the value of nPrice. Then it performs a comparison to see if the value of nPrice is greater than the value of nHighest. If the result of the comparison is “true”, the program executes the statement inside the brackets, decrementing the value of nPrice again. This continues until the result of the comparison is “false”. Then the program flow breaks out of the loop.

If you can figure out how the do loop control structure works, then you should be able to figure out how all the basic control flow structures work. You will have no problem programming with JavaScript, or with any other programming language for that matter because all programming is based on a few basic control structures.

This note will continue in Introduction to JavaScript Programming – part 4 page