JavaScript course Lesson 1 of 6

JavaScript from zero: a value has a type

typeof reports the type of a value. A latte label uses a number, a string, and a boolean. The program prints latte $4.50 ready, then number string boolean.

Zoe is one of GoByte's characters. This post was drafted by AI agents in Zoe's voice, then fact checked, run and edited by the GoByte team.

Back to top

JavaScript values have types even though names do not. typeof tells you which one you have. A cup label needs three. 450 is a "number", the price in cents. "latte" is a "string", the drink. true is a "boolean", whether you can hand it over. Operators follow the type. Adding two numbers adds them. Adding a number to a string joins text.

typeof asks the value, with one famous lie#

JavaScript values carry a tag, and typeof returns a string naming that tag. A number, a string, and a boolean report number, string, and boolean. There is no annotation to check against. The hole: typeof null is "object", a leftover from the first implementation, so typeof cannot tell you that a value is a real object. This program does not pass null. It builds the label, then prints the three tags that are honest. A test that must reject null should compare with === null before it trusts typeof.

typeof reports the kind of value
  1. 450numbertypeof
  2. lattestringtypeof
  3. truebooleantypeof
const name = "latte";
const cents = 450;
const ready = true;
const status = ready ? "ready" : "sold out";
const frac = String(cents % 100).padStart(2, "0");
console.log(name + " $" + Math.floor(cents / 100) + "." + frac + " " + status);
console.log(typeof cents, typeof name, typeof ready);
latte $4.50 ready
number string boolean

typeof returns a string#

typeof cents is the string "number", not the number 450. The second line prints three such strings. Use it when a value came from somewhere you do not control, such as an input or a JSON field, and you are about to do arithmetic.

The first line is the label. Math.floor(cents / 100) is the dollars. cents % 100 is the leftover cents, and padStart(2, "0") prints them as two digits so 50 stays 50 and 5 would become 05. The price stays a whole number of cents until that line. typeof null is "object". That is a long-standing oddity. Check value === null when null is possible, and do not trust typeof for that one case. typeof undefined is "undefined". An array's typeof is also "object". Array.isArray is the test for an array.

The plus sign follows the values#

"latte" + 450 is "latte450", because one side is a string and + then joins. 450 + 50 is 500. If a sum suddenly grows a digit on the end of a word, one of the values is a string. Number("450") turns the text into a number before you add. The label above builds the dollars on purpose, in the line that prints. The next lesson groups the string and the number in one object.

Report a mistake

Your product here? Partner with us

Back to top

Discussion

No comments yet. Signed in GoByte members with a verified e-mail can join. Community guidelines

Reading is open to everyone. Commenting and voting need a GoByte account with a verified e-mail.