Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted November 19th, 2015
Making calculations, compare numbers and variables with JavaScript

The first version of JavaScript was created in 1995 by Brendan Eich from Netscape Communications Corporation for use in the Netscape Navigator. With JavaScript it became possible to make websites interactive. Although these days we can use HTML5 and CSS3 for a lot of these interactive elements. JavaScript still has a big and important roll in the world wide web.

Because I’m learning JavaScript at the moment I would like to show you and teach you some basics about this awesome programming language.

Making calculations and compare numbers.

JavaScript can be useful when it comes to calculations and comparing numbers. You can make a sum where you have two numbers and JavaScript can check witch of the 2 has the higher value and return that to your console. Also let’s say that we have a web shop, it can sum up the prices of your shopping card, subtract a 10% discount and show you the price including and excluding the tax.

Let’s see how things work in code, in JavaScript you can make calculations. The first and important thing you need to know here is that the order here is the standard PEMDAS order. PEMDAS stand for, Parenthesis, Exponents, Multiplication, Division, Addition and Subtraction. This is the order that JavaScript does the calculations. More information about PEMDAS on Wikipedia.

Now let’s see what we can do with this. Note that in the examples after > is what we enter in the console and after => is what gets returned from the code. Now we can make simple calculations like:

> 14 - 5

=> 9

> 10 * 3

=> 30

> 25 / 5

=> 5

> 9 + 12

=> 21
Now let’s see a few longer and more complicated sums where we also use the PAMDAS method:

> (3 + 8) * 5
    11    * 5

=> 55
Example 1: first we do the part within the Parenthesis (3 + 8) = 11 after that we can multiply the 11 by 5 what gives the result of 55.

> (6 * 3) + 2 - 10 / 2
    18    + 2 - 10 / 2
    18    + 2 -    5

=> 15
Example 2: We first multiply the part within the Parenthesis, then we devide the 10 by 2. And after that we can add 2 to 18 and subtract the 5 what gives a result of 15.

> (-8 * 4) - 4 * -3
    -32    - 4 * -3
    -32    -  -12

=> -44
Example 3: Again we Multiply the part in the Parenthesis first. Then we Multiply the -4 by -3 and then we Subtract the results what gives the result of -44.

Compare numbers in JavaScript

So now that we know how to make sums in JavaScript we can start comparing numbers. I will show you the most common Number Comparators used in JavaScript Syntax. Whenever you compare numbers in JavaScript they’ll return a true or false statement to your console. Now let’s see this in action.

> 9 > 2

=> true
greater then: is 9 greater then 2 returns true.

> 14 < 9

=> false
less then: is 14 less then 9 returns false.

> 9 >= -11

=> true
greater then or equal to: is 9 greater then or equal to -11 returns true.

> 6 == 1

=> false
equal to: is 6 equal to 1 returns false.

> 9 != 7

=> true
not equal to: is 9 not equal to 7 returns true.

Posted by Alex Lagendijk

Leave a Reply

Your email address will not be published. Required fields are marked *