|
| 1 | +# Expression and Operators |
| 2 | + |
| 3 | +An expression is a valid piece of code that resolves to a value. There are two types of expressions: those that have effects such as asssigning the value and those who evaluate value. |
| 4 | + |
| 5 | +For example: `x = 7` is a expression that assigns the value of 7 to the variable `x` usig the `=` operator while the expression `2+2` uses the operator `+` to add 2 and 2 together and returns a value of 4. |
| 6 | + |
| 7 | +## Types of Operator |
| 8 | + |
| 9 | +- Assignment Operator: It assigns a value to its left operand to the value at its right operand with the operator `=`. |
| 10 | +- Comparision Operator: It compares its operands and returns a logical value based on whether the comparision is `true` or `false`. If two operands are not of same type, JS attempts to convert them to appropriate type for comparision. |
| 11 | + |
| 12 | +| Operator | Description | |
| 13 | +| -------------------------- | --------------------------------------------------------------------------------------------------- | |
| 14 | +| Equal (==) | Returns true if the operands are equal. | |
| 15 | +| Not equal (!=) | Returns true if the operands are not equal. | |
| 16 | +| Strict equal (===) | Returns true if the operands are equal and of the same type. See also Object.is and sameness in JS. | |
| 17 | +| Strict not equal (!==) | Returns true if the operands are of the same type but not equal, or are of different type. | |
| 18 | +| Greater than (>) | Returns true if the left operand is greater than the right operand. | |
| 19 | +| Greater than or equal (>=) | Returns true if the left operand is greater than or equal to the right operand. | |
| 20 | +| Less than (<) | Returns true if the left operand is less than the right operand. | |
| 21 | +| Less than or equal (<=) | Returns true if the left operand is less than or equal to the right operand. | |
| 22 | + |
| 23 | +- Arithematic Operator: It takes numerical values as operands and returns single numerical value.The standard arithematic operations are addition `(+)`, subtraction `(-)`, multiplication `(*)` and division `(/)`. |
| 24 | + |
| 25 | +```js |
| 26 | +20 / 2; //10 |
| 27 | +2 + 2; //4 |
| 28 | +10 * 2; //20 |
| 29 | +``` |
| 30 | + |
| 31 | +In addition to the standard arithematic operations `(+,-,*,/)`, JS also provides other arithematic operators. |
| 32 | + |
| 33 | +| Operator | Description | |
| 34 | +| -------------------------- | ------------------------------------------------------------------------------------------ | |
| 35 | +| Equal (==) | Returns true if the operands are equal. | |
| 36 | +| Not equal (!=) | Returns true if the operands are not equal. | |
| 37 | +| Strict equal (===) | Returns true if the operands are equal and of the same type. | |
| 38 | +| Strict not equal (!==) | Returns true if the operands are of the same type but not equal, or are of different type. | |
| 39 | +| Greater than (>) | Returns true if the left operand is greater than the right operand. | |
| 40 | +| Greater than or equal (>=) | Returns true if the left operand is greater than or equal to the right operand. | |
| 41 | +| Less than (<) | Returns true if the left operand is less than the right operand. | |
| 42 | +| Less than or equal (<=) | Returns true if the left operand is less than or equal to the right operand. | |
| 43 | + |
| 44 | +- Bitwise Operator: Bitwise operator treats their operands as a set of 32 bits (1s and 0s). For example, the decimal `7` has a binary represntation of `111`. Bitwise operators perform their operations on such binary representations but they return numerical values. |
| 45 | + |
| 46 | +| Operator | Description | |
| 47 | +| --------------------- | ------------------------------------------------------------------------------------------------ | |
| 48 | +| Bitwise AND (`a & b`) | Returns a one in each bit position for which the corresponding bits of both operands are ones. | |
| 49 | +| Bitwise OR (`a \| b`) | Returns a zero in each bit position for which the corresponding bits of both operands are zeros. | |
| 50 | +| Bitwise XOR (`a ^ b`) | Returns a zero in each bit position for which the corresponding bits are the same. | |
| 51 | +| Bitwise NOT (`~ a`) | Inverts the bits of its operand. | |
| 52 | + |
| 53 | +- Logical Operator: They are typically used with boolean values however `&&` and `||` operators actually return the value of one of the specified operands,so if the operands are used with non-boolean values, they may return non-boolean values. |
0 commit comments