Data Types

Loading concept...

JavaScript Data Types: The Building Blocks of Code đź§±

Imagine you have a magical toy box. Inside, you can store different kinds of toys: numbers, words, yes/no answers, and even empty spaces. JavaScript’s data types work exactly like this toy box!


The Story of the Data Type Kingdom

Once upon a time, in the land of JavaScript, there lived different types of citizens. Each citizen had a special job. Let’s meet them all!


1. Number: The Counter 🔢

What is it? Numbers in JavaScript are like the counting toys in your toy box. They can be whole numbers, decimals, or even negative numbers.

Think of it like: Counting apples in a basket.

let apples = 5;
let price = 2.99;
let temperature = -10;

Cool facts about Numbers:

  • Whole numbers: 1, 42, 1000
  • Decimals: 3.14, 0.5
  • Negative: -20, -0.5
  • Special: Infinity, -Infinity, NaN (Not a Number)
let big = Infinity;
let oops = "hello" * 2; // NaN

Remember: NaN means JavaScript tried to do math but got confused!


2. String: The Storyteller 📝

What is it? Strings are words, sentences, or any text wrapped in quotes. They’re like the storybooks in your toy box.

Think of it like: Writing a message on a sticky note.

let greeting = "Hello, World!";
let name = 'Alice';
let story = `Once upon a time...`;

Three ways to make strings:

  • Double quotes: "Hello"
  • Single quotes: 'Hello'
  • Backticks: `Hello` (special power: can include variables!)
let age = 10;
let message = `I am ${age} years old`;
// Result: "I am 10 years old"

3. Boolean: The Yes/No Decider ✅❌

What is it? Booleans are the simplest citizens. They can only say two things: true or false. That’s it!

Think of it like: A light switch. On or off. Nothing else.

let isHappy = true;
let isRaining = false;
let isBigger = 10 > 5; // true

When do we use them?

  • Is the user logged in? true or false
  • Is the game over? true or false
  • Is 5 greater than 3? true

4. null: The Empty Box 📦

What is it? null means “nothing on purpose.” You put an empty box in your toy box to say “I know this spot exists, but I’m leaving it empty.”

Think of it like: An empty parking spot with a “Reserved” sign.

let treasure = null;
// Later, you might fill it:
treasure = "Gold coins!";

Important: null is different from “nothing at all.” It’s a deliberate choice to have nothing.


5. undefined: The Mystery 🎭

What is it? undefined means “I don’t know what this is yet.” It’s like opening a drawer you’ve never used before.

Think of it like: Asking someone their favorite color before they’ve decided.

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

function greet(name) {
  console.log(name);
}
greet(); // undefined (no name given)

null vs undefined:

null undefined
Empty on purpose Not set yet
You chose nothing JavaScript doesn’t know
“I left this empty” “What’s this?”

6. typeof: The Detective 🔍

What is it? typeof is not a data type itself. It’s a special tool (operator) that tells you what type something is!

Think of it like: A magic magnifying glass that reveals the identity of anything.

typeof 42;        // "number"
typeof "hello";   // "string"
typeof true;      // "boolean"
typeof undefined; // "undefined"
typeof null;      // "object" (weird, but true!)
typeof Symbol();  // "symbol"
typeof 123n;      // "bigint"

Wait, why is typeof null “object”?

This is a famous bug from JavaScript’s early days! It should say “null”, but it says “object” by mistake. Programmers just learned to live with it.

// How to properly check for null:
let value = null;
if (value === null) {
  console.log("It's null!");
}

7. Symbol: The Secret ID 🏷️

What is it? Symbols are unique, one-of-a-kind identifiers. Even if two symbols have the same description, they’re different!

Think of it like: Every snowflake is unique, even if they look similar.

let id1 = Symbol("id");
let id2 = Symbol("id");

console.log(id1 === id2); // false!
// They look the same but are different

Why use Symbols?

  • To create secret keys that won’t clash with other code
  • To add hidden properties to objects
let secretKey = Symbol("password");
let user = {
  name: "Alice",
  [secretKey]: "super_secret_123"
};

console.log(user.name);       // "Alice"
console.log(user[secretKey]); // "super_secret_123"

8. BigInt: The Giant Number 🦕

What is it? Regular numbers in JavaScript have a limit. BigInt lets you work with HUGE numbers that go beyond that limit!

Think of it like: Normal numbers are like a small jar. BigInt is like an infinitely stretchy balloon that can hold any amount.

// Regular number limit
let max = 9007199254740991;

// BigInt goes beyond!
let huge = 9007199254740992n;
let bigger = BigInt("99999999999999999999");

How to create BigInt:

  • Add n at the end: 123n
  • Use BigInt(): BigInt(123)
let big1 = 100n;
let big2 = 200n;
let sum = big1 + big2; // 300n

Warning: You can’t mix BigInt with regular numbers!

// This will cause an error:
// let wrong = 100n + 50;

// Do this instead:
let right = 100n + 50n; // 150n

The Complete Family Portrait 👨‍👩‍👧‍👦

graph TD A[JavaScript Data Types] --> B[Primitive Types] B --> C[Number<br/>42, 3.14] B --> D[String<br/>'hello'] B --> E[Boolean<br/>true/false] B --> F[null<br/>empty on purpose] B --> G[undefined<br/>not set yet] B --> H[Symbol<br/>unique ID] B --> I[BigInt<br/>huge numbers]

Quick Check: What Type Is It?

Value typeof Result
42 "number"
"hello" "string"
true "boolean"
undefined "undefined"
null "object" ⚠️
Symbol() "symbol"
123n "bigint"

Summary: Your New Friends

Type What It Holds Example
Number Any number 42, 3.14
String Text "Hello"
Boolean Yes or No true, false
null Nothing (on purpose) null
undefined Unknown undefined
Symbol Unique ID Symbol("x")
BigInt Giant numbers 9999999999n

You Did It! 🎉

You now know all the JavaScript data types! Think of them as different containers in your coding toy box. Each one holds something special, and now you know exactly what goes where.

Remember:

  • 🔢 Number = math stuff
  • 📝 String = words and text
  • âś… Boolean = true or false
  • 📦 null = empty on purpose
  • 🎭 undefined = not set yet
  • 🔍 typeof = the detective tool
  • 🏷️ Symbol = secret unique IDs
  • 🦕 BigInt = super huge numbers

Now go forth and use these types to build amazing things!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.