EXERCISE
1JavaScript does not lock a variable to one type. You can store a number, then replace it with a string, then replace it with a boolean. That is called a loosely typed (or weakly typed) language.
Save
In languages like Java or C++, if you say a variable is a number, it is always a number. does not work that way.
A variable can change type freely:
var thing = 42;
console.log(typeof thing); // "number"
thing = "hello";
console.log(typeof thing); // "string"
thing = true;
console.log(typeof thing); // "boolean"
thing = undefined;
console.log(typeof thing); // "undefined"
// No errors. No warnings. Total freedom.
Why this matters for bugs:
var price = 100;
// ... 50 lines of code later ...
price = "free"; // Oops, accidentally changed to string
var total = price * 2;
console.log(total); // NaN (Not a Number)
// JavaScript did not stop you from turning a number into a string
// It silently calculated "free" * 2 = NaN
// This is a real-world bug that catches beginners daily
The 7 primitive types:
// 1. Number -> 42, 3.14
// 2. String -> "hello", 'world'
// 3. Boolean -> true, false
// 4. undefined -> undefined
// 5. null -> null
// 6. Symbol -> Symbol("id")
// 7. BigInt -> 999999999999999n
> Key Insight: Loosely typed means faster to write but harder to debug. Professional developers use strict equality (===) instead of loose equality (==) to avoid type-related surprises. And this is why exists: it adds type safety on top of JavaScript.