Learn HTML,CSS, Python etc from the foundation

Monday, January 6, 2020

FREE JAVASCRIPT COURSE – Lesson 8

No comments :

JavaScript mathematical operators

Hi friends, today we will be discussing an important topic.  The topic is Mathematical operators! Hey, don’t panic, we are not here to solve maths. I will just walk you through some mathematical operators that enable JavaScript to function in a unique way.  

Some of these mathematical operators in JavaScript include:
     1.     Plus symbol ( + ) : it is used for  adding two variables  together. Hence, it represents an addition.
       2.     Minus symbol ( - ): This is used when you want to perform subtraction.
      3.     The Asterisk symbol ( * ): it is used when you want to perform multiplication.
      4.     Forward slash symbol ( / ): It is used when you want to perform division.
     5.     Percentage symbol ( % ): It is basically used for modulus calculations.

Make sure you note the above mathematical operators very well. We will be using it in most of our subsequent lectures.

Now let us  put what we’ve just learned in to practice. Open your code editor and punch in the following codes.

<!DOCTYPE HTML><HTML>            <HEAD>                        <TITLE>Mathematical Operator</TITLE>                        <script>                                    var number1 = 42;                        var number2 = 100;                        var total;                        total = number1 + number2;                        document.write( total );                        </script>            </HEAD>            <BODY>            </BODY>

It will look like this on your code editor

JavaScript mathematical operators


Please note that the plus symbol(+) between number1  and  number2 does not mean "join together " or concatenation. It simply tells JavaScript to "add up" number1  and  number2.

Now try out something I would like you to take special notice  of. So go back to your codes, precisely line seven (7) which reads: var number2 = 100;

Put  100 in a quotation mark, it will look exactly like this
var number2 = "100";
Save your code and try running it again (or simply refresh your browser). The output will look like this:
42100

Now let me explain what just happened. Whenever you put a variable inside quotation marks, it will automatically turn such variables into text. What JavaScript will simply do is to join them together (or concatenate them), instead of adding them up.  In our next lesson, you will learn more about Concatenation.

Stay cool!


No comments :

Post a Comment