Values often need converting — for example, form inputs are always strings.
To number
Number('42')→42parseInt('42px')→42(reads digits from the start)parseFloat('3.5kg')→3.5Number('abc')→NaN
To string
String(42)→'42'(42).toString()→'42'
To boolean
Boolean(value) — but you rarely need it, because JavaScript checks truthiness in if.
Falsy values
These count as false: false, 0, '', null, undefined, NaN. Everything else is truthy, including '0' and [].
Example
Example — Adding two inputs correctly
HTML
<input id="x" value="10" size="4"> +
<input id="y" value="5" size="4">
<button onclick="add()">Add</button>
<pre id="out"></pre>
<script>
function add() {
const x = document.getElementById('x').value;
const y = document.getElementById('y').value;
document.getElementById('out').textContent =
'Without converting: ' + (x + y) + '\n' +
'With Number(): ' + (Number(x) + Number(y));
}
</script>