Data conversion transforms a value from one data type into another. JavaScript performs some conversions automatically, but explicit conversion makes layout calculations, animation timing, and comparisons easier to predict. section 2.2.7 section 2.1.3
Choose conversion deliberately when values come from different sources:
- Use Number(value), unary +, parseInt(), or parseFloat() when numeric input arrives as text. section 2.2.7
- Remember that arithmetic operators do not all behave alike: multiplication coerces numeric strings, while + may concatenate them. section 2.2.7
- Prefer strict equality (=== and !==), which compares both type and value without coercion. section 2.1.3
- Use parentheses when combining conversions and calculations so precedence does not change the intended result. section 2.9
Converting input before a comparison
A prompt or DOM value is a string even when it contains digits. Convert it before comparing it with a number: let input = "100"; let width = 100; let numericInput = Number(input); console.log(numericInput === width); // true. Without conversion, input === width is false. section 2.1.3
The concatenation trap
If input is "20", input * 1.15 produces the numeric value 23, but input + tip combines the string and number as "2023". Convert first when calculating a total: let input = Number(prompt("Enter the restaurant bill amount:")); section 2.8
Table 2.3 JavaScript comparison operators
| Operator |
Name |
Example |
Result |
|
|
Equal (loose) |
|
|
|
|
Equal (strict) |
|
|
|
|
Not equal (loose) |
|
|
|
|
Not equal (strict) |
|
|
|
|
Less than |
|
|
|
|
Greater than |
|
|
|
|
Less than or equal |
|
|
|
|
Greater than or equal |
|
|
Compare loose and strict operators, including the different results for "5" == 5 and "5" === 5. section 2.1.3




















