Types and Operators
Work with JavaScript's primitive values, comparisons, coercion, and useful operators.
Every JavaScript value has a type. Understanding how values compare and convert prevents subtle bugs at input and API boundaries.
Primitive values
JavaScript has string, number, bigint, boolean, undefined, symbol, and null primitive values. Objects, arrays, and functions are reference values.
const title = "JavaScript";
const lessons = 12;
const published = true;
const selected = null;
let draft;
console.log(typeof title); // "string"
console.log(typeof lessons); // "number"
console.log(typeof draft); // "undefined"
typeof null returns "object" for historical reasons. Test null directly with value === null.
Compare without surprise
console.log(3 === 3); // true
console.log("3" === 3); // false
console.log(5 > 2); // true
console.log("arc" !== "byte"); // true
Prefer strict equality. Loose equality performs implicit conversion that can hide invalid data.
Useful modern operators
const settings = { theme: null };
const theme = settings.theme ?? "system";
const city = settings.profile?.address?.city;
const label = `Theme: ${theme}`;
Nullish coalescing uses the fallback only for null or undefined. Optional chaining stops safely when a part of the path is missing.
Keep this
Know the primitive types, prefer strict equality, and make conversions explicit at system boundaries.