Strings and numbers
Strings and numbers are the most common data in any app: emails, product names, prices and phone numbers. This note covers cleaning user input, the daily string methods, template literals, the 0.1 + 0.2 puzzle, Math and rupee formatting, regex basics, and Number vs parseInt.
6.1 Problem: same email, still false
- saved is the email in the database; typed is what the user entered, with extra spaces and some capital letters (common on a mobile keyboard).
- typed === saved is false, so the user gets a login error.
- Strings are compared character by character: a space is a character, and R and r are different characters.
- Step 1
1.User types " Riya@Gmail.com "
- Step 2
2.Compared character by character: spaces and capitals don't match
- Step 3
3.trim(): remove spaces at the start and end
- Step 4
4.toLowerCase(): make every letter small
- Step 5
5.clean === saved: true, login works
Calling methods one after another is method chaining
JavaScript
const saved = "riya@gmail.com";const typed = " Riya@Gmail.com "; console.log(typed === saved); const clean = typed.trim().toLowerCase();console.log(clean);console.log(clean === saved);Output
> false> riya@gmail.com> true6.2 String methods for daily use
| Method / property | What it does | Example result |
|---|---|---|
| length | Character count | 19 |
| includes("iPhone") | Is the word inside? | true |
| slice(6, 12) | Part from start to end position | iPhone |
| split(" ") | Breaks into an array | [ 'Apple', 'iPhone', '16', 'Pro' ] |
| replaceAll(" ", "-") | Replaces every match (URL slug) | Apple-iPhone-16-Pro |
| trim() | Removes spaces at start and end | "riya@gmail.com" |
| toLowerCase() | All letters small | riya@gmail.com |
JavaScript
const product = "Apple iPhone 16 Pro"; console.log(product.length);console.log(product.includes("iPhone"));console.log(product.slice(6, 12));console.log(product.split(" "));console.log(product.replaceAll(" ", "-"));Output
> 19> true> iPhone> [ 'Apple', 'iPhone', '16', 'Pro' ]> Apple-iPhone-16-Pro6.3 Template literals
- Old way: join with + and keep track of spaces yourself.
- Template literal: backticks with variables inside ${ }; same output, cleaner code.
- Multi-line strings are easy, and expressions like a ternary work inside ${ }.
JavaScript
const name = "Riya";const total = 1499; const old = "Hi " + name + ", pay ₹" + total;const msg = `Hi ${name}, pay ₹${total}`;console.log(old);console.log(msg); const bill = `Order: ${name}Total: ₹${total}Delivery: ${total > 999 ? "Free" : "₹49"}`;console.log(bill);Output
> Hi Riya, pay ₹1499> Hi Riya, pay ₹1499> Order: RiyaTotal: ₹1499Delivery: Free6.4 Checkpoint: strings are immutable
- city.toUpperCase() is called, but city still prints delhi.
- No string method changes the original string; every method returns a new string.
- Store the result: city = city.toUpperCase().
JavaScript
let city = "delhi";city.toUpperCase();console.log(city);Output
> delhi6.5 The 0.1 + 0.2 puzzle
- 0.1 + 0.2 gives 0.30000000000000004, so total === 0.3 is false.
- JavaScript stores numbers in binary (64-bit floating point) and 0.1 cannot be stored exactly.
- Fix: round when you display. toFixed(2) gives "0.30" but returns a string.
- Math.round(total * 100) / 100 keeps the result a number (0.3).
JavaScript
const total = 0.1 + 0.2;console.log(total);console.log(total === 0.3); console.log(total.toFixed(2));console.log(Math.round(total * 100) / 100);Output
> 0.30000000000000004> false> 0.30> 0.36.6 Math and rupee formatting
- Math.round: nearest number; Math.floor: always lower; Math.ceil: always higher; Math.max: biggest value.
- Intl.NumberFormat with the en-IN locale and INR currency prints Indian-style commas and the rupee sign.
JavaScript
console.log(Math.round(4.5));console.log(Math.floor(4.9));console.log(Math.ceil(4.1));console.log(Math.max(199, 499, 99)); const price = 125000;const inr = new Intl.NumberFormat("en-IN", { style: "currency", currency: "INR",});console.log(inr.format(price));Output
> 5> 4> 5> 499> ₹1,25,000.006.7 Regex basics: mobile number check
- A regex finds a pattern in text.
- /^[6-9]\d{9}$/: starts with a digit 6-9 followed by exactly 9 more digits (a valid Indian mobile).
- test() returns true or false.
- replace(/\D/g, "") removes every non-digit; slice(-10) keeps the last 10 digits.
JavaScript
const phone = /^[6-9]\d{9}$/; console.log(phone.test("9876543210"));console.log(phone.test("12345")); const typed = "+91 98765-43210";const digits = typed .replace(/\D/g, "") .slice(-10);console.log(digits);console.log(phone.test(digits));Output
> true> false> 9876543210> true6.8 Comparison: Number vs parseInt
| Input | Number() | parseInt() |
|---|---|---|
| "42" | 42 | 42 |
| "42px" | NaN | 42 (reads leading digits) |
| "" (empty) | 0 | NaN |
| Use for | Strict validation | Pulling a number out of values like CSS sizes |
JavaScript
console.log(Number("42"));console.log(parseInt("42")); console.log(Number("42px"));console.log(parseInt("42px")); console.log(Number(""));console.log(parseInt(""));Output
> 42> 42> NaN> 42> 0> NaN6.9 Common mistakes
- toFixed too early: price becomes a string, + joins, and the total becomes "499.0040".
- Fix: calculate in numbers and apply toFixed only at the end (539.00).
- Calling a string method without storing the result.
- Comparing user input without trim / toLowerCase.
JavaScript
// Wrongconst price = 499;const delivery = 40;const total = price.toFixed(2) + delivery;console.log(total);JavaScript
// Rightconst price = 499;const delivery = 40;const total = price + delivery;console.log(total.toFixed(2));6.10 Rules
- Clean user input (trim, lowercase) before comparing or saving it.
- Build strings with template literals.
- Do money calculations in numbers; round or format only at display time.
- String methods return a new string, so store the result.
6.11 Interview tips
- Why 0.1 + 0.2 !== 0.3: numbers are 64-bit floating point and 0.1 cannot be represented exactly, so a small precision error appears.
- Solution: toFixed or Intl.NumberFormat for display, and calculate money in paise (integers).
- Strings are immutable; methods return new strings.
- Number("42px") is NaN but parseInt("42px") is 42.