DEV Community

Cover image for Understanding JavaScript Data Types
Ridoy Hasan
Ridoy Hasan

Posted on

Understanding JavaScript Data Types

Post 5: Understanding JavaScript Data Types

Introduction

In JavaScript, every value has a type, and understanding data types is essential for writing effective code. JavaScript provides different types of data, such as numbers, strings, booleans, objects, and arrays.

In this post, we’ll explore these data types with simple explanations and practical examples.


1. JavaScript Data Types Overview

JavaScript has two main categories of data types:

✅ Primitive Data Types (Store single values)

  1. String – Text values
  2. Number – Numeric values
  3. Booleantrue or false
  4. Undefined – A variable with no assigned value
  5. Null – Represents an empty or unknown value
  6. Symbol – Unique identifiers (advanced use)
  7. BigInt – Large integers (used for very big numbers)

✅ Non-Primitive (Reference) Data Types (Store complex data)

  1. Object – Collections of key-value pairs
  2. Array – Lists of values
  3. Function – Code blocks stored as values

2. Understanding Primitive Data Types

📌 String (Text Values)

A string is a sequence of characters, written inside quotes ("" or '').

let firstName = "Alice";
let message = 'Welcome to JavaScript!';
console.log(firstName);
console.log(message);
Enter fullscreen mode Exit fullscreen mode

Output:

Alice  
Welcome to JavaScript!  
Enter fullscreen mode Exit fullscreen mode

📌 Number (Numeric Values)

JavaScript numbers can be integers or decimals.

let age = 25;
let price = 99.99;
console.log(age, price);
Enter fullscreen mode Exit fullscreen mode

Output:

25 99.99  
Enter fullscreen mode Exit fullscreen mode

📌 Boolean (True/False Values)

A boolean represents either true or false.

let isLoggedIn = true;
let hasPermission = false;
console.log(isLoggedIn, hasPermission);
Enter fullscreen mode Exit fullscreen mode

Output:

true false  
Enter fullscreen mode Exit fullscreen mode

📌 Undefined (No Value Assigned)

A variable declared but not assigned a value is undefined.

let city;
console.log(city);
Enter fullscreen mode Exit fullscreen mode

Output:

undefined  
Enter fullscreen mode Exit fullscreen mode

📌 Null (Intentional Empty Value)

null is assigned when you want to indicate an empty or unknown value.

let discount = null;
console.log(discount);
Enter fullscreen mode Exit fullscreen mode

Output:

null  
Enter fullscreen mode Exit fullscreen mode

3. Understanding Non-Primitive Data Types

📌 Objects (Key-Value Pairs)

An object stores multiple values in a structured format using key-value pairs.

let person = {
    name: "John",
    age: 30,
    city: "New York"
};
console.log(person);
Enter fullscreen mode Exit fullscreen mode

Output:

{ name: 'John', age: 30, city: 'New York' }
Enter fullscreen mode Exit fullscreen mode

📌 Arrays (Lists of Values)

An array stores multiple values in an ordered list.

let colors = ["Red", "Green", "Blue"];
console.log(colors);
console.log(colors[0]); // Access first item
Enter fullscreen mode Exit fullscreen mode

Output:

["Red", "Green", "Blue"]
"Red"
Enter fullscreen mode Exit fullscreen mode

4. Checking Data Types

JavaScript provides the typeof operator to check a variable’s type.

console.log(typeof "Hello");   // String  
console.log(typeof 100);       // Number  
console.log(typeof true);      // Boolean  
console.log(typeof null);      // Object (special case)  
console.log(typeof undefined); // Undefined  
console.log(typeof { key: "value" }); // Object  
console.log(typeof [1, 2, 3]); // Object (Arrays are objects)  
Enter fullscreen mode Exit fullscreen mode

5. Practical Exercise

Try this in your script.js file:

let product = "Laptop";
let price = 899.99;
let isAvailable = true;

console.log("Product:", product);
console.log("Price:", price);
console.log("Available:", isAvailable);
Enter fullscreen mode Exit fullscreen mode

Expected Output:

Product: Laptop  
Price: 899.99  
Available: true  
Enter fullscreen mode Exit fullscreen mode

Next Steps

Now that we understand JavaScript data types, we’re ready to dive deeper into working with strings and numbers. Stay tuned for the next post! 🚀


Pro Tip:

🔹 Always use typeof to check the data type of a variable.

🔹 Strings must be inside quotes ("" or '').

🔹 Objects and arrays store multiple values.

Follow me on LinkedIn - Ridoy Hasan

Visit My Website - Webention.com

Top comments (0)