Data Types
Primitive Types
- Number: Numbers of any kind (integers or decimals).
- String: Text strings.
- Boolean:
trueorfalse.
The “Crazy Values”
JavaScript has unique behaviors with certain values that can be confusing at first:
1. NaN (Not-a-Number)
Occurs when you perform an invalid math operation (e.g., "hello" * 2).
- Fun fact:
typeof NaNis"number". NaN === NaNisfalse! You must useisNaN()to check for it.
2. null
Represents the intentional absence of a value. It is an object (typeof null is "object").
3. undefined
Means a variable has been declared but not assigned a value yet.
4. Falsy Values
In boolean contexts (like an if), these values evaluate to false:
0""(empty string)nullundefinedNaNfalse
The typeof operator
Use it to know what data type a variable has at that moment.
let x = 10;
console.log(typeof x); // "number"