The difference between a string and a number
JavaScript treats text and numbers as two different types of value. A string is a piece of text, written between quotes. A number is a numeric value, written without quotes. Two values that print the same can still be different types:
console.log(typeof 15); // number
console.log(typeof "15"); // string
The typeof operator tells you which type a value has. The quotes are what decide it. Anything inside quotes is a string, even if it contains only digits. The characters "15" are the digit 1 followed by the digit 5, and JavaScript does not treat them as the quantity fifteen.
You can use single quotes, double quotes or backticks to write a string. '15', "15" and `15` are the same string.
Why the type matters
A number supports arithmetic. A string supports text operations, such as asking for its length or its first character:
const phone = "2025550153";
console.log(phone.length); // 10
console.log(phone[0]); // 2
const age = 29;
console.log(age + 1); // 30
console.log(age.length); // undefined (numbers have no length)
The same operator can do different things depending on the types. With numbers, + adds. With a string on either side, + joins text:
console.log(5 + 3); // 8
console.log("5" + 3); // 53
console.log("5" + "3"); // 53
The lesson says that computers calculate with numbers but not with strings. That is the safe way to think about it, and this behavior is the reason: a numeric string used with + is joined, not added. JavaScript does convert strings to numbers automatically for the other arithmetic operators, so "5" - 3 is 2 and "5" * "2" is 10. A string that is not numeric gives NaN ("not a number"): "abc" - 1 is NaN. The mix of rules is a common source of bugs, so it is best to keep each value in the type that fits it.
Which type should a value have?
Ask what you will do with the value:
- If you calculate with it, use a number. Ages, prices, counts and scores are quantities. You add, compare and average them.
- If it is a label or an identifier, use a string. Phone numbers, postal codes, ID numbers and card numbers are made of digits, but you never add two phone numbers together.
There are practical reasons for storing identifiers as strings. A number drops leading zeros: Number("0012345") is 12345, so an ID that starts with zeros would be changed. Very long digit sequences also lose precision, because JavaScript numbers are 64-bit floating-point values, and integers above 9007199254740991 (Number.MAX_SAFE_INTEGER) are not stored exactly: Number("9007199254740993") becomes 9007199254740992. A string keeps every character exactly as written.
Converting between the two
You can convert on purpose:
console.log(Number("15") + 1); // 16
console.log(String(15) + 1); // 151
console.log((15).toString()); // "15"
console.log(Number("12px")); // NaN
console.log(parseInt("12px")); // 12
console.log(Number("")); // 0
Number(text) converts the whole string and gives NaN if any part is not numeric. parseInt reads digits from the start and stops at the first character that does not fit. An empty string converts to 0, which can surprise you.
The exercise, step by step
The lesson starts with two declarations that have the wrong types:
let age = "29";
let phone = 2025550153;
- Look at
age. An age is a quantity you might calculate with, so it should be the number29. Remove the quotes. - Look at
phone. A phone number is an identifier, so it should be the string"2025550153". Add the quotes.
let age = 29;
let phone = "2025550153";
You can check the result with typeof: typeof age gives "number" and typeof phone gives "string".
Common mistakes
- Adding a numeric string to a number.
"29" + 1is"291", not30. When a result looks like digits stuck together, check for strings. - Comparing strings as if they were numbers.
"10" < "9"istrue, because strings are compared character by character and"1"comes before"9". With numbers,10 < 9isfalse. - Using
==between the two types."10" == 10istruebecause==converts, while"10" === 10isfalse. Prefer===, which also compares the types. - Quoting a number by habit. A value in quotes is text, whatever it contains.
Key takeaways
- Quotes make a string. No quotes, and a numeric literal, make a number.
typeofreveals the type of a value.- Use numbers for quantities you calculate with, and strings for identifiers such as phone numbers.
+joins a string and a number instead of adding them. Convert on purpose withNumber()andString().
