Data Types

JavaScript uses different data types to represent values. Understanding these types is essential to writing effective code.

JavaScript Data Types Overview
TypeDescription
NumberRepresents both integer and floating-point numbers (e.g., 42, 3.14)
StringSequence of characters enclosed in quotes (e.g., "Hello")
BooleanLogical value: true or false
UndefinedVariable declared but not assigned a value
NullRepresents intentional absence of any object value
SymbolUnique and immutable identifier (used less often)
ObjectCollection of properties (complex data structures like arrays, functions, objects)
Illustration of data_types
Illustration of data_types

💡 Primitive vs. Object Types

Primitive types (Number, String, Boolean, Undefined, Null, Symbol) hold simple values. Objects store collections of values and more complex data.

Key Points:

  • typeof operator returns the data type of a value.
  • Strings can be enclosed in single (' '), double (" "), or backticks (` `) for template literals.
  • Null is a special value but typeof null returns "object" (a known JavaScript quirk).

📌 Deep Dive: Checking Data Types with typeof

JAVASCRIPT
console.log(typeof 123);        // "number"
console.log(typeof "text");       // "string"
console.log(typeof true);         // "boolean"
console.log(typeof undefined);    // "undefined"
console.log(typeof null);         // "object"
console.log(typeof {a:1});        // "object"
console.log(typeof Symbol());     // "symbol"
Output
"number"
"string"
"boolean"
"undefined"
"object"
"object"
"symbol"

💡 Null vs Undefined

null means a variable explicitly has no value.
undefined means a variable has been declared but not assigned.

⚠️ typeof null Quirk

typeof null returns "object" due to legacy reasons. Treat null as a primitive representing "no value".

📌 Deep Dive: Primitive vs Object Types

JAVASCRIPT
let num = 42;             // Number (primitive)
let text = "Hello";         // String (primitive)
let list = [1, 2, 3];       // Array (object)
let person = {name: "Joe"}; // Object

console.log(typeof list);    // "object"
console.log(Array.isArray(list)); // true - arrays are special objects
Output
"object"
true

💡 Summary

  • JavaScript has 7 fundamental data types.
  • Primitive types store simple values; objects store collections and complex data.
  • Use typeof to identify types, but be aware of quirks like null.