# Introduction to JavaScript Programming – part 3

Introduction to JavaScript Programming – part 3:

Continuing from Introduction to JavaScript Programming – part 2.

In previous note we’re discussed about JavaScript Strings and Arrays. In this part we are going to discuss about 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 a 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.81316125,"math_prob":0.9931437,"size":4573,"snap":"2019-51-2020-05","text_gpt3_token_len":1143,"char_repetition_ratio":0.1733421,"word_repetition_ratio":0.13032886,"special_character_ratio":0.24622786,"punctuation_ratio":0.1216686,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99791193,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-23T04:20:28Z\",\"WARC-Record-ID\":\"<urn:uuid:0e77a0d0-9256-4fa7-803b-ecb7ec5166d3>\",\"Content-Length\":\"61884\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3e639995-eee2-4f88-814f-660450c16408>\",\"WARC-Concurrent-To\":\"<urn:uuid:eb0eb407-28fa-4582-a525-4cf8c5f14a10>\",\"WARC-IP-Address\":\"37.72.171.98\",\"WARC-Target-URI\":\"https://linesh.com/blog/introduction-to-javascript-programming-part-3/\",\"WARC-Payload-Digest\":\"sha1:IDUHX3NLA7LCXKTNWMQCSWN6XQ7HRBXK\",\"WARC-Block-Digest\":\"sha1:D2M65I4C4X3BRA3RWLES2AODHGVBZCDU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250608295.52_warc_CC-MAIN-20200123041345-20200123070345-00075.warc.gz\"}"}