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)
- String – Text values
- Number – Numeric values
-
Boolean –
true
orfalse
- Undefined – A variable with no assigned value
- Null – Represents an empty or unknown value
- Symbol – Unique identifiers (advanced use)
- BigInt – Large integers (used for very big numbers)
✅ Non-Primitive (Reference) Data Types (Store complex data)
- Object – Collections of key-value pairs
- Array – Lists of values
- 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);
Output:
Alice
Welcome to JavaScript!
📌 Number (Numeric Values)
JavaScript numbers can be integers or decimals.
let age = 25;
let price = 99.99;
console.log(age, price);
Output:
25 99.99
📌 Boolean (True/False Values)
A boolean represents either true
or false
.
let isLoggedIn = true;
let hasPermission = false;
console.log(isLoggedIn, hasPermission);
Output:
true false
📌 Undefined (No Value Assigned)
A variable declared but not assigned a value is undefined
.
let city;
console.log(city);
Output:
undefined
📌 Null (Intentional Empty Value)
null
is assigned when you want to indicate an empty or unknown value.
let discount = null;
console.log(discount);
Output:
null
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);
Output:
{ name: 'John', age: 30, city: 'New York' }
📌 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
Output:
["Red", "Green", "Blue"]
"Red"
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)
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);
Expected Output:
Product: Laptop
Price: 899.99
Available: true
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)