Skip to content

Commit d3440b8

Browse files
committedFeb 15, 2024
Added more info on integers and floating numbers
1 parent fb2fd38 commit d3440b8

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed
 

‎JavaScript-Quick-Reference.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,14 @@ console.log(multiLineString);
792792

793793
Fundamental number-related functionalities in JavaScript.
794794

795+
### What is an Integer?
796+
797+
An integer is a whole number without any fractional or decimal parts, such as -1, 0, or 42. It's used in scenarios requiring countable quantities, like loop counters, array indexes, or any situation where partial values don't make sense.
798+
799+
### What is a Floating-Point Number?
800+
801+
A floating-point number includes decimal parts, allowing for the representation of fractions and precise measurements, like 3.14 or -0.01. Useful in calculations requiring high precision, such as financial transactions, scientific computations, or any scenario where small, incremental values matter.
802+
795803
### parseInt(string, radix)
796804

797805
- `parseInt("10", 10);` Converts the string "10" to an integer (base 10).
@@ -1832,6 +1840,25 @@ const emailInput = document.getElementById('email').value.toLowerCase();
18321840
const quantity = parseInt(document.getElementById('quantity').value, 10);
18331841
```
18341842

1843+
### Validating Numeric Input with `isNaN()`
1844+
1845+
- **Purpose:** `isNaN(value)` checks if a value is NaN (Not-a-Number). It's useful for validating that the conversion from a string to a number was successful.
1846+
- **How it works:** After converting a string to a number, `isNaN()` can be used to check if the result is numeric. If `isNaN()` returns `true`, the input was not a valid number.
1847+
- **Example:**
1848+
1849+
```javascript
1850+
const inputElement = document.getElementById('input').value;
1851+
const number = Number(inputElement);
1852+
1853+
if (isNaN(number)) {
1854+
console.error('Input is not a valid number.');
1855+
} else {
1856+
console.log('Valid number input:', number);
1857+
}
1858+
```
1859+
1860+
- **Use case:** Essential for form validation, ensuring that inputs expected to be numeric do not contain letters or other invalid characters.
1861+
18351862
### Sanitizing Input
18361863

18371864
- **Purpose:** Removes or escapes potentially harmful characters.
@@ -2054,7 +2081,6 @@ function loadCart() {
20542081

20552082
Web Storage, encompassing both localStorage and sessionStorage, offers robust solutions for persisting data in web applications. By leveraging these technologies, developers can enhance the user experience, providing seamless session continuity and personalized settings across visits. Through practical applications like theme selection, form data preservation, and shopping cart management, Web Storage proves to be an invaluable asset in modern web development.
20562083

2057-
20582084
## [🔝 Back to Top](#top)
20592085

20602086
## 10.1 TIPS AND BEST PRACTICES

0 commit comments

Comments
 (0)
Failed to load comments.