Loading
0x20Lesson 3 of 16

Types, operators and tricky conversions

Predict what an expression produces before using it in a real program.

18 min 3-question quiz 1 code exercise
By the end of this lesson you can
  • Distinguish strings, numbers, booleans, null and undefined
  • Use arithmetic, comparison and logical operators
  • Spot accidental string concatenation and NaN

A value has a type. Numbers do arithmetic; strings hold text; booleans answer yes/no questions. undefined often means a value was never supplied, while null is an intentional empty value. Use typeof to inspect a value. JavaScript converts types in some expressions, so predict the type before the result.

types.js
1console.log(typeof 42);
2console.log(typeof "42");
3console.log(Number("42") + 1);
4console.log("42" + 1);
Output
number
string
43
421

Try it

Up to 15 XP

Sort the values

Before clicking, predict which values are numbers, strings, booleans or empty markers.

0 of 5 sortedScore 0/0
  • “0”

  • “"0"”

  • “false”

  • “null”

  • “undefined”

Use === for strict equality. && and || short-circuit: they may skip the right side. ?? uses a fallback only for null or undefined, so a valid zero stays zero. Number("oops") produces NaN; check it with Number.isNaN(value).

defaults.js
1const seats = 0;
2console.log(seats || 10);
3console.log(seats ?? 10);
4console.log(Number.isNaN(Number("oops")));
Output
10
0
true

Key takeaways

  • Inspect types with typeof and convert input deliberately.

  • === compares without coercion.

  • ?? preserves zero and false; || treats them as fallback cases.

Lesson quiz

3 questions · pass with 3 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write JavaScript

Write the program, run it against the test cases, and use the feedback to build your JavaScript skills.

Exercise 1

Calculate a reliable cart total

+25 XP

Input is JSON with price, quantity and optional discount. Convert the numeric text, treat a missing discount as zero, and print the total with two decimal places. If price or quantity is invalid, print Invalid cart.

  • Normal cart
  • No discount
  • Bad price
main.js
Loading editor…

JavaScript runs in a sandboxed browser frame with a 1.5 second time limit. Input is available as the string variable input.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…