Operators in JS
21 October 2024 11:36
Operator Detailed Description (with Important Points) Code Example Important Points for Exam
Type
Arithmetic - Used for basic arithmetic operations like addition, javascript let x = 10, y = 5; console.log(x + y); // 15 - Perform basic mathematical
Operators subtraction, multiplication, division, and modulus. console.log(x - y); // 5 console.log(x * y); // 50 operations. - % returns
- Includes: +, -, *, /, %, ++ (increment), -- (decrement). console.log(x / y); // 2 console.log(x % y); // 0 x++; remainder. - ++ and --
console.log(x); // 11 y--; console.log(y); // 4 increment/decrement values.
Assignment - Assign values to variables or update existing values. - let x = 10; - Assigns or updates values. -
Operators Includes: =, +=, -=, *=, /=, %= (shorthand for arithmetic x += 5; // x = x + 5 Shorthand available for
operations). console.log(x); // 15 arithmetic operations like +=, -=,
x -= 2; etc.
console.log(x); // 13
Comparison - Compare two values and return a boolean (true or let x = 10, y = "10"; - == compares values, ===
Operators false). - Includes: == (loose equality), === (strict console.log(x == y); // true compares both values and
equality), !=, !==, >, <, >=, <=. console.log(x === y); // false types. - Be familiar with != and !
== --> performs comparison after type console.log(x != y); // false ==.
conversion(loose equality). console.log(x !== y); // true
=== --> performs comparison without type console.log(x > 5); // true
conversion(coercien) console.log(x <= 10); // true
== coercien rules:
• Strings convert other operands to strings.
• Numbers convert other operands to numbers.
• Booleans convert to numbers (true → 1, false → 0).
Logical - Used to combine boolean expressions or invert them. (OR),!` (NOT).
Operators - Includes: && (AND), `
Bitwise - Work on binary (bit) representations of numbers. (OR),^(XOR),~(NOT), ```javascript let x = 5; // 101 in
Operators - Includes: & (AND), ` <<(left shift),>>` (right shift). binary let y = 3; // 011 in binary
a>>b --> equivalent to dividing a by 2^b. console.log(x & y); // 1
a<<b --> equivalent to multiplying a a by 2^b console.log(x)
Ternary - A shorthand for if-else. let age = 18; - Condensed form of if-else
Operator - Syntax: condition ? expressionIfTrue : let canVote = age >= 18 ? "Yes" : "No"; statement. - Frequently used for
expressionIfFalse. console.log(canVote); // Yes concise conditional logic.
Type - Used to check or determine the type of a variable. javascript let x = "Hello"; console.log(typeof x); // - typeof is for checking types. -
Operators - Includes: typeof (returns data type), instanceof string let date = new Date(); console.log(date instanceof checks inheritance.
(checks if an object is an instance of a specific class). instanceof Date); // true
String - Used to manipulate or concatenate strings. javascript let firstName = "John"; - String concatenation uses + and
Operators - The + operator is used for string concatenation. let lastName = "Doe"; +=. - Common in string
console.log(firstName + " " + lastName); // John Doe manipulation tasks.
let greeting = "Hello";
greeting += ", World!";
console.log(greeting); // Hello, World!
Unary - Operate on a single operand. javascript let x = 10; console.log(+x); // 10 console.log(- - Used in loops (++, --) and for
Operators - Includes: + (positive), - (negative), ++ (increment), -- x); // -10 x++; console.log(x); // 11 let y = true; negating values. - typeof gives
(decrement), ! (logical NOT), typeof. console.log(!y); // false the type of the operand.
Relational - Determine relationships between values. let person = { name: "Alice", age: 25 }; - in checks if a property exists in
Operators - Includes in (checks if a property exists in an object), console.log("name" in person); // true console.log(arr an object. - instanceof checks
instanceof (checks if an object is an instance of a instanceof Array); // true object type inheritance.
constructor).
Logical - Returns the right-hand operand if the left-hand let foo = null; - Used to handle null or
Nullish (??) operand is null or undefined, otherwise returns the left- let bar = foo ?? "default value"; undefined values. - Alternative to
hand operand. console.log(bar); // "default value" `
Optional - Safely access deeply nested object properties without let person = { name: "John", address: { city: "New - Useful for accessing nested
Chaining (?.) causing runtime errors. York" } }; console.log(person?.address?.city); // "New object properties. - Helps avoid
- Prevents error if a property is null or undefined. York" errors when accessing non-
console.log(person?.contact?.phone); // undefined (no existent properties.
error)
FrontEnd Page 1