Javascript : Dealing with numbers
Tags: javascript, beginners, programming, tutorial
Published: 2021-10-17 11:07:59

<p></p><div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-8q6l5LEx6yI/YKwQTp6UYbI/AAAAAAAAALg/AqnlJEak6Rkzbm4MJLq8TEDBuKJ7Nz5LACLcBGAsYHQ/s800/javascript%25233.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Javascript : #3 Dealing with numbers" border="0" data-original-height="300" data-original-width="800" src="https://1.bp.blogspot.com/-8q6l5LEx6yI/YKwQTp6UYbI/AAAAAAAAALg/AqnlJEak6Rkzbm4MJLq8TEDBuKJ7Nz5LACLcBGAsYHQ/s16000/javascript%25233.png" title="Javascript : #3 Dealing with numbers" /></a></div><br /><br /></div><br /><span style="font-family: Ubuntu; font-size: large;"><br /></span><p></p><p><span style="font-family: Ubuntu; font-size: large;">In Javascript , you can do <b>operations</b> on numbers to help you do <b>calculations</b> and make things easier.</span></p><p><span style="font-family: Ubuntu; font-size: large;"><br /></span></p><h1 style="text-align: left;"><span style="font-family: Ubuntu; font-size: x-large;">Basic operations on numbers</span></h1><div><ul style="text-align: left;"><li><span style="font-family: Ubuntu; font-size: large;">Addition : using " + " operator&nbsp;</span></li></ul></div><pre><code class="javascript">var sum ;
sum = 10 + 5 ;
console.log(sum); // output : 15</code></pre></br></br><ul style="text-align: left;"><li><span style="font-family: Ubuntu; font-size: large;">Subtraction : using " - " operator</span></li></ul><pre><code class="javascript">var difference ;
difference = 22 - 10 ;
console.log(difference); // output : 12</code></pre></br></br><ul style="text-align: left;"><li><span style="font-family: Ubuntu; font-size: large;">Division : using " / " operator</span></li></ul><pre><code class="javascript">var quotient ;
quotient = 50 / 5 ;
console.log(quotient); // output : 10</code></pre><ul style="text-align: left;"><li><span style="font-family: Ubuntu; font-size: large;">Multiplication : using " * " operator</span></li></ul><pre><code class="javascript">var product ;
product = 7 * 2;
console.log(product); // output : 14</code></pre><ul style="text-align: left;"><li><span style="font-family: Ubuntu; font-size: large;">Getting remainder of division : using " % " operator</span></li></ul><pre><code class="javascript">var remainder ;
remainder = 10 % 4;
console.log(remainder); // output : 2</code></pre><ul style="text-align: left;"><li><span style="font-family: Ubuntu; font-size: large;">Raising a number to a power : using the " ** " operator</span></li></ul><pre><code class="javascript">var num = 3;
squaredNum = num ** 2;
console.log(squaredNum) // output : 9
</code></pre></br><span style="font-family: Ubuntu;"></br></br><h1>Augmented operations on numbers</span></h1><div><span style="font-family: Ubuntu; font-size: large;">Sometimes , you want to <b>update </b>a variable storing a <b>numeric value</b> by doing any of the operations (like : adding to it , subtraction from it , dividing or multiplying it by something).</span></div><div>class="javascript"><span style="font-family: Ubuntu; font-size: large;">But you don't have to use the long form like the following one.</span></div><pre><code class="javascript">var num = 2 ;
// adding 3 to the value of num and assigning this value (5) to the variable num
num = num + 3 ; 
</code></pre><span style="font-family: Ubuntu; font-size: large;">You can shorten it to be as the following example</span><pre><code class="javascript">var num = 2;
 // adding 3 to the value of num and assigning this value (5) to the variable num
num += 3 ;
</code></pre></br><p style="text-align: left;"><span style="font-size: large;"><span style="font-family: Ubuntu;">In the previous example , we used the plus equals </span><span style="font-family: Ubuntu;">operator " += " <br /></span></span><span style="font-size: large;"><span style="font-family: Ubuntu;">which does the same thing as the </span><span style="font-family: Ubuntu;">code example</span><span style="font-family: Ubuntu;"> before it.</span></span></p><p style="text-align: left;"><span style="font-size: large;"><span style="font-family: Ubuntu;">This can be used in </span><span style="font-family: Ubuntu;">the </span><span style="font-family: Ubuntu;">operations (+, -, *, /).</span></span></p></pre><pre style="text-align: left;"><code class="javascript">var num = 20 ;
// examples of augmented operations that can be done
num += 2 ;  
num -= 3 ;
num *= 5 ; 
num /= 11 ;
</code></pre><p style="text-align: left;"><span style="font-family: Ubuntu; font-size: large;"><br /></span></p><p style="text-align: left;"><span style="font-family: Ubuntu; font-size: large;">You can replace the value you use to <b>update</b> your variable by another&nbsp;</span><span style="font-family: Ubuntu; font-size: large;"><b>variable</b> as in the following example</span></p><pre style="text-align: left;"><code class="javascript">var salary = 1500;
var bonus = 250;
console.log(salary); // output : 1500
salary += bonus;
console.log(salary); // output : 1750
</code></pre><span style="font-family: Ubuntu; font-size: large;">Another example</span>
<pre style="text-align: left;"><code class="javascript">var maxTemperature = 32;
var minTemperature = 24;
var averageTemperature =  ( maxTemperature + minTemperature ) / 2 ;
console.log(averageTemperature); // output : 28<span style="font-family: Ubuntu;">
</span></code></pre><h1 style="text-align: left;"><span style="font-family: Ubuntu;"><br /></span></h1><h1 style="text-align: left;"><span style="font-family: Ubuntu;">Incrementing and decrementing by one</span></h1><p style="text-align: left;"><span style="font-family: Ubuntu; font-size: large;">Sometimes you want to update variables by incrementing or decrementing them by one, instead of using (num += 1 or num -= 1) , you can use a shorter way using " ++ " and " -- " operators.</span>/p><pre style="text-align: left;"><code class="javascript"><span style="font-family: Ubuntu;">var num = 6;
num++; // increases it by one to be 7
num--;  // decreases it by one to be 6 again
</span></code></pre></br ></h1><h1 style="text-align: left;"><span style="font-family: Ubuntu;">Order of operations </span></h1><p style="text-align: left;"><span style="font-family: Ubuntu; font-size: large;">If you have a more <b>complex calculation</b>&nbsp;you have to know what is calculated first , the <b>order of operations</b> that the computer follows is the same as the one you studied in your math's classes, which is <b>PEDMAS </b>(from left to right) :</span></p><p style="text-align: left;"></p><ul style="text-align: left;"><li><span style="font-family: Ubuntu; font-size: large;"><b><u>P</u></b>arenthesis&nbsp;(Brackets)</span></li><li><span style="font-family: Ubuntu; font-size: large;"><b><u>E</u></b>xponents ( powers )</span></li><li><span style="font-family: Ubuntu; font-size: large;"><u><b>D</b></u>ivision and <u><b>M</b></u>ultiplication</span></li><li><span style="font-family: Ubuntu; font-size: large;"><b><u>A</u></b>ddition and <u><b>S</b></u>ubtraction</span></li></ul><span style="font-family: Ubuntu; font-size: large;"><div><span style="font-family: Ubuntu; font-size: large;"><br /></span></div>Some people make it funnier and easier to remember by saying it like : " <b><u>p</u></b>lease <b><u>e</u></b>xcuse <b><u>m</u></b>y <u><b>d</b></u>ear <u><b>a</b></u>unt <u><b>S</b></u>ally ! "</span><p></p><div><span style="font-family: Ubuntu; font-size: large;"><b>Example</b> :&nbsp;</span><span style="font-family: Ubuntu; font-size: large;">in ( 2 * (4 + 6) ** 2 / 5 - 1) :</span></div><div><ul style="text-align: left;"><li><span style="font-size: large;"><span style="font-family: Ubuntu;"><b>first</b> ( 4 + 6 )&nbsp;</span><span style="font-family: Ubuntu;">&nbsp;is calculated </span><span style="font-family: Ubuntu;">to be 10 ,&nbsp;</span></span></li><li><span style="font-family: Ubuntu; font-size: large;"><b>then</b> it's raised to the second power to be 100 ,</span></li><li><span style="font-family: Ubuntu; font-size: large;"><b>then</b> it's multiplied by 2 to be 200 ,&nbsp;</span></li><li><span style="font-family: Ubuntu; font-size: large;"><b>after that,</b> it's divided by 5 to be 40 ,</span></li><li><span style="font-family: Ubuntu; font-size: large;"><b>and finally,</b> 1 is subtracted from it to be 39.</span></li></ul></div>