Types, operators and tricky conversions
Predict what an expression produces before using it in a real program.
- 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.
1console.log(typeof 42);
2console.log(typeof "42");
3console.log(Number("42") + 1);
4console.log("42" + 1);number string 43 421
Try it
Up to 15 XPSort the values
Before clicking, predict which values are numbers, strings, booleans or empty markers.
“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).
1const seats = 0;
2console.log(seats || 10);
3console.log(seats ?? 10);
4console.log(Number.isNaN(Number("oops")));10 0 true
Key takeaways
Inspect types with
typeofand 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.
Calculate a reliable cart total
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
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…