-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic-operators.js
123 lines (79 loc) · 3.46 KB
/
arithmetic-operators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.
The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).
1 - Addition (+)
The addition operator produces the sum of numeric operands or string concatenation.
Syntax:
Operator: x + y
2 - Subtraction (-)
The subtraction operator subtracts the two operands, producing their difference.
Syntax:
Operator: x - y
3 - Division (/)
The division operator produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.
Syntax:
Operator: x / y
4 - Multiplication (*)
The multiplication operator produces the product of the operands.
Syntax:
Operator: x * y
5 - Remainder (%)
The remainder operator returns the remainder left over when one operand is divided by a second operand.
It always takes the sign of the dividend, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing var1 by var2 — for example — var1 modulo var2.
There is a proposal to get an actual modulo operator in a future version of ECMAScript, the difference being that the modulo operator result would take the sign of the divisor, not the dividend.
Syntax:
Operator: var1 % var2
6 - Exponentiation (**)
The exponentiation operator returns the result of raising first operand to the power second operand.
that is, var1var2, in the preceding statement, where var1 and var2 are variables. Exponentiation operator is right associative. a ** b ** c is equal to a ** (b ** c).
Syntax:
Operator: var1 ** var2
7 - Increment (++)
The increment operator increments (adds one to) its operand and returns a value.
* If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.
* If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.
Syntax:
Operator: x++ or ++x
8 - Decrement (--)
The decrement operator decrements (subtracts one from) its operand and returns a value.
* If used postfix (for example, x--), then it returns the value before decrementing.
* If used prefix (for example, --x), then it returns the value after decrementing.
Syntax:
Operator: x-- or --x
*/
/*
Now let's write code!!!
*/
// Addition (+)
var num1 = 2;
var num2 = 4;
document.write('Result: ' + (num1+num2) + '<br>');
// Subtraction (-)
var num1 = 10;
var num2 = 6;
document.write('Result: ' + (num1-num2) + '<br>');
// Subtraction (/)
var num1 = 10;
var num2 = 5;
document.write('Result: ' + (num1/num2) + '<br>');
// Multiplication (*)
var num1 = 2;
var num2 = 5;
document.write('Result: ' + (num1*num2) + '<br>');
// Remainder (%)
var num1 = 12;
var num2 = 5;
document.write('Result: ' + (num1%num2) + '<br>');
// Exponentiation (**)
var num1 = 3;
var num2 = 3;
document.write('Result: ' + (num1**num2) + '<br>');
// Increment (++)
var num1 = 12;
num1++;
document.write('Result: ' + (num1) + '<br>');
// Increment (--)
var num1 = 12;
num1--;
document.write('Result: ' + (num1) + '<br>');
// you can test this code in: https://jsfiddle.net/