JavaScript operators are used to compare, count, assign or to calculate. They are grouped into these classes;
1. Assignment operators
2. Comparison operators
3. Arithmetic operators
4. Counting operators
1. Assignment operators
2. Comparison operators
3. Arithmetic operators
4. Counting operators
JS Assignment operators
Assignment operators are used to pass (or give) values to a variable. Look at the following for example
var who = "emmanuel";
The symbol "=" is the assignment operator in the example above. It passed a string value "emmanuel" to the variable name "who". The equals sign is the most used operator in almost all high level languages. There are more operators though.
+=
An example of the above operator usage is
C +=2;
What this basically means is that variable C be incremented by 2. The JavaScript statement above can be rewritten as var C = C + 2; which means that the new value of C should be the old value of C plus 2.
-=
The same principle applies for the operator above.
C -=2;
Means variable C be decremented by 2 everytime the statement is encountered. It can also be re-written as: C = C - 2.
*= and /= follow the exact same principle where asterick (*) is for multiplication and front-facing slash(/) is for division.
JS Comparison operators
Comparison operators are used for evaluation and comparison purposes. The following operators are used for comparison.
==, <=, >=, !=
== -> Means is it equal to?
<= -> is this less than or equal to?
>= -> is this greater than or equal to?
!= -> is this not equal to?
=== -> explicitly equal to?
var number1 = 5;
var number2 = 8;
var number3 = "8"; //Note that number3 here is holding a string value 8.
Comparison operators are answer seeking. Their output begs either a true or false answer. Using the JS variables declared above, consider the following statements below.
1
number1 >= number2
(5 is greater than or equal to 8 ?)
(5 is greater than or equal to 8 ?)
The statement above reports a false because the integer value 5 is not greater or equal to 8.
2
number1 != number2
(5 is not equal to 8 ?)
(5 is not equal to 8 ?)
The above statement resolves to true since the integer number 5 is not the same as 8
3
number1 == number2
(5 is equal to 8?)
(5 is equal to 8?)
The statement above compares the numeric values 5 and 8 and outputs false since they are not equal.
4
number2 == number3
(8 is equal to "8"?)
Statement above compares the integer value 8 in number2 to the string value 8 in number3 and returns a true even though the datatypes are different.
5
number2 === number3
(8 is both value-wise and data-type-wise equal to "8"?)
The statement above resolves to a false. Even though both variables contain the same value. But they are of different types.
JS Arithmetic operators
Arithmetic operators are used to perform calculations such as addition, subtraction, multiplication, division and modulus on numeric or floating point values. These operators are represented by the basic mathematical symbols (+, -, *, /, %). Yes, an asterisk is for multiplication not X - ecks.
var number1 = 5;
var number2 = 8;
var number3 = "8"; //Note that number3 here is holding a string value 8.
number1 * number2; //outputs 40.
number2 % number5; //outputs 3 (modulus is the reminder after division)
I believe you get the idea.
number1 + number3;
(5 + "8")
58 is output because the value of number3 is not a number. It is a string. JavaScript will therefore proceed to do what is called a concatenation of the two values which is simply the joining of the two values. More on concatenation in the near future.
JS Counting operators
Counting operators are for counting basically. And it does this by either incrementing or decrementing by one upon every encounter. (++, -- )
var number1 = 5;
var number2 = 8;
var increOne = ++number1; //increment number1 by one before assigning the value of number1 to increOne.
var increTwo = number2--; //Means, assign the value of number2 to increTwo before decrementing number2 by one
Now, when the counting operator is placed before a variable it means, the variable must first be incremented or decremented(depending on the symbol used) by one before its value is used.
When the counting operator is placed after a variable, it means the variable's value should be used before it is either incremented or decremented (depending on the symbol used)
alert(number1++);
// This displays 5 in a dialogue box
alert(number1);
// This displays 6 in a dialogue box now, because its value has been incremented by one in the earlier statement.
Feel free to drop your observation, a comment or a question in the comment section below.
Joseph Kayode Agbede
great
ReplyDelete