Javascript Module 7
Javascript Module 7
Operators in Javascript
JavaScript supports various operators, including arithmetic, comparison, logical, assignment, bitwise,
ternary , increment and decrement and more. Here's an overview of some common operators with
sample code:
Arithmetic Operators:
Arithmetic operators perform mathematical calculations ,
• Addition (+): Adds two operands.
Code :
let result = 10 + 5; // result is 15
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
• Strict Equal (===): Checks if two values are equal and of the same type.
Code :
console.log(5 === '5'); // false
• Strict Not Equal (!==): Checks if two values are not equal and of the same type.
Code :
console.log(5 !== '5'); // true
• Greater Than (>): Checks if the left operand is greater than the right.
Code :
console.log(10 > 5); // true
• Less Than (<): Checks if the left operand is less than the right.
Code :
console.log(5 < 10); // true
• Greater Than or Equal To (>=): Checks if the left operand is greater than or equal to the right.
Code :
console.log(10 >= 10); // true
• Less Than or Equal To (<=): Checks if the left operand is less than or equal to the right.
console.log(5 <= 10); // true
Logical Operators:
Logical operators are used to combine or invert boolean values.
• Logical AND (&&): Returns true if both operands are true.
Code :
console.log(true && false); // false
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Assignment Operators:
Assignment operators are used to assign values to variables.
• Assignment (=): Assigns the value of the right operand to the left operand.
Code :
let x = 5;
• Addition Assignment (+=): Adds the value of the right operand to the variable and assigns the
result to the variable.
Code :
let x = 5;
x += 3; // x is now 8
• Subtraction Assignment (-=): Subtracts the value of the right operand from the variable and
assigns the result to the variable.
Code :
let x = 5;
x -= 3; // x is now 2
• Multiplication Assignment (*=): Multiplies the variable by the value of the right operand and
assigns the result to the variable.
Code :
let x = 5;
x *= 3; // x is now 15
• Division Assignment (/=): Divides the variable by the value of the right operand and assigns the
result to the variable.
Code :
let x = 15;
x /= 3; // x is now 5
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Bitwise Operators:
Bitwise operators perform operations on the binary representations of numbers.
• Bitwise AND (&): Performs a bitwise AND operation.
Code :
let result = 5 & 3; // result is 1
• Left Shift (<<): Shifts the bits of the first operand to the left by the number of positions specified
by the second operand.
Code :
• Sign-propagating Right Shift (>>): Shifts the bits of the first operand to the right by the number
of positions specified by the second operand. The sign bit is used to fill the trailing positions.
Code:
let result = -10 >> 1; // result is -5
• Zero-fill Right Shift (>>>): Shifts the bits of the first operand to the right by the number of
positions specified by the second operand. Zeros are used to fill the trailing positions.
Code :
let result = -10 >>> 1; // result is 2147483643
Other Operators:
There are also other miscellaneous operators in JavaScript.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
• Conditional (Ternary) Operator (? :): Evaluates a condition and returns one of two possible
values based on the result of the condition.
Code :
let result = (5 > 3) ? 'Yes' : 'No'; // result is 'Yes'
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
• Implicit Type Conversion: JavaScript performs implicit type conversion (also known as type
coercion) when operands of different types are used with operators.
Code :
let num = 5;
let str = "10";
let result = num + str; // result is "510" (string concatenation)
• Unary Increment (++) and Decrement (--): Increases or decreases the value of a variable by 1
respectively. These can be used as prefix or postfix operators, which affects when the
increment/decrement occurs.
Code :
let x = 5;
x++; // x is now 6
--x; // x is now 5
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]