Skip to content
dreamcode
dreamcode
Map
Type conversion
Lesson 4 of 48
+15 XP on finish
JS BASICSChapter 1 · JS Basics

Types and Conversion

typeof tells you what kind of value you have: "number", "string", "boolean", "object" or "undefined". Convert on purpose with Number("42"), String(42) and Boolean(value). JavaScript also converts automatically in some places, which surprises people: "5" * 2 is 10, but "5" + 2 is "52". The values 0, "", null, undefined and NaN are falsy; everything else is truthy.

Worked example

How it reads

  • Number("sky") cannot convert, so it gives NaN (not a number)
  • * always does maths, but + joins text if either side is a string
  • Empty text and 0 are falsy, so Boolean("") is false
Cloud tip: Convert input explicitly with Number(...) as soon as you receive it, instead of relying on automatic conversion later.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Change the last line so it prints the real total, 10.

Press Run to check your work.