0% found this document useful (0 votes)
0 views

Javascript Module 7

Uploaded by

ANIME DRAWINGS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Javascript Module 7

Uploaded by

ANIME DRAWINGS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Software Training and Placement Center

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

• Subtraction (-): Subtracts the second operand from the first.


Code :
let result = 10 - 5; // result is 5

• Multiplication (*): Multiplies two operands.


Code :
let result = 10 * 5; // result is 50

• Division (/): Divides the first operand by the second.


Code :
let result = 10 / 5; // result is 2

• Modulus (%): Returns the remainder of the division.


Code :
let result = 10 % 3; // result is 1

• Increment (++): Increases the value of a variable by 1.


Code :
let x = 5;
x++; // x is now 6

• Decrement (--): Decreases the value of a variable by 1.


Code :
let y = 5;
y--; // y is now 4

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

Comparison Operators: Comparison operators are used to compare values.


• Equal (==): Checks if two values are equal.
Code :
console.log(5 == '5'); // true

• Not Equal (!=): Checks if two values are not equal.


Code :
console.log(5 != 10); // true

• 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

• Logical OR (||): Returns true if either operand is true.


Code :
console.log(true || false); // true

• Logical NOT (!): Returns the opposite boolean value.


Code :
console.log(!true); // false

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

• Bitwise OR (|): Performs a bitwise OR operation.


Code :
let result = 5 | 3; // result is 7

• Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation.


Code :
let result = 5 ^ 3; // result is 6

• Bitwise NOT (~): Performs a bitwise NOT (complement) operation.


Code :
let result = ~5; // result is -6

• Left Shift (<<): Shifts the bits of the first operand to the left by the number of positions specified
by the second operand.
Code :

let result = 5 << 2; // result is 20

• 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'

• typeof Operator: Returns a string indicating the type of the operand.


Code :
console.log(typeof 42); // 'number'

• instanceof Operator: Returns true if an object is an instance of a specified object type.


Code :
console.log([] instanceof Array); // true

• delete Operator: Deletes a property from an object.


Code :
let obj = { a: 1, b: 2 };
delete obj.a; // removes property 'a' from obj

• void Operator: Evaluates an expression and returns undefined.


Code :
void(0); // returns undefined

Type Conversion Operators:


JavaScript provides various ways to convert between different data types.
• String Conversion: Converts a value to a string.
Code :
let num = 42;
let str = String(num); // str is "42"

• Number Conversion: Converts a value to a number.


Code :
let str = "42";
let num = Number(str); // num is 42

• Boolean Conversion: Converts a value to a boolean.


Code :

let value = "Hello";

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

let bool = Boolean(value); // bool is true

• 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]

You might also like