JavaScript – Operators

What is an Operator?

Give us a chance to take a straightforward articulation 4 + 5 is equivalent to 9. Here 4 and 5 are called operands and ‘+’ is known as the operator. JavaScript underpins the accompanying kinds of operators.

  • Number juggling Operators
  • Examination Operators
  • Legitimate (or Relational) Operators
  • Task Operators
  • Restrictive (or ternary) Operators

Let’s view all operators individually.

Arithmetic Operators

JavaScript underpins the accompanying math operators −

Accept variable A holds 10 and variable B holds 20, at that point −

Sr. No.Operator & Description
1.+(Addition)
Includes two operands
Ex. A+B will give us 30
2.(Subtraction)
Subtracts the second operand from the first
Ex. A-B will give us -10
3.* (Multiplication)
Duplicate the two operands
Ex: A * B will give 200
4. /(Division)
Partition the numerator by the denominator
Ex: B/A will give 2
5. % (Modulus)
Yields the rest of a whole number division
Ex: B % A will give 0
6. ++ (Increment)
Builds a whole number an incentive by one
Ex: A++ will give 11
7. (Decrement)
Diminishes a whole number an incentive by one
Ex: A- – will give 9

Note − Addition operator (+) works for Numeric just as Strings. for example “a” + 10 will give “a10”.

Example

The accompanying code tells the best way to utilize number-crunching operators in JavaScript.

<html>
   <body>
   
      <script type = "text/javascript">
         <!--
            var a = 33;
            var b = 10;
            var c = "Test";
            var linebreak = "<br />";
         
            document.write("a + b = ");
            result = a + b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a - b = ");
            result = a - b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a / b = ");
            result = a / b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a % b = ");
            result = a % b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a + b + c = ");
            result = a + b + c;
            document.write(result);
            document.write(linebreak);
         
            a = ++a;
            document.write("++a = ");
            result = ++a;
            document.write(result);
            document.write(linebreak);
         
            b = --b;
            document.write("--b = ");
            result = --b;
            document.write(result);
            document.write(linebreak);
         //-->
      </script>
      
      Set the variables to different values and then try...
   </body>
</html>

Output:

a + b = 43
a - b = 23
a / b = 3.3
a % b = 3
a + b + c = 43Test
++a = 35
--b = 8
Set the variables to different values and then try...

Comparison Operators

JavaScript underpins the accompanying correlation operators −

Expect variable A holds 10 and variable B holds 20, at that point −

Sr.No. Operator and Description
1. = = (Equal)
Checks on the off chance that the estimation of two operands are equivalent or not, in the event that indeed, at that point the condition turns out to be valid.
Ex: (A == B) isn’t valid.
2. != (Not Equal)
Checks if the estimation of two operands are equivalent or not, on the off chance that the qualities are not approached, at that point the condition turns out to be valid.
Ex: (A != B) is valid.
3.>(Greater than)
Checks if the estimation of the left operand is more noteworthy than the estimation of the correct operand, on the off chance that truly, at that point the condition turns out to be valid.
Ex: (A > B) isn’t valid.
4.< (Less than)
Checks if the estimation of the left operand is not exactly the estimation of the correct operand, on the off chance that indeed, at that point the condition turns out to be valid.
Ex: (A < B) is valid.
5.= (Greater than or Equal to)
Checks if the estimation of the left operand is more prominent than or equivalent to the estimation of the correct operand, in the event that truly, at that point the condition turns out to be valid.
Ex: (A >= B) isn’t valid.
6.<= (Less than or Equal to)
Checks if the estimation of the left operand is not exactly or equivalent to the estimation of the correct operand, on the off chance that truly, at that point the condition turns out to be valid.
Ex: (A <= B) is valid.

Example

The accompanying code tells the best way to utilize examination operators in JavaScript.

<html>
   <body>  
      <script type = "text/javascript">
         <!--
            var a = 10;
            var b = 20;
            var linebreak = "<br />";
      
            document.write("(a == b) => ");
            result = (a == b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a < b) => ");
            result = (a < b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a > b) => ");
            result = (a > b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a != b) => ");
            result = (a != b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a >= b) => ");
            result = (a >= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a <= b) => ");
            result = (a <= b);
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      Set the variables to different values and different operators and then try...
   </body>
</html>

Output:

(a == b) => false 
(a < b) => true 
(a > b) => false 
(a != b) => true 
(a >= b) => false 
a <= b) => true
Set the variables to different values and different operators and then try...

Logical Operators

JavaScript bolsters the accompanying coherent operators −

Expect variable A holds 10 and variable B holds 20, at that point −

Sr.No. Operator and Description
1 && (Logical AND)
In the event that both the operands are non-zero, at that point, the condition turns out to be valid.
Ex: (A && B) is valid.
2|| (Logical OR)
If any of the two operands are non-zero, then the condition becomes true.
Ex: (A || B) is true.
3.! (Logical NOT)
Switches the consistent condition of its operand. On the off chance that a condition is valid, at that point the Logical NOT operator will make it false.
Ex: ! (A && B) is false.

Example:

Attempt the accompanying code to figure out how to execute Logical Operators in JavaScript.

<html> 
  <body>   
      <script type = "text/javascript">
         <!--
            var a = true;
            var b = false;
            var linebreak = "<br />";
      
            document.write("(a && b) => ");
            result = (a && b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a || b) => ");
            result = (a || b);
            document.write(result);
            document.write(linebreak);
         
            document.write("!(a && b) => ");
            result = (!(a && b));
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

Output:

(a && b) => false 
(a || b) => true 
!(a && b) => true
Set the variables to different values and different operators and then try...

Bitwise Operators

JavaScript underpins the accompanying bitwise operators −

Accept variable A holds 2 and variable B holds 3, at that point −

Sr.No. Operator and Description
1. &(Bitwise AND)
It plays out a Boolean AND activity on each piece of its number contentions.
Ex: (An and B) is 2.
2. ^ (BitWise OR)
It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both.
Ex: (A ^ B) is 1.
3. ^ (Bitwise XOR)
It plays out a Boolean select OR activity on each piece of its number contentions. Restrictive OR implies that either operand one is valid or operand two is valid, however not both.
Ex: (A ^ B) is 1.
4. ~ (Bitwise Not)
It is an unary operator and works by turning around every one of the bits in the operand.
Ex: (~B) is – 4
5. << (Left Shift)
It moves every one of the bits in its first operand to one side by the number of spots determined in the subsequent operand. New bits are loaded up with zeros. Moving a worth left by one position is proportional to duplicating it by 2, moving two positions is proportionate to increasing by 4, etc.
Ex: (A << 1) is 4.
6.>> (Right Shift)
Double Right Shift Operator. The left operand’s worth is moved right by the number of bits indicated by the correct operand.
Ex: (A >> 1) is 1.
7. (Right move with Zero)
This operator is much the same as the >> operator, then again, actually the bits moved in on the left are constantly zero.
Ex: (A >>> 1) will be 1.

Example:

Attempt the accompanying code to execute Bitwise operator in JavaScript.

html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 2; // Bit presentation 10
            var b = 3; // Bit presentation 11
            var linebreak = "<br />";
         
            document.write("(a & b) => ");
            result = (a & b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a | b) => ");
            result = (a | b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a ^ b) => ");
            result = (a ^ b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(~b) => ");
            result = (~b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a << b) => ");
            result = (a << b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a >> b) => ");
            result = (a >> b);
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

Output:

(a & b) => 2 
(a | b) => 3 
(a ^ b) => 1 
(~b) => -4 
(a << b) => 16 
(a >> b) => 0
Set the variables to different values and different operators and then try...

Miscellaneous Operator

We will examine two operators here that are very helpful in JavaScript: the conditional operator (? 🙂 and the typeof operator.

Conditional Operator (? 🙂

The restrictive operator initially assesses an articulation for a genuine or false worth and afterward executes one of the two given proclamations relying on the aftereffect of the assessment.

Sr.No. Operator and Description
1. ? : (Conditional )
In the event that Condition is valid? At that point esteem X : Otherwise worth Y

Attempt the accompanying code to see how the Conditional Operator functions in JavaScript.

Example:

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 10;
            var b = 20;
            var linebreak = "<br />";
         
            document.write ("((a > b) ? 100 : 200) => ");
            result = (a > b) ? 100 : 200;
            document.write(result);
            document.write(linebreak);
         
            document.write ("((a < b) ? 100 : 200) => ");
            result = (a < b) ? 100 : 200;
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

Output:

((a > b) ? 100 : 200) => 200 
((a < b) ? 100 : 200) => 100
Set the variables to different values and different operators and then try...

typeof Operator

The typeof operator is an unary operator that is put before its single operand, which can be of any kind. Its worth is a string showing the information kind of the operand.

The typeof operator assesses to “number”, “string”, or “boolean” if its operand is a number, string, or boolean worth and returns genuine or false dependent on the assessment.

Here is a rundown of the arrival esteems for the typeof Operator.

Type String Returned by typeof
Number “Number”
String “string”
Boolean “boolean”
Object “object”
Function “Function”
Undefined “undefined”
Null “object”

Example:

The accompanying code tells the best way to execute typeof operator.

<html>
   <body>      
      <script type = "text/javascript">
         <!--
            var a = 10;
            var b = "String";
            var linebreak = "<br />";
         
            result = (typeof b == "string" ? "B is String" : "B is Numeric");
            document.write("Result => ");
            document.write(result);
            document.write(linebreak);
         
            result = (typeof a == "string" ? "A is String" : "A is Numeric");
            document.write("Result => ");
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

Output:

Result => B is String 
Result => A is Numeric
Set the variables to different values and different operators and then try...

Leave a Comment

error: Alert: Content is protected!!