Type Conversion in JavaScript
Table of Contents + β
In the previous lesson, we learned about the different data types. Now letβs see how JavaScript converts values from one type to another.
π What is Type Conversion?
Type conversion means changing a value from one type to another, such as turning the text "42" into the number 42.
There are two kinds:
- Explicit conversion β - you convert the value yourself, on purpose
- Implicit conversion π€ - JavaScript converts the value automatically
β Explicit Conversion
You convert values yourself using built-in functions. This is the clearest and safest way to change a type.
| Function | Converts to | Example |
String(value) | A string | String(42) β "42" |
Number(value) | A number | Number("42") β 42 |
Boolean(value) | A boolean | Boolean(1) β true |
Here it is in code:
const age = Number("25"); // string "25" becomes number 25const score = String(100); // number 100 becomes string "100"const isActive = Boolean(1); // number 1 becomes true
console.log(age, score, isActive); // 25 "100" trueLetβs walk through each conversion:
Number("25")takes the text"25"and returns the actual number25, which we store inage.String(100)takes the number100and returns the text"100", which we store inscore.Boolean(1)turns the number1into the booleantrue, which we store inisActive.console.logprints all three results side by side:25 "100" true.
A common use case
When you read a number from a form input, it comes in as text. You use
Number(...) to turn it into a real number before doing any math with it.
π€ Implicit Conversion (Type Coercion)
Sometimes JavaScript converts types automatically. This is called type coercion, and it often happens with the + operator and comparisons.
console.log("5" + 1); // "51" β number 1 becomes a string, then joinsconsole.log("5" - 1); // 4 β string "5" becomes a number, then subtractsconsole.log("5" * 2); // 10 β string "5" becomes a numberHere is what each line does:
"5" + 1sees a string on the left, so the number1becomes the text"1"and the two join into"51"."5" - 1uses subtraction, so the string"5"becomes the number5and the result is4."5" * 2uses multiplication, so the string"5"becomes the number5and the result is10.
The + operator is special: if either side is a string, it joins them as text. The other math operators (-, *, /) always convert to numbers.
Coercion can surprise you
Automatic conversion is a common source of confusing bugs. When in doubt,
convert values yourself with Number(...) or String(...) so your code does
exactly what you expect.
π’ Converting Strings to Numbers
Besides Number(), you can use parseInt and parseFloat to pull numbers out of text.
console.log(Number("42")); // 42console.log(parseInt("42px")); // 42 β reads the number, ignores "px"console.log(parseFloat("3.14")); // 3.14console.log(Number("hello")); // NaN β "hello" is not a numberLetβs compare these four approaches:
Number("42")converts the clean text"42"into the number42.parseInt("42px")reads digits from the front, stops at"px", and returns the whole number42.parseFloat("3.14")reads a decimal number from the text and returns3.14.Number("hello")fails because"hello"is not a number, so it returnsNaN.
What is NaN?
NaN stands for βNot a Numberβ. JavaScript returns it when you try to convert
something that is not a valid number, like Number("hello").
β οΈ Common Mistakes to Avoid
| Mistake | Problem | Solution |
| Adding a string and a number | "5" + 1 gives "51", not 6 | Convert with Number(...) before adding |
| Doing math on form input directly | Input values are strings | Convert to a number first |
Ignoring NaN | Invalid conversions produce NaN | Check that the value is a valid number |
π§ Try It Yourself!
- Convert the string
"123"to a number and print it. - Convert the number
99to a string and print its type withtypeof. - Print the result of
"5" + 2and"5" - 2, and explain the difference to yourself. - Try
Number("abc")and see that it returnsNaN.
π§© What Youβve Learned
- β Type conversion changes a value from one type to another
- β
Explicit conversion uses
String(),Number(), andBoolean() - β
Implicit conversion (coercion) happens automatically, often with
+ - β
+joins text if either side is a string; other math operators convert to numbers - β
parseIntandparseFloatread numbers out of text, andNaNmeans βNot a Numberβ
π Whatβs Next?
You have finished the JavaScript Basics module. Next, we will start working with operators, beginning with arithmetic. Letβs continue to Arithmetic Operators.