Featureintro
Explicit type conversion
Also called type conversion, typecasting, Number(), String(), Boolean(), parseInt(), parseFloat(), unary plus, double NOT, number coercion, string coercion, boolean coercion.
Number(x), String(x) and Boolean(x) convert a value on request. They use the steps of implicit coercion, with small changes for BigInts and symbols. parseInt reads a number prefix.
- Language
- JavaScript
Synopsis
| To | Function | Short form | Notes |
|---|---|---|---|
| number | Number(x) |
+x |
+x throws for a BigInt; Number(x) does not |
| string | String(x) |
`${x}` |
`${x}` throws for a symbol; String(x) does not |
| Boolean | Boolean(x) |
!!x |
never calls methods on objects |
| integer from text | parseInt(s, radix) |
stops at the first bad character | |
| number from text | parseFloat(s) |
stops at the first bad character |
MDN says that "Type conversion (or typecasting) means transfer of data from one data type to another." In Number("0x11"), "the string "0x11" is explicitly converted to the number 17".1
console.log(Number("0x11"), Number(" 42 "), Number(""), Number("4px"), Number(null), Number(undefined));
console.log(String(12.5), String(null), String([1, 2]), String(Symbol("s")));
console.log(Boolean(""), Boolean("0"), Boolean(0n), Boolean({}), !!NaN);
console.log(parseInt("4px", 10), parseFloat("3.5e2 kg"), parseInt("ff", 16));
17 42 0 NaN 0 NaN
12.5 null 1,2 Symbol(s)
false true false true false
4 350 255
Semantics
r1. The same steps as coercion. The explicit forms run the steps of number, string and Boolean coercion, as listed on the MDN Number, String and Boolean pages.234 The specification defines these steps in 7.1.5
r2. Number(x). "undefined turns into NaN. null turns into 0." Strings "are converted by parsing them as if they contain a number literal. Parsing failure results in NaN."2 "Empty or whitespace-only strings are converted to 0." "Numeric separators are not allowed."2 Example: synopsis.
r3. Number(x) and +x. "+x does exactly the number coercion steps", and Number(x) "uses the same algorithm to convert x, except that BigInts don't throw a TypeError".2 Example: bigint.
r4. String(x). Numbers and BigInts use the steps of toString(10). Objects use the hint 'string', so toString() runs before valueOf().3 String(x) is like `${x}`, "except that Symbols don't throw a TypeError".3
r5. Boolean(x). Only undefined, null, 0, -0, NaN, 0n and '' give false. "All objects become true."4 "boolean coercion does not attempt to convert objects to primitives by calling user methods."4 See Booleans and truthiness.
r6. parseInt and parseFloat read a prefix. "If parseInt encounters a character in the input string that is not a valid numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point."6 "If the first character cannot be converted to a number with the radix in use, parseInt returns NaN."6 Example: synopsis.
Examples
The examples run with Node.js 24.
Number() accepts a BigInt, and unary + does not (rule r3):
This code raises TypeError, on purpose.
console.log(Number(2n ** 64n));
console.log(+10n);
Strings that look like numbers, and strings that do not (rule r2):
const fields = ["12", " 7 ", "", "1_000", "abc"];
for (const f of fields) {
const n = Number(f);
console.log(JSON.stringify(f), n, Number.isNaN(n));
}
"12" 12 false
" 7 " 7 false
"" 0 false
"1_000" NaN true
"abc" NaN true
Pitfalls
The empty string is 0
Number('') is 0, not NaN (rule r2). Test for an empty string first when it means no value.
Boolean('false') is true
Each non-empty string gives true (rule r5).
new Boolean(false) is truthy
Boolean(x) without new gives a primitive. MDN says: "Do not use the Boolean() constructor with new." All objects, "including a Boolean object whose wrapped value is false, are truthy".4 See Primitive wrapper objects.
parseInt is not Number
parseInt('4px', 10) is 4, but Number('4px') is NaN (rule r6).
Comparison
| Topic | JavaScript | Python |
|---|---|---|
| to number | Number(x), parseInt(s, radix)26 |
int(x), float(x)7 |
| underscores in text | Number('1_000') is NaN2 |
int(' -12_345 \n ') is -123457 |
| to Boolean | Boolean(x)4 |
bool(x) uses "the standard truth testing procedure"7 |
Python int returns "an integer object constructed from a number or a string".7 See Type conversions and Type conversion.
References
- Type conversion, MDN Glossary, definition
- Number, MDN, Number coercion
- String, MDN, String coercion
- Boolean, MDN, Boolean primitives and Boolean objects; Boolean coercion
- ECMAScript 2026 Language Specification, 7.1 Type Conversion
- parseInt(), MDN, Description
- Built-in Functions, The Python Standard Library, bool; float; int
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag MZN4YR Edit this page History Report an error Markdown