Basic Programming Tutorials

JavaScript Operators

Welcome to the JavaScript tutorials, we have already covered JavaScript popup box, and JavaScript variables. Today we will discuss JavaScript operators to understand its use for the long run. The JavaScript operators are not different then other programming languages. You can use JavaScript operators to add, subtract, and assign values etc.

Types of JavaScript operators

  • Arithmetic operator – You can use it to perform the arithmetics, like addition, subtraction, multiplication and division etc.
  • Assignment operator – It will let you assign values to the JavaScript Variables.
  • Bitwise operator – This operator will work on the 32 bits. It means it will work on the binary representation of the integer, decimal or other number. Later it will return the number.
  • Logical operator – You can use this operator with Boolean values.
  • Comparison operator –You can use it to compare two operands, and return the result based on the comparison.
  • Conditional operator – You can use it to test the condition, and then execute the relevant statement.

Arithmetic JavaScript operators

Now we will see the list of JavaScript operators in Arithmetic, that you can use:

  • Addition ‘+’ – It will let you add two operands. For example, if A=30, and B=20, then the A + B = 50.
  • Subtraction ‘-‘ – It will subtract the two operands. For example, if A =40, and B=10, then the A – B = 30.
  • Multiplication ‘ *’ – It will multiply both operands. For example, if A=10, and B=20, then the A*B =200.
  • Divide ‘/’ – It will divide the one operand with the other. For example, if A =100, and B=10, then the A/B=10.
  • Modulus ‘%’ – It will divide the one operand with the other, and will return the remainder. For example, if A=9, and B=3, then the A%B=0.
  • Increment ‘++’ – It will increment the value by 1. For example A=1, then A++= 2.
  • Decrement ‘ –‘ – It will decrement the value by 1. For example A=2, then A– =1.

Assignment JavaScript Operators

Now we will see the list of JavaScript operators in Assignment, that you can use:

  • Assignment operator ‘=’ – It will let you assign values to the operands. For example, A =10, and B=10, then  C = A or C=B.
  • Addition Assignment ‘+=’ –It will let you assign value to the left operand after adding left and right operands. For example, A=10, and B=10, then A+=B will give you the result A=20. It is like A= A+B.
  • Subtraction Assignment ‘-=’ – It will let you assign value to the left operand after subtracting the right and left operands. For example, A=10, and B=10, then A-=B will give you the result A=0. It is same like A= A-B.
  • Multiplication Assignment ‘*=’ – It will let you assign value to the left operand after multiplying the left and right operands. For example, A=10, and B=10, then A*=B will give you the result A=100.
  • Division Assignment ‘/=’ – It will let you assign the value of the left operand after dividing the left and right operands. For example, A=10, and B=10, then A/=B will give you the result A=1.
  • Modulus or remainder Assignment ‘%=’ – It will let you assign value to the left operand after getting the remainder of left and right operands. For example, A=10, and B=10, then A%=B will give you the result A=0.

Note: Things are not different for bitwise JavaScript operators as well. We left it for you to practice, otherwise you can use JavaScript Reference.

Bitwise JavaScript Operators

Now we will see the list of JavaScript operators in Bitwise, that you can use:

  • And ‘&’ operator – It will perform the Boolean ‘&’ function on each bit. For example, A=3, and B=4, then A & B= 0.
  • OR ’|’ operator – It will perform the Boolean ‘|’ function on each bit. For example A=3, and B=4, then A |B=7.
  • XOR ^ operator – It will perform the Boolean ‘^’ function on each bit. For example A=3, and B=4, then A^B=7.
  • NOT (~) operator – It will perform the Boolean ‘~’ function on each bit. For example A=3, and B=4, then A~B=-5.
  • Shift Left ‘ <<’ operator – It will perform the Boolean ‘<<’ function on each bit. For example A=3, and B=4, then A<<B=48.
  • Shift Right ‘>>’ operator – It will perform the Boolean ‘>>’ function on each bit. For example A=3 and B=4, then A>>B=0.

Logical JavaScript operators

Now we will see the list of JavaScript operators in Logical, that you can use:

  • Logical AND ‘&&’ operator – If you are using Logical ‘&&’ operator with the Boolean values, then it will return true only if the both operands are true. You will get false result, if any operand value is false.
  • Logical OR ‘||’ operator – If you are using logical OR operator with the Boolean values, then it will return true, if any or both the operands are true. You will get false result, if both the operand value is false.
  • Logical Not (!) Operator – You can use this operator to reverse the result of the single operand or the result of operands.

Comparison JavaScript operators

Now we will see the list of JavaScript operators in comparison, that you can use:

  • Equal ‘==’ operator – You can use this operator to check the value of two operands. If the value is equal, then it will return the true.
  • Not equal (!=) Operator – It will work opposite to the equality operator. If their value is not equal, then it will return the true.
  • Precise equal ‘===’ operator – You can use this operator to check the value and type of the two operands. If its value is different, but the type is same, then it will return false. In case the type is same but the value is different, still it will return the false value. For example, A=3, and B=”3”, then the result of A===B will be false. Here you can see type is different.
  • Precise not equal ‘!==’ Operator – It will work opposite to the ‘===’ operator. It will return true, if either operand value is not same or the operand type is not same.
  • Greater than ‘>’ operator – It will let you find the greater operands. If the left operand is greater than right operands, then it will return the true else false for the right operand. For example, A=4, and B=8, then A>B will give you false results.
  • Greater than or equal to ‘>=’ operator –It will work like ‘>’ operator with the additional check. It will check whether the left operand is greater than right operands or its value is equal or not. If any condition is true, then it will return the true.
  • Less than ‘<’ operator – This operator will let you check, whether the left operand is less than the right operand or not. If yes, then it will return the true, otherwise it will return false.
  • Less than or equal to ‘=<’ operator – It will work like ‘<’ operator with the additional check. It will let you check, whether the left operand is less than the right operand or its value is equal or not. If any condition is true, then it will return the true.

Conditional operator

We will see the use of the condition in JavaScript operators. You can use the conditional JavaScript operators to evaluate an expression first, and then it will execute the statements.

condition?  Statement1: Statement2

For example:

Now we will see the use of conditional JavaScript operators, in the below example we have set the values x=10, and y=20, Then let’s find out x<y. If the condition will be true, then it will return ‘yes’, else it will return the ‘no’ result.

<html >

<body>

<script type="text/javascript">

<!--

var x = 10;

var y = 20;

document.write("((x < y) ? yes : no) => ");

SharpResult = (x < y) ? "yes" : "no";

document.write(SharpResult);

//-->

</script>

</body>

</html>

The output will be:

((x < y) ? yes : no) => yes

As we know that the value of x is 10, and y is 20. We have checked whether the x is less than y, since the condition is true so it will return yes.

Conclusion

We have just covered JavaScript operators. These are the basics for you to stand firm in the JavaScript world. If your basics are not so strong, then you will probably wobble down. At least we always ask our reader to practice more. Because practice will make you perfect in JavaScript.