This lesson explains the fundamental concepts of values and types in JavaScript, covering built-in data types, the difference between primitives and objects, common quirks, and practical examples for beginners. It aims to help learners write robust JavaScript code by understanding how data is stored, manipulated, and checked.
"If a variable falls in a forest and no one logs it, does it still have a type?" — Probably. Also, 100% yes, it does. You just finished getting your environment battle-ready: saving sketches, managing assets folders, and wrestling with setup gremlins. Good job! Now let’s turn the machine into somet...
1) What's a value ? What's a type ? Value : the actual thing stored (like 42, "hello", [1,2,3]). Type : the label that describes what kind of thing that value is (Number, String, Boolean, Object...). Think of values as luggage and types as the luggage tags. If you hand the airport a fragile packa...
2) The important built-in types (cheat-sheet + examples) Primitives (immutable, copied by value): Number — numeric values (integers, floats) String — text in quotes Boolean — true / false null — explicit “no value” undefined — variable exists but has no assigned value BigInt — very large inte...
3) Primitives vs Objects — the postcard vs suitcase analogy Primitives = postcards. When you hand someone the postcard, they get their own copy; changing yours does not change their copy. Objects = suitcases. When you hand someone the suitcase, they have the same suitcase — if they add clothes, yo...
4) Quirks and gotchas (the stuff people google at 2 AM) null vs undefined undefined : the variable exists but you didn’t assign anything. null : you explicitly assigned "no value". NaN (Not-a-Number) typeof NaN === "number" (another party trick). NaN results from invalid numeric operations. Us...
5) Practical mini-examples you’ll actually use in CS30 Checking a value type before using it (defensive programming): function drawImageIfLoaded(img) { if (img && typeof img === 'object') { image(img, 0, 0); } else { console.warn('Expected an image object — found:', img); } } Clone an array ...
8 study modes available based on your content