Friday, February 14, 2020

Operators In JavaScript

Operators In JavaScript


An Operator is a symbolic character that is used in programming to perform predefined operations, These operations can be like arithmetic, logical, boolean, bitwise, etc. Operators are applied on operands and supply some results on the basis of some predefined functions for thas operator.


Types of Operators in JavaScript

  • Arithmetic Operator
  • Assignment Operator
  • String Operator
  • Logical Operator
  • Bitwise Operators

Arithmetic Operators

Arithmetic operators are used to performing arithmetic operations.


Arithmetic Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement

We can solve arithmetic expressions with these operators like,

    var a=12;
    var b=23;
    var c=a+b;
    dicument.write('Sum = '+c);

Assignment Operators


Assignment Operators
Operator Operation equivalent to
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
**= x **= y x = x ** y


String Operators

( + )operators are used to concatenating two strings, for example

    var fname="james";
    var lname="Bond";
    var name=fname+' ' +lname;

(+) is also known as concatenation operator, ( + )operators can also be used with  assignment operator like,

    var q1 = "Have a ";
    q1 += "great day";
    document.write(q1);
/*
Output:
Have a great day
*/

We can also concatenate string and number together with + operator.

    var s=' The number is ';
    var num=5;
    document.write(s+num);
/*
Output:
The number is 5
*/


Comparison Operator


Comparison Operator
Operator Description
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator

Comparison operators are used to comparing values, the outcome is boolean always. For example


    var a=10;
    var b=5;
    if(a>b)
    {
      document.write('a is greater than b');
    }
    else{

      document.write('b is greater than a');
    }
/*
Output:
a is greater than b
*/



Logical Operators

Logical operators are used to performing logical operations like and, or, not, etc.


Logical Operators
Operator Description
&& logical and
|| logical or
! logical not
      

    var a=10;
    var b=6;
    if(a>5 && b>5)
    {
      document.write('a and b are greater than 5');
    }
    else{
      document.write('a and b are less than 5');
    }
/*
Output:
a and b are greater than 5
*/



Bitwise Operators

Bitwise operators are used to performing bitwise operations on the 32-bit representation of numerical operands.

Bitwise Operators
Operator Meaning Example Same as Result Decimal
& AND 12 & 11 1100 & 1011 0100  8
| OR 5 | 1 0101 | 0001 0101  5
~ NOT ~ 5  ~0101 1010  10
^ XOR 5 ^ 1 0101 ^ 0001 0100  4
<< Zero fill left shift 5 << 1 0101 << 1 1010  10
>> Signed right shift 5 >> 1 0101 >> 1 0010   2
>>> Zero fill right shift 5 >>> 1 0101 >>> 1 0010   2
  

    document.write(12&11);
    document.write('<br>');
    document.write(12|11);
    document.write('<br>');
    document.write(!1);
/*
Output:
8
15
false

*/

 Example