
JavaScript Operators Overview
Learn about the key types of JavaScript operators - arithmetic, comparison, logical, assignment, unary, ternary, bitwise, type, and string operators. Understand their definitions, functions, and examples while exploring how they manipulate data and aid in decision-making within programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Presented by: Jasjeet Kaur (1520) Aaina (1556) Class: BCA Sem V
Introduction to Operators Introduction to Operators Definition : Operators in JavaScript are special symbols used to perform operations on operands (values or variables). They are fundamental to programming as they allow us to manipulate data and make decisions. JavaScript has a variety of operators, each serving a different purpose. Understanding these operators is crucial for writing effective and efficient code.
Types of Operators: Types of Operators: 1. Arithmetic Operators 2.Comparison Operators 3. Logical Operators 4.Assignment Operators 5. Unary Operators 6. Ternary Operators 7. Bitwise Operators 8. Type Operators 9. String Operators
Arithmetic Operators: Arithmetic Operators: Arithmetic operators are used to perform basic mathematical operations on numbers. They include: Addition (+): Addition (+): Adds two operands. Subtraction ( Subtraction (- -): ): Subtracts the second operand from the first. Multiplication (*): Multiplication (*): Multiplies two operands. Division (/): Division (/): Divides the first operand by the second. Modulus (%): Modulus (%): Returns the remainder of division. Increment (++): Increment (++): Increases the value of a variable by one. Decrement ( Decrement (-- --): ): Decreases the value of a variable by one.
Examples of Arithmetic Examples of Arithmetic Operators Operators Addition (+) : Subtraction (-) : Multiplication (*) : Division (/) : Modulus (%) : Increment (++) : Decrement (--) : let sum = 10 + 5; // 15 let difference = 10 - 5; // 5 let product = 10 * 5; // 50 let quotient = 10 / 5; // 2 let remainder = 10 % 3; // 1 let value = 10; value++; // value is now 11 let value = 10; value--; // value is back to 10
Comparison Operators Comparison Operators Comparison operators are used to compare two values. They return a Boolean result (true or false), which is often used in conditional statements. Equal to (==): Equal to (==): Checks if two values are equal after type coercion Strict Equal (===): Strict Equal (===): Checks if two values are equal and of the same type. Not Equal to (!=): Not Equal to (!=): Checks if two values are not equal after type coercion. Strict Not Equal (!==): Strict Not Equal (!==): Checks if two values are not equal or not of the same type. Greater Than (>): Greater Than (>): Checks if the first value is greater than the second. Less Than (<): Less Than (<): Checks if the first value is less than the second. Greater Than or Equal To (>=): Greater Than or Equal To (>=): Checks if the first value is greater than or equal to the second. Less Than or Equal To (<=): Less Than or Equal To (<=): Checks if the first value is less than or equal to the second.
Examples of Comparison Examples of Comparison operators operators Equal to (==) : Strict Equal (===) : Not Equal to (!=) : Strict Not Equal (!==) : Greater Than (>) : Less Than (<) : Greater Than or Equal To (>=): Less Than or Equal To (<=): console.log(10 == '10'); // true console.log(10 === '10'); // false console.log(10 != 5); // true console.log(10 !== '10'); // true console.log(10 > 5); // true console.log(5 < 10); // true console.log(10 >= 10); // true console.log(5 <= 10); // true
Logical Operators Logical Operators Used to perform logical operations and combine multiple conditions. AND (&&): Returns true if both operands are true. Example: (5 > 3) && (8 > 5) results in true. OR (||): Returns true if at least one operand is true. Example: (5 > 3) || (8 < 5) results in true. NOT (!): Returns true if the operand is false. Example: !(5 > 3) results in false.
1. Logical AND (&&): The AND operator returns true only if both operands are true. Operand 1 && Operand 2 Operand 1 Operand 2 true true true true false false false true false false false false 2. Logical OR (||) The OR operator returns true if at least one operand is true. Operand 1 Operand 2 Operand 1 || Op2 true true true true false true false true true false false false
3. Logical NOT (!) The NOT operator returns the opposite Boolean value of the operand. Operand !Operand true false false true summary of logical Operators: Logical AND (&&): Both conditions must be true for the result to be true. Logical OR (||): At least one condition must be true for the result to be true. Logical NOT (!):Inverts the Boolean value.
Assignment Operators Assignment Operators Assignment operators are used to assign values to variables. They can also perform arithmetic operations and assign the result to the variable. Simple Assignment (=): Simple Assignment (=): Assigns a value to a variable. Add and Assign (+=): Add and Assign (+=): Adds the right operand to the left operand and assigns the result to the left operand. Subtract and Assign ( Subtract and Assign (- -=): =): Subtracts the right operand from the left operand and assigns the result to the left operand. Multiply and Assign (*=): Multiply and Assign (*=): Multiplies the left operand by the right operand and assigns the result to the left operand. Divide and Assign (/=): Divide and Assign (/=): Divides the left operand by the right operand and assigns the result to the left operand. Modulus and Assign (%=): Modulus and Assign (%=): Applies the modulus operator and assigns the result to the left operand.
Examples of Assignment Examples of Assignment Operators: Operators: Simple Assignment (=) : Add and Assign (+=) : Subtract and Assign (-=) : Multiply and Assign (*=) : Divide and Assign (/=) : Modulus and Assign (%=) : let a = 10; // a is 10 let a = 10; a += 5; // a is now 15 let a = 10; a -= 3; // a is now 7 let a = 4; a *= 2; // a is now 8 let a = 8; a /= 2; // a is now 4 let a = 9; a %= 4; // a is now 1
Unary Operators Unary Operators Operate on a single operand. Unary Plus (+) Unary Plus (+): Converts the operand to a number. Unary Negation ( Unary Negation (- -) ): Negates the value of the operand. Logical NOT (!) Logical NOT (!): Inverts the Boolean value of the operand. Delete (delete) Delete (delete): Removes a property from an object. Typeof (typeof) Typeof (typeof): Returns the type of the operand. Instanceof (instanceof) Instanceof (instanceof): Checks if an object is an instance of a specific class.
Example of Unary Example of Unary Operators Operators Unary Plus (+): Unary Negation (-): Logical NOT (!): Typeof (typeof): Instanceof (instanceof): let str = '10'; let num = +str; // num is 10 (number) let num = 10; let neg = -num; // neg is -10 let isTrue = true; let isFalse = !isTrue; // isFalse is false let value = 42; let type = typeof value; // type is "number" let obj = {}; let isObject = obj instanceof Object; // isObject is true
Type Operators Type Operators Type operators are used to determine the type of a variable or check its instance. typeof: Returns a string indicating the type of the operand. For example, typeof 'hello' results in 'string'. instanceof: Tests if an object is an instance of a specific class or constructor function. For example, 'hello' instanceof String results in false (because 'hello' is a string primitive, not a String object).
Ternary Operators Ternary Operators The ternary operator is a shorthand for the if-else statement. It takes three operands and is used to evaluate a condition and return one of two values. Syntax: condition ? expr1 : expr2 Example: let result = (age >= 18) ? 'Adult' : 'Minor'; Here, if age is 18 or more, result will be 'Adult'; otherwise, it will be 'Minor'.
Bitwise Operators Bitwise Operators Perform operations on binary representations of integers. AND (&) AND (&): Performs a bitwise AND. OR (|) OR (|): Performs a bitwise OR. XOR (^) XOR (^): Performs a bitwise XOR. Complement (~) Complement (~): Inverts the bits. Left Shift (<<) Left Shift (<<): Shifts bits to the left. Right Shift (>>) Right Shift (>>): Shifts bits to the right. Unsigned Right Shift (>>>) Unsigned Right Shift (>>>): Shifts bits to the right without preserving the sign.
Example of Bitwise Example of Bitwise Operators Operators AND (&) AND (&): let a = 6; // 0110 in binary let b = 3; // 0011 in binary let result = a & b; // 0010 in binary (2 in decimal) OR (|) OR (|): let a = 6; // 0110 in binary let b = 3; // 0011 in binary let result = a | b; // 0111 in binary (7 in decimal) XOR (^) XOR (^): let a = 12; // 1100 in binary let b = 5; // 0101 in binary let result = a ^ b; // 1001 in binary (9 in decimal)
Complement (~) Complement (~): let a = 10; // 0000 1010 in binary let result = ~a; // 1111 0101 in binary (inverted bits, -11 in decimal) Left Shift (<<): Left Shift (<<): let a = 3; // 0000 0011 in binary let result = a << 2; // 0000 1100 in binary (12 in decimal) Right Shift (>>) Right Shift (>>): let a = 12; // 0000 1100 in binary let result = a >> 2; // 0000 0011 in binary (3 in decimal) Unsigned Right Shift (>>>) Unsigned Right Shift (>>>): let a = -12; // 1111 0100 in binary (signed 32-bit integer) let result = a >>> 2; // 0011 1101 in binary (1073741819 in decimal)
String Operators String Operators 1.Concatenation (+ operator) : 1.Concatenation (+ operator) : Used to combine two or more strings. Example: let str1 = "Hello"; let str2 = "World"; let result = str1 + " " + str2; OUTPUT: "Hello World"
2.Template Literals (`): 2.Template Literals (`): Provides a more powerful way to handle string interpolation and multi- line strings. Example: let name = "Alice"; let greeting = `Hello, ${name}!`; Output "Hello, Alice!"
Operators Precedence: Operators Precedence: Parentheses (()): Parentheses (()): Used to group expressions and override default precedence. Example: Example: (2 + 3) * 4 evaluates to 20. 2) 2) Member Access (. and []): Member Access (. and []): Used to access properties or methods of an object and array elements. Example: Example: obj.property, arr[0] 3) 3) Unary Operators (++, Unary Operators (++, -- --, +, , +, - -, ~, !): , ~, !): Apply operations to a single operand. Includes increment, decrement, unary plus, unary negation, bitwise NOT, and logical NOT. Example: Example: -a, !true, ++x 4) 4) Exponentiation (**): Exponentiation (**): Performs exponentiation (raising a number to a power). Example: Example: 2 ** 3 evaluates to 8. 1) 1)
5) 5) Multiplication, Division, Modulus (*, /, %): Multiplication, Division, Modulus (*, /, %): Perform arithmetic operations: multiplication, division, and modulus. Example: Example: 6 / 2 * 3 evaluates to 9. 6) 6) Addition and Subtraction (+, Addition and Subtraction (+, - -): ): Perform arithmetic addition and subtraction. Example: Example: 5 + 3 - 2 evaluates to 6. 7) 7) Bitwise Shift Operators (<<, >>, >>>): Bitwise Shift Operators (<<, >>, >>>): Perform bitwise shifts: left shift, right shift, and unsigned right shift. Example: Example: 4 << 1 evaluates to 8. 8) 8) Relational Operators (<, <=, >, >=, instanceof): Relational Operators (<, <=, >, >=, instanceof): Compare values and types: less than, less than or equal to, greater than, greater than or equal to, and instance of. Example: Example: 5 < 10 evaluates to true. .
9) 9) Equality Operators (==, !=, ===, !==): Equality Operators (==, !=, ===, !==): Check equality and inequality with type conversion (==, !=) and without type conversion (===, !==). Example: Example: 5 == '5' evaluates to true, 5 === '5' evaluates to false. 10) 10) Bitwise AND (&): Bitwise AND (&): Perform a bitwise AND operation. Example: Example: 5 & 3 evaluates to 1. 11) 11) Bitwise XOR (^): Bitwise XOR (^): Perform a bitwise XOR (exclusive OR) operation. Example: Example: 5 ^ 3 evaluates to 6. 12) 12) Bitwise OR (|): Bitwise OR (|): Perform a bitwise OR operation. Example: Example: 5 | 3 evaluates to 7. 13) 13) Logical AND (&&): Logical AND (&&): Perform a logical AND operation. Evaluates the second operand only if the first is true. Example: Example: true && false evaluates to false.
Operators Precedence: Operators Precedence: Precedence Level Description Operators 1. Parentheses: Override default precedence. () 2. . [] Member Access: Access properties/methods or array elements. 3. ++ -- + - ~ ! Unary Operators: Unary operations (increment, decrement, unary plus/negation, bitwise NOT). 4. ** Exponentiation: Raises a number to the power of another. 5. * / % Multiplication, Division, Modulus: Arithmetic operations. 6. Addition, Subtraction: Arithmetic operations. + - 7. << >> >>> Bitwise Shifts: Left shift, right shift, unsigned right shift. 8. < <= > >= Relational Operators: Comparisons and type checking. 9 == != === !== Equality Operators: Equality and inequality checks with and without type conversion.
10. & Bitwise AND: Bitwise conjunction. 11. Bitwise XOR: Bitwise exclusive OR. ^ 12. Logical AND: Logical conjunction. && 13. Conditional (Ternary): Conditional expressions, shorthand for if-else. ?: 14. Comma Operator: Evaluates multiple expressions from left to right, returns the last expression s result. , Summary: Summary: Highest Precedence: Highest Precedence: Parentheses, member access, unary operators. Middle Precedence: Middle Precedence: Arithmetic operations (multiplication, division, modulus), bitwise shifts. Lower Precedence: Lower Precedence: Relational and equality operators, bitwise operations. Lowest Precedence: Lowest Precedence: Logical operations, conditional operator, assignment operators.