Primitive Types & Safe Conversions
Identify JavaScript's primitive values, inspect them with typeof, and convert between text, numbers, and booleans on purpose.
By the end, you’ll be able to…
- Name the seven primitive types and distinguish null from undefined.
- Use typeof correctly, including the null quirk.
- Convert values explicitly with Number, String, and Boolean.
- Use template literals and avoid accidental coercion.
01Seven primitive types
JavaScript has seven primitive types: string, number, boolean, null, undefined, bigint, and symbol. A primitive is an immutable value, and primitive values are compared by value. Objects, arrays, and functions are a separate family that you will study later.
undefined usually means a value has not been assigned. null is an intentional absence, chosen by the programmer to say there is no value here. They are different values. A bigint represents an integer larger than the safe number range and is written with an n suffix, such as 42n.
02typeof and the null quirk
typeof is an operator that returns a short string describing a value. It is useful for quick checks, but it is not a complete type system. For example, typeof 42n is bigint, while typeof Symbol() is symbol. Keep the returned strings lowercase.
The famous exception is typeof null, which returns object. This is a historical bug preserved for compatibility. A function also reports function even though functions are objects. Remember these two cases instead of trusting typeof as a perfect label.
03Conversions you ask for
Number, String, and Boolean convert values when you call them. Number("42") produces 42, while Number("four") produces NaN. Number("") produces 0 because an empty string converts to zero. String(42) produces the text 42, and Boolean(0) produces false.
Boolean conversion follows truthiness. Boolean("") is false, but Boolean("false") is true because a non-empty string is truthy. Do not assume the visible word decides the result. When a conversion can fail, inspect the result before using it in arithmetic. If the result is NaN, stop and ask what the input actually contained.
04Template literals make messages readable
A template literal uses backticks and ${...} placeholders. The expression inside the braces is evaluated and inserted into the text. This is easier to read than joining several pieces with plus signs, especially when a sentence contains more than one value.
A template literal always produces a string, even if every placeholder holds a number. Use one when you want to build a labeled message. Keep the embedded expressions short; if a placeholder becomes complicated, calculate it first and give it a descriptive name.
05Avoid accidental coercion
The plus operator is overloaded. If either operand is a string, plus joins text instead of adding numbers. "5" + 2 produces "52". The minus, multiply, and divide operators instead try to convert their operands to numbers, so "5" - 2 produces 3. Those inconsistent rules are why explicit conversion is safer.
Convert before calculating and keep converted values in clearly named variables. Compare Number("5") + 2, which is 7, with "5" + 2, which is text. When a value arrives from a form, a file, or a network request, treat it as text until you have converted and checked it. A named converted value also documents that decision for the next reader.
The ideas worth keeping.
- JavaScript has seven primitive types; null is an intentional absence and undefined is usually an unassigned value.
- typeof is useful but has quirks: typeof null is object and functions report function.
- Convert explicitly with Number, String, or Boolean, and use template literals to build readable messages.
Make it yours.
Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.
Exercise 1: Build a type report
Declare text = "42", count = 42, active = true, missing = null, notSet = undefined, and large = 42n. Print one line for each value containing typeof followed by the value itself.
Explore a worked solution
const text = "42";const count = 42;const active = true;const missing = null;const notSet = undefined;const large = 42n;console.log(typeof text, text);console.log(typeof count, count);console.log(typeof active, active);console.log(typeof missing, missing);console.log(typeof notSet, notSet);console.log(typeof large, large);The output begins with string 42, number 42, and boolean true. The null line reads object null because of the historical typeof quirk, and the last line reads bigint 42.
Exercise 2: Calculate a checkout total safely
The values priceText = "19.5" and quantityText = "3" arrive as text. Convert both explicitly, multiply them, and print a line in the form Total: 58.5.
Explore a worked solution
const priceText = "19.5";const quantityText = "3";const price = Number(priceText);const quantity = Number(quantityText);console.log(`Total: ${price * quantity}`);Number converts each string before multiplication. Without those conversions, the template would still build a string, but a plus-based calculation would concatenate the text instead of adding it.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
Exercise 1: Build a type report
Declare text = "42", count = 42, active = true, missing = null, notSet = undefined, and large = 42n. Print one line for each value containing typeof followed by the value itself.
string 42 number 42 boolean true object null undefined undefined bigint 42
Keep exploring.
The official references behind this lesson, when you want a second angle or the full details.
One lesson closer.
Reading is a start. Trying it yourself makes the connection.