Honesty Rocks! truth rules.

Javascript Operators

HOME      >>       Programming

Tourist

In this part of the tutorial, we will discuss about JavaScript Arithmetic Operators, JavaScript Assignment Operators, Comparison Operators, Logical operators and conditional operator. +, -, *, / etc are the JavaScript Arithmetic Operators. =, +=, -= etc are the JavaScript Assignment Operators. ==, != etc are the Comparison Operators and “&&”, “||” etc are the Logical operators. Arithmetic operators are used to perform arithmetic between variables and/or values and Assignment operators are used to assign values to JavaScript variables. Comparison operators are used in logical statements to determine equality or difference between variables or values. Logical operators are used in determine the logic between variables or values.

The operator “+” is used to add values and/or variables. For an example let p=25, so q=p+5 will gives us a result q=30. The operator “-” is used to subtract. For an example let p=25, so q=p-5 will gives us a result q=20. The operator “*” is used to multiply. For an example let p=25, so q=p*5 will gives us a result q=125. The operator “/” is used to divide. For an example let p=25, so q=p/5 will gives us a result q=5. Modulus (division remainder) operator is “%”. For an example let p=25, so q=p%4 will gives us a result q=1. The Increment operator is “++”.For an example let p=25, so q=++p will gives us a result q=26. . The decrement operator is “--”.For an example let p=25, so q=--p will gives us a result q=24.

The operator “=” is used to show that tow value are equal. For an example let p=50, so q=p will gives us a result q=50. Other important JavaScript Assignment Operators are “+=”, “-=”, “*=”, “/=” and “%=”. If p=25 and q=10, p+=q will gives us a result p=35 (it is similar to p=p+q). If p=25 and q=10, p-=q will gives us a result p=15 (it is similar to p=p-q). If p=25 and q=10, p*=q will gives us a result p=250 (it is similar to p=p*q). If p=25 and q=10, p/=q will gives us a result p=2.5 (it is similar to p=p/q) and if p=25 and q=10, p%=q will gives us a result p=5 (it is similar to p=p%q).

The “is equal to” operator is “==”. For an example, let p=15, so p==20 is false. The “is exactly equal to (value and type)” operator is “===”. For an example, let p=15, so p=== “15” is false but p===15 is true. The “is not equal” operator is “!=”. For an example, let p=15, so p!=20 is true. The “is greater than” operator is “>”. For an example, let p=15, so p>20 is false. The “is less than” operator is “<”. For an example, let p=15, so p<20 is true. The “is greater than or equal to” operator is “>=”. For an example, let p=15, so p>=20 is false. The “is less than or equal to” operator is “<=”. For an example, let p=15, so p<=20 is true.

The “&&” operator is used to describe and, “||” operator for or and “!” operator for not. For example let p=20 and q=10, so
(p < 30 && q > 5) is true, (p==15 || q==15) is false and !(p==q) is true.

JavaScript also contains a conditional operator. This conditional operator assigns a value to a variable based on some condition. Let we want to compare a value of a variable to a pre-defined value, and if it returns true it will gives an output and if not it will returns other output. The syntax is
variable_name=(condition)?value1:value2
Let the variable name is permission; we want that only 18+ age are welcomed here others are not. So, the condition will be age>=18 and value1 will be (incase of true) “Welcome” and value2 will be (incase of false) “You are too young”. So, total code will be

permission=(age>=18)? “Welcome”: “You are too young”;

If you feel any problem to understand this tutorial or find any typing mistake please post here to correct them.