Javascript Tutorial – Operators in Javascript

Operators in Javascript

What is Operators

Operators are used to perform Operations. Now, we get a question, what is Operation. So, here are few example of Operations

  1. Add 2 Numbers
  2. Subtract 2 Numbers
  3. Compare 2 Numbers
  4. Assign a Value to a Variable

So, if you want to perform all these operation, you will need operators for that, and the Numbers or Variables, on which, you will perform, these operations, are called Operands. So, we have 3 terms with us.

  1. Operators
  2. Operands
  3. Operation

There are some predefined symbols for Operators that are being used, and we will discuss about those symbols in this session. Now, because Operotors use symbols, So If we want to define Oerators, then we can define in this way

Operators are Symbol, which are used to perform some operation on the operands.

Types of Operators in Javascript

We are going to discuss different types of operators in this session. We can categorize it in 2 Categories

  1. Type of Operators based on the Operations they perform
  2. Type of Operators based on the number of the operands they require

Based on the number of Operands required

  1. Binary Operators – Binary Operators require 2 Operands
  2. Unary Operators – Unary Operators require only 1 Operand
  3. Turnary Operators – Turnary Operators require 3 Operands
  • Binary Operators can perform operation on variables as well as Constant.
  • Unary Operators can perform operation only on variables.

Based on the Operations they perform

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators

Arithmetic Operators

Arithmetic Operators are used to perform arithmetic operation on operands.
Let’s say we have 2 variables a and b, and we want to perform arithmetic operations on them.
Let’s say that the value of a and b is

let a = 10;
let b = 4;

OperatorsOperationNumber of OperandSyntaxType
+Addition (Add 2 Numbers)2a+bBinary
Subtraction (Subtract 2 Numbers)2a-bBinary
*Multiplication (Multily 2 Numbers)2a*bBinary
/Division (Divide 2 Numbers)2a/bBinary
%Modulus (Remainder)2a/bBinary
++Increment (Increase value by 1)1a++Unary
Decrement (Decrease value by 1)1a++Unary

Assignment Operators

Assignment Operators are used to perform assigment operations on the operands. We need atleast 1 Variable for peforming Assignment Operations. Let’s say we have 2 Variables a and b, and the values that they hold is.

let a = 10;
let b = 4;

OperatorsSyntaxOperationNumber of OperandType
=a=bAssigns value of left variable (b) to right variable(a)2Binary
+=a+=ba=a+b (Add a and b and assign the result to a)2Binary
-=a-=ba=a-b (Subtract b from a and assign the result to a)2Binary
*=a*=ba=a*b (Multiply a and b and assign the result to a)2Binary
/=a/=ba=a/b (Divide b from a and assign the result to a)2Binary
%=a%=ba=a%b (Remainder of a/b is assigned to a)2Binary

Comparison Operators

Comparison Operators compares 2 values and then returns either a true value or a false value. Comparison Operators return boolean values.
let’s say we have a variable a with a value of 7

let a = 7;

OperatorSyntaxOperationResultType
==a==7Compares a with 7 for equal valuetrueBinary
a==”7″Compares a with “7” for equal valuetrueBinary
a==8Compares a with 8 for equal valuefalseBinary
===a===7Compares a with 7 for equal value and equal typetrueBinary
a===”7″Compares a with “7” for equal value and equal typefalseBinary
a===8Compares a with 8 for equal value and equal typefalseBinary
!=a!=7Compares a with 7 for not equal valuefalseBinary
a!=”7″Compares a with “7” for not equal valuefalseBinary
a!=8Compares a with 8 for not equal valuetrueBinary
!==a!==7Compares a with 7 for not equal value and equal typefalseBinary
a!==”7″Compares a with “7” for not equal value and equal typetrueBinary
a!==8Compares a with 8 for not equal value and equal typetrueBinary
>a>7Compare if a is greater than 7falseBinary
<a<7Compare if a is less than 7falseBinary
>=a>=Compare if a is greater than or equal to 7trueBinary
<=a<=Compare if a is less than or equal to 7trueBinary

Logical Operators

Logical Operators are the Operators that performs Logical Operation. Meaning of Logical Operation is Performing Operation on boolean values and returning a boolean value.

OperatorDescriptionSyntaxResultType
&&Logical AND(true && true)trueBinary
(true && false)falseBinary
(false && false)falseBinary
||Logical OR(true || true)trueBinary
(true || false)trueBinary
(false || false)falseBinary
!Logical NOT!truefalseUnary
!falsetrueUnary

Summary

We have covered below topics in this session

  1. What is Operators
  2. Types of Operators
  3. Unary Operator
  4. Binary Operator
  5. Arithmetic Operator
  6. Assignment Operator
  7. Comparison Operator
  8. Logical Operator

Leave a Comment