JavaScript Data Types

Understanding different types of data in JavaScript

📊 What are Data Types?

JavaScript data types define what kind of data a variable can hold. JavaScript has dynamic typing, meaning you don't need to specify the data type when creating variables.


// JavaScript automatically determines data types
let name = "John";        // String
let age = 25;            // Number
let isStudent = true;    // Boolean
                                    

Output:

name: "John" (string)

age: 25 (number)

isStudent: true (boolean)

JavaScript Data Types

🔤

String

Text data enclosed in quotes

let message = "Hello World";
🔢

Number

Integers and floating-point numbers

let count = 42;
let price = 19.99;

Boolean

True or false values

let isActive = true;
let isComplete = false;

Undefined

Variable declared but not assigned

let x;
console.log(x); // undefined

🔹 Primitive Data Types

JavaScript has 7 primitive data types:

// 1. String
let firstName = "Alice";
let lastName = 'Smith';
let template = `Hello ${firstName}`;

// 2. Number
let integer = 100;
let decimal = 3.14;
let negative = -50;

// 3. Boolean
let isLoggedIn = true;
let hasPermission = false;

// 4. Undefined
let notAssigned;
console.log(notAssigned); // undefined

// 5. Null
let emptyValue = null;

// 6. Symbol (ES6)
let id = Symbol('id');

// 7. BigInt (ES2020)
let bigNumber = 123456789012345678901234567890n;

Output:

firstName: "Alice"

template: "Hello Alice"

integer: 100

decimal: 3.14

isLoggedIn: true

notAssigned: undefined

emptyValue: null

🔹 Non-Primitive Data Types

Objects are non-primitive data types that can store multiple values:

// Object
let person = {
    name: "John",
    age: 30,
    city: "New York"
};

// Array
let colors = ["red", "green", "blue"];
let numbers = [1, 2, 3, 4, 5];

// Function
function greet(name) {
    return "Hello " + name;
}

// Date
let today = new Date();

console.log(person.name);     // "John"
console.log(colors[0]);       // "red"
console.log(greet("Alice"));  // "Hello Alice"

Output:

person.name: "John"

colors[0]: "red"

greet("Alice"): "Hello Alice"

🔹 Dynamic Typing Example

JavaScript variables can change their data type:

let data = "Hello";        // String
console.log(typeof data);   // "string"

data = 42;                  // Now it's a Number
console.log(typeof data);   // "number"

data = true;                // Now it's a Boolean
console.log(typeof data);   // "boolean"

data = ["a", "b", "c"];     // Now it's an Array (Object)
console.log(typeof data);   // "object"

Output:

First typeof: "string"

Second typeof: "number"

Third typeof: "boolean"

Fourth typeof: "object"

🧠 Test Your Knowledge

What data type is the value 'true' in JavaScript?