JavaScript Variables & Primitive Data Types 🖥️🚀

JavaScript Variables & Primitive Data Types 🖥️🚀
1️⃣ Declaring Variables: var, let, and const
In JavaScript, variables are used to store data values. There are three ways to declare variables:
✅ 1. var (Function-scoped, outdated)
Can be re-declared and updated.
Not recommended in modern JavaScript.
var name = "Alice";
console.log(name); // Alice
var name = "Bob"; // Allowed (Re-declaration)
console.log(name); // Bob
✅ 2. let (Block-scoped, recommended)
Cannot be re-declared in the same scope.
Can be updated.
let age = 25;
console.log(age); // 25
age = 30; // Allowed (Updating the value)
console.log(age); // 30
// let age = 35; ❌ ERROR: Cannot redeclare in the same scope
✅ 3. const (Block-scoped, immutable)
Cannot be re-declared or updated.
Must be initialized when declared.
const country = "USA";
console.log(country); // USA
// country = "Canada"; ❌ ERROR: Cannot reassign a const variable
📌 Best Practice:
Use const by default unless you need to change the value.
Use let only when reassignment is necessary.
Avoid var in modern JavaScript.
2️⃣ JavaScript Primitive Data Types
JavaScript has 7 primitive data types, meaning they store values directly in memory.
Data Type Example Description
String "Hello" Text values enclosed in quotes.
Number 42, 3.14 Integers and floating-point numbers.
Boolean true, false Represents truthy or falsy values.
Null null Intentionally empty value.
Undefined undefined Variable declared but not assigned a value.
Symbol Symbol("id") Unique identifiers (used for object properties).
BigInt 1234567890123456789n Handles very large numbers.
1️⃣ Strings (Text Data)
let greeting = "Hello, World!";
console.log(greeting); // "Hello, World!"
2️⃣ Numbers (Integers & Floats)
let age = 30;
let price = 9.99;
console.log(age, price); // 30, 9.99
3️⃣ Booleans (True/False)
let isLoggedIn = true;
console.log(isLoggedIn); // true
4️⃣ Null (Intentional Empty Value)
let user = null;
console.log(user); // null
5️⃣ Undefined (Uninitialized Variable)
let score;
console.log(score); // undefined
6️⃣ Symbols (Unique Identifiers)
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // false (Symbols are always unique)
7️⃣ BigInt (Large Numbers)
Used for numbers larger than 2^53 - 1.
let bigNumber = 9007199254740991n;
console.log(bigNumber); // 9007199254740991n
🎯 Summary: Best Practices
✅ Use const whenever possible, let when reassigning values, and avoid var.
✅ Understand primitive data types to avoid unexpected behavior in JavaScript.
✅ Use null to explicitly clear a variable and undefined for uninitialized values.
Date: 2025-03-29 00:00:00.000000