Data Types in JavaScript
Master JavaScript data types! Learn about strings, numbers, booleans, null, undefined, objects, and how to work with each type.
Master JavaScript data types! Learn about strings, numbers, booleans, null, undefined, objects, and how to work with each type. This hands-on tutorial focuses on practical implementation of data types in javascript concepts.
Data Types in JavaScript
JavaScript has different types of data. Understanding data types is crucial for writing effective code!
1. What are Data Types? π¨
What is it? Data types define what kind of value a variable can hold. Think of them as categories of information.
Why use it? Knowing the type helps you understand what operations you can perform. You can't multiply a "word" by 5, but you can multiply a number.
How it works: JavaScript is dynamically typed, meaning you don't have to specify the type when declaring a variable. The engine figures it out automatically.
2. Primitive Data Types π¦
Primitive types are the basic building blocks. They are immutable (cannot be changed) and stored directly in the Stack memory.
The 7 Primitives:
- String: Text data (
"Hello") - Number: Integers and decimals (
42,3.14) - Boolean: Logical values (
true,false) - Null: Intentionally empty value (
null) - Undefined: Variable declared but not assigned (
undefined) - Symbol: Unique identifier (Advanced)
- BigInt: Very large numbers (Advanced)
3. Reference Data Types π
Reference types are more complex. They are mutable (can be changed) and stored in the Heap memory. The variable only holds a reference (address) to the data.
Common Reference Types:
- Object: Collection of key-value pairs
- Array: Ordered list of values
- Function: Block of code
4. Primitives vs Reference Types (Memory) π§
This is a critical concept!
- Primitives are compared by value.
- Reference Types are compared by reference (memory address).
5. Type Coercion π
JavaScript tries to be helpful by automatically converting types, but this can lead to bugs.
Implicit Coercion:
"5" + 3->"53"(Number becomes String)"5" - 3->2(String becomes Number)true + 1->2(true becomes 1)
6. typeof Quirks π
The typeof operator tells you the type of a value, but it has some historical bugs.
7. Symbols & BigInt (Advanced) π
Symbol
Used to create unique identifiers for object properties.
const sym1 = Symbol("id");
const sym2 = Symbol("id");
console.log(sym1 === sym2); // false (Always unique!)
BigInt
Used for numbers larger than 2^53 - 1 (9 quadrillion).
const big = 9007199254740991n; // Notice the 'n' at the end
Practical Example: Data Type Validator π‘οΈ
AI Mentor
Confused about "JavaScript data types, primitives vs references, type coercion, and memory allocation"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 4What does typeof null return?
Key Takeaways
β
Primitives (String, Number, etc.) are stored by value in the Stack.
β
Reference Types (Object, Array) are stored by reference in the Heap.
β
typeof null is "object" (it's a bug!).
β
Type Coercion can be tricky; be explicit with conversions.
β
Symbols are unique, BigInt is for huge numbers.
What's Next?
Now that you understand data types, let's learn about Operators & Expressions to manipulate and compare these values!
Keep coding! π