| JavaScript Variables and Data Types :👈 | 👉:Javascript-Operators |
JavaScript Data Types |
🚀 What You’ll Learn
In this lesson, you’ll learn:
- What are data types and how to use them in JavaScript.
- JavaScript data types (primitive & reference).
- How type conversion works in JS.
🟡 JavaScript Data Types
Primitive Types
In JavaScript, Primitive Types are the most basic data types. They represent single values (not collections) and are immutable (cannot be changed directly).
1️⃣ String
A sequence of characters enclosed in quotes.
let name = "Alice";
console.log(name); // "Alice"
2️⃣ Number
Represents both integers and floating-point numbers.
let age = 25;
let price = 99.99;
console.log(age); // 25
console.log(price); // 99.99
3️⃣ Boolean
Represents logical values: true or false.
let isStudent = true;
let isAdmin = false;
console.log(isStudent); // true
4️⃣ Undefined
A variable declared but not assigned any value is undefined.
let city;
console.log(city); // undefined
5️⃣ Null
Represents an intentional absence of any value.
let car = null;
console.log(car); // null
6️⃣ Symbol (ES6)
Used to create unique identifiers.
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // false (always unique)
7️⃣ BigInt
Used for very large integers beyond the safe limit of Number.
let bigNum = 123456789012345678901234567890n;
console.log(bigNum);
✅ Quick Recap
- String → Textual data, e.g. "Hello".
- Number → Numeric values, e.g. 42 or 3.14.
- Boolean → Logical values: true or false.
- Null → Represents "no value" or "empty".
- Undefined → A variable that has been declared but not assigned a value.
- Symbol → A unique and immutable identifier (used for object properties).
- BigInt → For representing integers larger than 2^53 - 1.
Reference Types
- Objects → Store data as key-value pairs.
- Arrays → Store ordered lists of values.
- Functions → Reusable blocks of code that perform actions.
1️⃣ Objects
An Object in JavaScript is a collection of key-value pairs. Keys are called properties, and values can be any data type (string, number, array, function, etc.).
// Example of an object
let user = {
name: "Alice",
age: 25,
isStudent: true
};
console.log(user.name); // Alice
console.log(user.age); // 25
2️⃣ Arrays
An Array is an ordered list of values. Each value is called an element, and each element has an index (starting from 0).
// Example of an array
let fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // apple
console.log(fruits[2]); // cherry
3️⃣ Functions
A Function is a block of code designed to perform a task. Functions can take inputs (parameters) and return outputs (values).
// Example of a function
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice")); // Hello, Alice!
🔄 JavaScript Type Conversion
There are two main types of conversion:
- Implicit Conversion (Type Coercion) → JavaScript automatically converts types.
- Explicit Conversion (Type Casting) → You manually convert types using functions.
- Be careful with
==because it does coercion. Prefer===for strict comparison.
1️⃣ Implicit Conversion (Type Coercion)
JavaScript automatically converts one data type to another when needed.
// String + Number → String
console.log("5" + 2); // "52"
// String - Number → Number
console.log("5" - 2); // 3
// Boolean + Number → Number
console.log(true + 1); // 2
// Equality operator (==) does coercion
console.log(5 == "5"); // true
2️⃣ Explicit Conversion (Type Casting)
We manually convert values using built-in functions.
Convert to Number
console.log(Number("123")); // 123
console.log(parseInt("12.34")); // 12
console.log(parseFloat("12.34"));// 12.34
console.log(+"5"); // 5 (unary plus)
Convert to String
console.log(String(123)); // "123"
console.log((123).toString()); // "123"
console.log(true.toString()); // "true"
Convert to Boolean
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean("Hello")); // true
console.log(Boolean("")); // false
3️⃣ Special Cases
// Null and Undefined
console.log(Number(null)); // 0
console.log(Number(undefined)); // NaN
// Empty string
console.log(Number("")); // 0
// Non-numeric string
console.log(Number("abc")); // NaN
👉 Next Lesson
In the next lesson, we’ll learn about Operators in JavaScript (Arithmetic, Comparison, Logical).
| JavaScript Variables and Data Types :👈 | 👉:Javascript-Operators |