JavaScript Data Types¶
JavaScript is a Dynamically Typed language. This means you don't need to specify the type of a variable, and the same variable can hold different types at different times.
The 8 Types in JavaScript¶
1. Primitive Types (Basic)¶
- String: Textual data (e.g.,
"Hello"). - Number: Both integers and floats (e.g.,
10,3.14). - BigInt: For very large integers.
- Boolean:
trueorfalse. - Undefined: A variable that has not been assigned a value.
- Null: Represents an intentional "empty" value.
- Symbol: A unique and immutable identifier.
2. Complex Types¶
- Object: Used to store collections of data. (Arrays and Functions are also technically Objects).
The typeof Operator¶
You can use typeof to find out what type of data is stored in a variable.
typeof "John" // returns "string"
typeof 3.14 // returns "number"
typeof true // returns "boolean"
typeof {name:'John'} // returns "object"
The Null Bug
In JavaScript, typeof null returns "object". This is a famous bug from the original version of JS that was never fixed to keep the web compatible.