|
| 1 | +# JavaScript Operations and Conversion Rules 📊 |
| 2 | + |
| 3 | +JavaScript provides various operations for performing calculations and manipulating data, along with rules for converting between different data types. |
| 4 | + |
| 5 | +## Conversion Rules |
| 6 | + |
| 7 | +| From | To | Conversion | Example | |
| 8 | +|------------|------------|-----------------------------|------------------------------------| |
| 9 | +| String | Number | Use `Number()` function | `let valueInNumber = Number(score);` | |
| 10 | +| Number | String | Use `String()` function | `let stringNumber = String(someNumber);` | |
| 11 | +| String | Boolean | Use `Boolean()` function | `let booleanIsLoggedIn = Boolean(isLoggedIn);` | |
| 12 | +| Number | Boolean | Non-zero numbers convert to `true`, zero converts to `false` | `let isLoggedIn = "hitesh"; let booleanIsLoggedIn = Boolean(isLoggedIn);` | |
| 13 | + |
| 14 | +## Operations |
| 15 | + |
| 16 | +| Operator | Description | Example | Result | |
| 17 | +|------------|--------------------------------|------------------------------|---------| |
| 18 | +| `+` | Addition | `2 + 2` | `4` | |
| 19 | +| `-` | Subtraction | `5 - 3` | `2` | |
| 20 | +| `*` | Multiplication | `4 * 3` | `12` | |
| 21 | +| `/` | Division | `10 / 2` | `5` | |
| 22 | +| `**` | Exponentiation | `2 ** 3` | `8` | |
| 23 | +| `%` | Modulus (Remainder) | `10 % 3` | `1` | |
| 24 | +| `++` | Increment | `let gameCounter = 100; ++gameCounter;` | `101` | |
| 25 | +| `--` | Decrement | `let Counter = 100; Counter++;` | `101` | |
| 26 | + |
| 27 | +Avoid complex conversions for clarity and readability. These operations and conversion rules are fundamental in JavaScript for manipulating data and performing calculations. |
| 28 | + |
| 29 | +### Unary Plus and Minus |
| 30 | +- Unary Plus (+): Converts values to numbers. |
| 31 | +- Unary Minus (-): Negates values. |
| 32 | + |
| 33 | +### Increment and Decrement Operators |
| 34 | +- Prefix Increment (++i): Increments the value before using it. |
| 35 | +- Postfix Increment (i++): Uses the value before incrementing it. |
| 36 | + |
| 37 | +## Example: Type Conversion and Operations |
| 38 | +```javascript |
| 39 | +let score = "hitesh"; |
| 40 | +let valueInNumber = Number(score); |
| 41 | +console.log(typeof valueInNumber); // Output: "number" |
| 42 | +console.log(valueInNumber); // Output: NaN (not a number) |
| 43 | + |
| 44 | +let isLoggedIn = "hitesh"; |
| 45 | +let booleanIsLoggedIn = Boolean(isLoggedIn); |
| 46 | +console.log(booleanIsLoggedIn); // Output: true |
| 47 | + |
| 48 | +let num1 = 1; |
| 49 | +let num2 = 2; |
| 50 | +let result = num1++ + ++num2; // Equivalent to (1 + 3) |
| 51 | +console.log(result); // Output: 4 |
| 52 | + |
| 53 | +``` |
0 commit comments