Explicit Conversion Techniques
Convert types deliberately with Number, String, Boolean.
Explicit Conversion Techniques is a free JavaScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Explicit Conversion
Explicit conversion means deliberately changing a value type with a function, instead of relying on operators to coerce it. This makes code clearer and safer.
console.log(Number("42"))
console.log(String(42))Number Function
The Number function converts a value to a number. Numeric strings convert cleanly; non-numeric strings become NaN.
console.log(Number("3.14"))
console.log(Number(" 10 "))
console.log(Number("abc"))Number Edge Cases
Number treats an empty string and the value null as zero, but undefined becomes NaN. Knowing these edges prevents bugs.
console.log(Number(""))
console.log(Number(null))
console.log(Number(undefined))parseInt
parseInt reads an integer from the start of a string, stopping at the first non-numeric character. It ignores trailing text.
console.log(parseInt("42px"))
console.log(parseInt("3.99"))
console.log(parseInt("abc"))parseInt Radix
Always pass a radix to parseInt to declare the number base. Use 10 for normal decimal parsing to avoid ambiguity.
console.log(parseInt("0x1F", 16))
console.log(parseInt("101", 2))
console.log(parseInt("08", 10))parseFloat
parseFloat reads a floating-point number from the start of a string, keeping the decimal portion that parseInt would drop.
console.log(parseFloat("3.14m"))
console.log(parseFloat("1.5e3"))String Function
The String function converts any value to its text representation, including null and undefined, which become the words null and undefined.
console.log(String(123))
console.log(String(true))
console.log(String(null))toString Method
Most values have a toString method, which can take a radix for numbers to produce binary or hexadecimal text.
console.log((255).toString(16))
console.log((5).toString(2))Boolean Function
The Boolean function converts a value using the truthy and falsy rules, returning true or false explicitly.
console.log(Boolean(1))
console.log(Boolean(""))
console.log(Boolean("no"))Choosing the Right Tool
Use Number for full-string numeric conversion, parseInt or parseFloat for extracting numbers from mixed text, and String or toString for text output.
console.log(Number("100"))
console.log(parseInt("100 dollars", 10))Validating Conversions
After converting to a number, check with Number.isNaN to confirm the conversion succeeded before using the result.
const raw = "x9"
const n = Number(raw)
if (Number.isNaN(n)) {
console.log("invalid number")
}Quick Check
Test your conversion knowledge.
Recap
Recap: Convert types explicitly for clarity.
- Number converts whole strings; non-numeric become NaN
- parseInt and parseFloat extract leading numbers
- Always pass a radix to parseInt
- String and Boolean convert to text and truth values
console.log(Number("7"))
console.log(String(7))Frequently asked questions
Is the “Explicit Conversion Techniques” lesson free?
Yes — the full text of “Explicit Conversion Techniques” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Explicit Conversion Techniques”?
Convert types deliberately with Number, String, Boolean. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Explicit Conversion Techniques” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Truthy and Falsy Values
- Loose vs Strict Equality
- Implicit Coercion in Operations
- Explicit Conversion Techniques