Make conversions visible
JavaScript can convert values automatically when an operator needs a particular type. That is convenient until a value comes from a text field, a URL parameter, or another source you do not control. For example, "5" + 2 produces "52" because the plus operator joins the values as text, while "5" - 2 produces 3. An explicit conversion makes your intended type easier to see and lets you decide what to do with invalid input.
The examples here work in a modern browser console or a current Node.js environment; they need no libraries or page markup. Use String(value) when you want text, Number(value) when you want a number, and Boolean(value) when you want JavaScript’s truthiness converted to a boolean. These functions perform conversions, not validation. The distinction matters most when the original value is text.
Convert to strings without hiding the source value
String(42) returns "42", and String(false) returns "false". These conversions are useful when preparing a label or a message. String(null) returns "null" and String(undefined) returns "undefined", however. Those results may accurately describe the values in code but look odd in an interface.
Decide how to handle missing values before converting them. For example, const label = value == null ? "Not provided" : String(value); gives both null and undefined a readable label while preserving 0 and false. This is a deliberate use of == null to match those two missing values. If empty text should also count as missing, check for it separately rather than assuming every falsy value means the same thing.
Validate numeric text before calculating
Number("24") returns 24, and Number(" 24 ") also returns 24. But Number("") and Number(" ") return 0. Number("24px") returns NaN, a special number value meaning the conversion did not produce a usable number. Number(true) returns 1 and Number(false) returns 0. These results are defined behavior, but they may not match what a form or application considers valid input.
For a field that should contain a number, check the text before using the result: function convertNumber(raw) { const text = raw.trim(); if (text === "") return null; const value = Number(text); return Number.isFinite(value) ? value : null; }. Now convertNumber("0") returns 0, while convertNumber(" ") and convertNumber("24px") return null. Here null means invalid input; it is not the number zero. This example expects raw to be a string, so check or normalize its type first if your caller might pass something else.
Number() accepts some formats beyond ordinary decimal notation, including exponent notation such as "1e3". If your interface accepts only a narrower format, validate that format before calling Number(). Avoid switching to parseInt() or parseFloat() merely to make an error disappear: they can read a numeric prefix and ignore trailing text, so parseFloat("24px") returns 24. That may be useful for a specific parsing task, but it is not strict validation.
Recognize NaN instead of comparing it directly
NaN is unusual: NaN === NaN is false. Use Number.isNaN(value) to ask whether a value is actually NaN. For example, Number.isNaN(Number("oops")) is true, whereas Number.isNaN("oops") is false because the string has not been converted. Number.isFinite(value) is often a better final check for calculations because it rejects NaN, Infinity, and -Infinity without converting a string for you.
Keep the original input available until validation is complete. If you immediately use Number(raw) in an arithmetic expression, an invalid result can spread through later calculations as NaN. Check at the boundary where the input enters your program, then pass the validated number onward. When testing the conversion, include zero, surrounding spaces, empty text, nonnumeric text, and any numeric format your interface claims to accept.
Parse boolean text rather than relying on truthiness
Boolean(value) answers whether JavaScript treats a value as truthy. Boolean(0), Boolean(""), and Boolean(null) are false. Boolean("false") and Boolean("0") are true because both are nonempty strings. That makes Boolean(raw) unsuitable for interpreting a text field that explicitly says "true" or "false".
For such a field, define the accepted words: function parseBoolean(raw) { const text = raw.trim().toLowerCase(); if (text === "true") return true; if (text === "false") return false; return null; }. This preserves false as a valid answer and reserves null for invalid input. The same principle applies across all three types: choose the values your application accepts, convert explicitly, and keep validation separate from JavaScript’s automatic conversion rules.



