java-script:bitwise-or-bitwise-and-ternary-optional-chaining-operators
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java-script:bitwise-or-bitwise-and-ternary-optional-chaining-operators [2023/08/10 04:26] – odefta | java-script:bitwise-or-bitwise-and-ternary-optional-chaining-operators [2023/08/11 13:19] (current) – odefta | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Java Script | Bitwise OR, & Bitwise AND, ? Ternary Conditional Operator and ?. Optional Chaining Operator ====== | + | ====== Java Script | Bitwise OR, ^ Bitwise XOR, & Bitwise AND, ? Ternary Conditional Operator and ?. Optional Chaining Operator ====== |
In JavaScript, there are the **| (bitwise OR)** and **& (bitwise AND)** operators, which are different from **||** and **&& | In JavaScript, there are the **| (bitwise OR)** and **& (bitwise AND)** operators, which are different from **||** and **&& | ||
- | ===== | (Bitwise OR) ===== | + | ===== | Bitwise OR ===== |
The **|** operator returns a value where each bit is set if the corresponding bit is set in **either** of the two operands: | The **|** operator returns a value where each bit is set if the corresponding bit is set in **either** of the two operands: | ||
Line 12: | Line 12: | ||
</ | </ | ||
- | ===== & (Bitwise AND) ===== | + | ===== ^ Bitwise XOR ===== |
+ | |||
+ | The ^ XOR (exclusive or) operation returns true if exactly one of the bits being compared is true: | ||
+ | <code javascript> | ||
+ | let a = 5; // binary: 0101 | ||
+ | let b = 3; // binary: 0011 | ||
+ | |||
+ | let result = a ^ b; // binary: 0110, decimal: 6 | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== & Bitwise AND ===== | ||
The **&** operator returns a value where each bit is set if the corresponding bit is set in **both** operands: | The **&** operator returns a value where each bit is set if the corresponding bit is set in **both** operands: | ||
Line 29: | Line 40: | ||
</ | </ | ||
- | ===== ? (Ternary Conditional Operator) ===== | + | ===== ? Ternary Conditional Operator ===== |
The ternary conditional operator is a concise way to perform a conditional (if-else-like) operation. \\ | The ternary conditional operator is a concise way to perform a conditional (if-else-like) operation. \\ | ||
Line 44: | Line 55: | ||
</ | </ | ||
- | ===== ?. (Optional Chaining Operator) ===== | + | ===== ?. Optional Chaining Operator ===== |
Introduced in ECMAScript 2020, the optional chaining operator **?.** allows you to access properties of objects without having to check if the object or property exists. If the object or property doesn' | Introduced in ECMAScript 2020, the optional chaining operator **?.** allows you to access properties of objects without having to check if the object or property exists. If the object or property doesn' |
java-script/bitwise-or-bitwise-and-ternary-optional-chaining-operators.1691630813.txt.gz · Last modified: 2023/08/10 04:26 by odefta