All topics › Unit 4 · JavaScript Fundamentals
Topic 43 · 42 of 43
Data Types
All in one: every data type, typeof answer and conversion, side by side
What this example compares
- The quotes decide:
25 + 5vs"25" + 5, andtypeof xvstypeof(x) - The eight types: the seven primitives (string, number, bigint, boolean, undefined, null, symbol) vs the object type
- Strings:
"double"vs'single'vs`backticks`vs"", and digits kept as text - Numbers: whole vs decimal vs
InfinityvsNaN, all one type - Booleans:
truevsfalsevs a comparison vs the string"true" - Nothing, two ways:
undefinedvsnull - Big whole numbers: number vs bigint (
25n), and why10n + 1is an error - Unique labels:
Symbol("id")vs anotherSymbol("id")vs the string"id" - The object type: object
{ }vs array[ ]vs function - typeof quirks:
typeof nullvs=== null,typeof []vsArray.isArray(), and"function" - Dynamic typing: one
letholding three types vs aconst - Copied or shared: a primitive vs an object or array, and
===on objects +joins or adds:"5" + 1vstrue + 1,null + 1,undefined + 1- Only maths:
-*/%**with strings - Comparing:
"10" > 9vs"10" > "9", and==vs=== - Converting on purpose:
String()vsNumber(), withparseInt()andnew Number()as look-alikes Boolean(): the falsy values vs the truthy ones, and howifuses them- Typed by a person: a form box with and without
Number()vsprompt()
Point at any word in the code to see what it means (tap on a phone); click a word to keep its meaning on screen. Read the comment above each numbered panel, press Run, then Edit & experiment to try the TRY lines yourself.
Loading the example…
What you should see
You should see 18 panels of small cards. Panel 1 shows 30 next to "255"; panel 2 shows one card for each slide value, where null and the array are both tagged object. Panels 3 to 5 show four kinds of string, eight numbers (including Infinity and NaN, all tagged number) and the booleans next to the string "true". Panel 6 shows three ways to get undefined beside null. In panel 7, 9007199254740993 comes out rounded to ...992, while the bigint keeps the 3; the 10n + 1 card is red with a TypeError. One card in panel 11 is red too: the const card, with "Assignment to constant variable". The other panels show the answers predicted in their comments, such as "405" for the classic bug in panel 13, NaN for Number("42px") and true for Boolean("0"). The Console prints one line per card in the form 13. "5" + 1 -> "51" (string), with one extra line, 4 51 2, after panel 13. Panel 18 prints nothing until you press its buttons.
Quick check: in panel 13 the card maths + bonus shows "405". Give two different edits that make it show 45.
Remove the quotes in let maths = "40";, so both sides are numbers and + adds. Or keep the text and convert it on purpose: Number(maths) + bonus (panel 16). The second fix is the one you use for a value typed into a form, because a form box always gives you a string.