Operators
An operator performs some operation on single or multiple operands (data value) and produces a result. For example 1 + 2, where + sign is an operator and 1 is left operand and 2 is right operand. + operator adds two numeric values and produces a result which is 3 in this case.
Syntax:
<Left operand> operator <right operand>
<Left operand> operator
Example 1: Arithmetic Operators
Arithmetic operators are used to perform mathematical operations between numeric operands.
Source code:
var x = 10, y = 10
console.log("Addition Operator",x + y)
console.log("Subtraction Operator",x - y)
console.log("Multiplication Operator",x * y)
console.log("Division Operator",x / y)
console.log("Modulus Operator",x % y)
console.log("Increment Operator",x++)
console.log("Decrement Operator",y--)
Output:
Addition Operator 20
Subtraction Operator 0
Multiplication Operator 100
Division Operator 1
Modulus Operator 0
Increment Operator 10
Decrement Operator 10
Example 2: Comparison Operators
Comparison operators are used to compare two operands and return Boolean value true or false.
Brief:
== Compares the equality of two operands without considering type.
=== Compares equality of two operands with type.
!= Compares inequality of two operands.
> Checks whether left side value is greater than right side value. If yes then returns true otherwise false.
< Checks whether left operand is less than right operand. If yes then returns true otherwise false.
>= Checks whether left operand is greater than or equal to right operand. If yes then returns true otherwise false.
<= Checks whether left operand is less than or equal to right operand. If yes then returns true otherwise false.
Source code:
var x = 10, y = 10
console.log("equality Operator of two operands without considering type ::",x == y)
console.log("equality Operator of two operands with type ::",x === y)
console.log("inequality Operator ::",x != y)
console.log("greaterthan Operator ::",x > y)
console.log("lesserthan Operator ::",x < y)
console.log("greaterthan equal Operator ::",x >= y)
console.log("lesserthan equal Operator ::",x <= y)
Output:
equality Operator of two operands without considering type :: true
equality Operator of two operands with type :: true
inequality Operator :: false
greaterthan Operator :: false
lesserthan Operator :: false
greaterthan equal Operator :: true
lesserthan equal Operator :: true
Example 3: Logical Operators
Logical operators are used to combine two or more conditions.
Brief:
&& is known as AND operator. It checks whether two operands are non-zero (0, false, undefined, null or "" are considered as zero), if yes then returns 1 otherwise 0.
|| is known as OR operator. It checks whether any one of the two operands is non-zero (0, false, undefined, null or "" is considered as zero).
! is known as NOT operator. It reverses the boolean result of the operand (or condition)
Source code:
var x = 10, y = 10
console.log("AND Operator::",(x != y) && (x < y))
console.log("OR Operator::",(x != y) || (x < y))
console.log("NOT Operator::",(x!=y))
Output:
AND Operator:: false
OR Operator:: false
NOT Operator:: false
Example 4: Assignment Operators
Assignment operators are used to assign values to variables with less key strokes.
Brief:
= Assigns right operand value to left operand.
+= Sums up left and right operand values and assign the result to the left operand.
-= Subtract right operand value from left operand value and assign the result to the left operand.
*= Multiply left and right operand values and assign the result to the left operand.
/= Divide left operand value by right operand value and assign the result to the left operand.
%= Get the modulus of left operand divide by right operand and assign resulted modulus to the left operand.
Source code:
var x = 10, y = 10
console.log("= Operator::",x = y)
console.log("+= Operator::",x += 1)
console.log("-= Operator::",x -= 1)
console.log("*= Operator::",x *= 5)
console.log("/= Operator::",x /= 5)
console.log("%= Operator::",x %= 2)
Output:
= Operator:: 10
+= Operator:: 11
-= Operator:: 10
*= Operator:: 50
/= Operator:: 10
%= Operator:: 0
Example 5: Conditional Operators (or) ternary operator
Special operator called ternary operator (or) Conditional Operators :? That assigns a value to a variable based on some condition. This is like short form of if-else condition.
Syntax:
<condition> ? <value1> : <value2>;
Brief:
Conditional operator starts with conditional expression followed by ? operator. Second part ( after ? and before : operator) will be executed if condition turns out to be true. If condition becomes false then third part (after :) will be executed.
Source code:
var a = 10, b = 5;
var c = a > b? a : b;
var d = a > b? b : a;
console.log(c,d)
Output:
10 5
Thank you, Come Back to Learn More About Tech Stuff with Tech Bird
Have A Nice Day !
:)
Comments