Learning objective: Check types with typeof, convert explicitly with Number, String and Boolean, parse with parseInt and parseFloat, and predict implicit conversions.
Skills practised: Values and variables, Type conversion.
04Explanation
typeof tells you a value's type: "number", "string", "boolean", "undefined", "object" (also for null and arrays) or "function".
Convert on purpose: Number("25") is 25, String(25) is "25", Boolean("") is false. When text is not a valid number, Number gives NaN.
parseInt("42px") reads digits from the start and stops at the first non-digit, giving 42. parseFloat("3.5kg") gives 3.5. Number("42px") would be NaN, because Number needs the whole text to be a number.
JavaScript also converts implicitly: "5" * 2 is 10, but "5" + 2 is "52" because + prefers joining text. Explicit conversion makes your intention clear and avoids surprises.
Implicit coercion follows the abstract operations ToNumber, ToString, ToPrimitive and ToBoolean. + performs string concatenation if either operand becomes a string after ToPrimitive; other arithmetic operators use ToNumber.
Number("") and Number(" ") are 0, Number(null) is 0 and Number(undefined) is NaN: empty form fields therefore need explicit checks before conversion.
Always pass a radix to parseInt (parseInt(x, 10)) in code that may see leading zeros or hex prefixes. Unary plus (+x) is a terse Number conversion, common in codebases but less readable to beginners.
For type checks use typeof for primitives, Array.isArray for arrays and value === null for null. Validation libraries and TypeScript move many of these checks to the boundary of the program.
05Definitions
typeof
An operator that returns the type name of a value as a string.
Type conversion
Changing a value from one type to another, such as text to number.
Implicit coercion
A conversion JavaScript performs automatically, such as "5" * 2 → 10.
parseInt / parseFloat
Read a whole number / decimal from the start of a string.
Number()
Converts a value to a number, or NaN if the whole value is not numeric.
A booking form adds adult and child tickets. A family of 20 adults and 10 children should see 30 tickets, but the summary says 2010.
const adults = "20"; // from an input field
const children = "10"; // from an input field
const total = adults + children;
console.log("Total tickets:", total);
The Tip Calculator reads the bill, tip percentage and number of people from input fields. Their values are strings, so the project converts them with Number() and checks for NaN before any calculation.
const billText = "80";
const bill = Number(billText);
console.log(Number.isNaN(bill) ? "invalid" : bill * 1.15);
12Knowledge check
1. What does this print?
console.log("5" + 3);
8
53
NaN
"53"
2. What does this print?
console.log(Number(""));
NaN
0
undefined
An error
3. Which converts "19.99" to a number without losing the cents?
parseInt("19.99", 10)
parseFloat("19.99")
String("19.99")
Boolean("19.99")
4. What does this print?
console.log(typeof [1, 2, 3]);
array
object
list
undefined
5. Why does this print 1050 instead of 60?
const price = "10";
console.log(price + 50);
The numbers are too big
price is a string, so + joins the text
Math is needed for addition
The variable names are wrong
6. Both read a quantity from text. Which is clearer to a reader?
const q = +text;
const q = Number(text);
Both are equally clear to beginners
Neither converts
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the form total: Prints Tickets: 5; tickets is the number 5; Inputs stay strings
Read the measurements: Prints the values and types; width and weight are numbers
Validate an age field: Prints four validation results; Uses Number.isNaN
ex-010-01: output matches the documented expected output
ex-010-02: output matches the documented expected output
ex-010-03: output matches the documented expected output
ex-010-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-02-variables-values-and-types/lesson-010-type-conversion-and-type-checking/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
typeof names a value's type; arrays and null report "object".
Convert on purpose with Number, String and Boolean.
parseInt and parseFloat read numbers from the start of text.
+ joins text; other arithmetic converts to numbers. Check for NaN and empty input.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationExplicit versus implicit conversion
Context: I am learning type conversion.
Task: Explain explicit and implicit conversion with six short examples.
Constraints: Beginner level, no objects.
Expected behaviour: I can predict + and - with mixed types.
Testing: End with four snippets to predict.
Code predictionCoercion puzzles
Context: I know Number, parseInt and parseFloat.
Task: Give me eight conversion expressions to evaluate.
Constraints: Include empty strings and units like px. Wait for answers.
Expected behaviour: Each has one answer.
Testing: Explain any I miss.
DebuggingText instead of numbers
Context: My form total prints 2050 instead of 70.
Task: Help me trace which values are strings.
Constraints: Ask me to log typeof for each value.
Expected behaviour: I find and convert the strings myself.
Testing: Suggest one test for empty input.
Exercise generationInput validation drills
Context: I finished the age validation exercise.
Task: Create two validation exercises for text inputs (price, quantity).
Constraints: Exact expected output for each input.
Expected behaviour: Each handles empty, invalid and valid input.
Testing: Include expected output.
Code reviewConversion review
Context: My script reads five form values and calculates a total.
Task: Review where conversions happen and suggest converting once at input.
Constraints: No rewrites; list issues.
Expected behaviour: Each value is converted exactly once.
Testing: Point out any missing NaN check.