create account

The JavaScript Journey #2 - Arithmetic and Operators by adnanrahic

View this thread on: hive.blogpeakd.comecency.com
· @adnanrahic · (edited)
$0.05
The JavaScript Journey #2 - Arithmetic and Operators
![](https://cdn.pixabay.com/photo/2016/02/19/11/36/classroom-1209820_960_720.jpg)

If you missed lesson #1, you can take a look at it [here](https://steemit.com/programming/@adnanrahic/the-javascript-journey-1-values).

## Intro
Last lesson you gained the understanding of how computers work, what bits are, and how to use the binary numbering system. You learned what basic data types are, their characteristics and how they behave. We only touched the definitions and basic use cases of Numbers.

## Now you'll see why math is important.

This time you will see how to make numbers interact with each other. The main thing you can do with numbers is arithmetic.

```99 + 6 * 12```

The ``` + ``` and ``` * ``` symbols are called **operators**. What do you think happens first in the expression above? Have in mind the first operator stands for addition while the second stands for multiplication. Putting an operator between two values will cause it to produce a new value. Remember math class in primary school? Do you realize now how important that was?

The result of the expression above is ```171```. First, ```6``` and ```12``` are multiplied, then the result is added.

What about now?
```
(99 + 6) * 12
22 - 5 / 7
7 / (27 + 3) * 99
```

When operators appear together without parentheses, the order in which they are applied is determined by the precedence of the operators. First multiplication and division are calculated, then addition and subtraction. Same as when using operators in math!

The four main arithmetic operators are, like in math: ```+, -, *, /'```. However, we have one more special operator in JavaScript. The ```%``` symbol is used to represent the remainder operator. ```X % Y``` is the remainder of dividing ```X``` and ```Y```.
```
314 % 100 = 14
```
This operation is often called modulo or modulus. However remainder is a but more accurate.

## What about strings?
Good question. Strings cannot be manipulated with arithmetic operators. They cannot be divided, multiplied nor subtracted. Now comes the funny bit. The ```+``` operator can be used on strings. But this process does not add strings. It only glues them together in a process called **concatenation**.

This expression will form the string value *concatenate*.
```
'con' + 'ca' + 'te' + 'nate'
```

## Unary operators
Enough about arithmetic, let's talk about something more interesting. Operators don't need to be symbols, there are many different types of operator, one of which stands out.
```
typeof
```
This operator is unary, which means it operates on a single value.
```
typeof 12.7
```
This expression will return a single string value naming the type of the value to the right of the operator. In this case, ```typeof 12.7``` would be equal to ```'number'```. What do you think would be the value of the expression below?
```
typeof 'x'
```
If you guessed ```'string'```, then you deserve a gold star! Good job, you're getting the hang of this.

All arithmetic operators are **binary** which means they operate on **two** values. The operator, ```typeof``` is not, as it only operates on one. In all general cases, you will only ever come across the fact that ```typeof``` is the only unary operator in JavaScript. However, JavaScript is a bit freaky, so it has its exceptions. The minus operator ```(-)``` can also be a unary operator.
```
- (16 - 5)
```
The expression above equals ```-11```. The minus operator changes the sign of the number value, making it, in turn, a unary operator.

![](https://cdn.pixabay.com/photo/2016/11/29/01/16/abacus-1866497_960_720.jpg)
## Binary operators
To understand the concept of binary operators you first need to get acquainted with **Boolean** values. A Boolean value is simply a value that has the possibility of being either ```true```, or ```false```. Just like a light switch. It's either on, or off. A value is present, or it is not. The values of ```true``` and ```false``` are actual values stored in the computer's memory.

So, what's a binary operator now? It compares two values. It **operates** on two values.
```
3 > 2
7 < 4
```
These are examples of binary operators, the less than sign ```(<)```, and the greater than sign ```(>)```. Applying these operators to two numbers, as in this case, has the result in the shape of a Boolean value that indicates whether the expression holds true. Remember primary school math? How many more operators like these two are there?

- ```>=``` is greater than or equal to
- ```<=``` is less than or equal to
- ```==``` is equal to
- ```!=``` is not equal to

Which of these expressions hold true?
Don't look at the solutions below before trying it yourself!
```
1. 9 / 3 + 1 <= 4 * 2 - 4
2. 2 + 5 * 2 > 3 - 4 / 2
3. 1 * 54 == 25 / 5 * 20
```
Solutions:
```
1. true
2. true
3. false
```

## Summary
Great work! You've brushed up on your math skills and applied them in the field of programming. Arithmetic and operators are the core building blocks of any program. These expressions are used to check the validity in many parts of our programs. The ```true``` or ```false``` value dictates whether the program will continue executing or follow a totally opposite flow of control. These concepts will all be crystal clear to you in a few lessons when we get into them in more detail. Join me in the next lesson when I'll be writing about **logical operators**, **special values**, and **precise type comparisons**

___

*Hope you guys and girls had as much fun reading this article as I had writing it!*
*Drop an upvote if you liked it, share if you believe it will be of help to someone.*
*It means a lot to me.*

*Feel free to ask any questions you have in the comments below.*
👍  , , , , , , , , , , , , , , , ,
properties (23)
authoradnanrahic
permlinkthe-javascript-journey-2-arithmetic-and-operators
categoryprogramming
json_metadata{"tags":["programming","howto","education","technology","steemit"],"image":["https://cdn.pixabay.com/photo/2016/02/19/11/36/classroom-1209820_960_720.jpg","https://cdn.pixabay.com/photo/2016/11/29/01/16/abacus-1866497_960_720.jpg"],"app":"steemit/0.1","format":"markdown","links":["https://steemit.com/programming/@adnanrahic/the-javascript-journey-1-values"]}
created2017-06-10 15:28:48
last_update2017-06-10 19:12:57
depth0
children6
last_payout2017-06-17 15:28:48
cashout_time1969-12-31 23:59:59
total_payout_value0.050 HBD
curator_payout_value0.002 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,678
author_reputation1,605,311,705,685
root_title"The JavaScript Journey #2 - Arithmetic and Operators"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,536,835
net_rshares18,007,307,766
author_curate_reward""
vote details (17)
@rycharde ·
Sorry, too late for an upvote but thanks for the post. After years teaching maths, I wish we would abandon standard confusing algebraic notation in favour of expressions written in computer code, so every operator has a symbol and the logic, once learnt, is unalterable.

eg students freak out when seeing 2(6-4) as there is no operator between the '2' and the '('.
👍  
properties (23)
authorrycharde
permlinkre-adnanrahic-the-javascript-journey-2-arithmetic-and-operators-2017622t02645675z
categoryprogramming
json_metadata{"app":"chainbb/0.2","format":"markdown+html","tags":[]}
created2017-06-21 17:27:15
last_update2017-06-21 17:27:15
depth1
children0
last_payout2017-06-28 17:27:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length365
author_reputation19,101,504,594,449
root_title"The JavaScript Journey #2 - Arithmetic and Operators"
beneficiaries
0.
accountchainbb
weight1,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id5,523,440
net_rshares94,500,461
author_curate_reward""
vote details (1)
@steemitboard ·
Congratulations @adnanrahic! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png)](http://steemitboard.com/@adnanrahic) Award for the number of upvotes

Click on any badge to view your own Board of Honnor on SteemitBoard.
For more information about SteemitBoard, click [here](https://steemit.com/@steemitboard)

If you no longer want to receive notifications, reply to this comment with the word `STOP`

By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-adnanrahic-20170612t200501000z
categoryprogramming
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2017-06-12 18:05:00
last_update2017-06-12 18:05:00
depth1
children0
last_payout2017-06-19 18:05:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length689
author_reputation38,975,615,169,260
root_title"The JavaScript Journey #2 - Arithmetic and Operators"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,672,830
net_rshares0
@sulejman ·
I'm learning programming again with your articles and it's fun :D
properties (22)
authorsulejman
permlinkre-adnanrahic-the-javascript-journey-2-arithmetic-and-operators-20170610t153253143z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-10 15:32:54
last_update2017-06-10 15:32:54
depth1
children1
last_payout2017-06-17 15:32:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation24,910,391,297
root_title"The JavaScript Journey #2 - Arithmetic and Operators"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,537,018
net_rshares0
@adnanrahic ·
I'm just glad to be making a difference. Enjoy! :)
properties (22)
authoradnanrahic
permlinkre-sulejman-re-adnanrahic-the-javascript-journey-2-arithmetic-and-operators-20170610t153527431z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-10 15:35:27
last_update2017-06-10 15:35:27
depth2
children0
last_payout2017-06-17 15:35:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length50
author_reputation1,605,311,705,685
root_title"The JavaScript Journey #2 - Arithmetic and Operators"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,537,149
net_rshares0
@teambuktu ·
Good post! I have just one remark on the small paragraph about concatenation: The strings should be enquoted, or the expression would cause a runtime error.
properties (22)
authorteambuktu
permlinkre-adnanrahic-the-javascript-journey-2-arithmetic-and-operators-20170610t185650049z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-10 18:56:57
last_update2017-06-10 18:56:57
depth1
children1
last_payout2017-06-17 18:56:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length156
author_reputation33,800,779,326
root_title"The JavaScript Journey #2 - Arithmetic and Operators"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,547,354
net_rshares0
@adnanrahic ·
You're right! I just fixed it. Thanks for the heads up. Glad you liked the post.
properties (22)
authoradnanrahic
permlinkre-teambuktu-re-adnanrahic-the-javascript-journey-2-arithmetic-and-operators-20170610t191448487z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-10 19:14:57
last_update2017-06-10 19:14:57
depth2
children0
last_payout2017-06-17 19:14:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length80
author_reputation1,605,311,705,685
root_title"The JavaScript Journey #2 - Arithmetic and Operators"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,548,265
net_rshares0