JavaScript is dynamically typed and weakly typed, meaning:
- Dynamically typed → You don’t need to declare variable types; the type is determined at runtime.
- Weakly typed → It automatically converts types when needed (type coercion).
1️⃣ Dynamically Typed Example
let data = 10; // data is a number
data = "Hello"; // now it's a string
data = true; // now it's a boolean
✅ JavaScript allows the same variable to hold values of different types without error.
2️⃣ Weakly Typed Example
console.log("5" - 2); // Output: 3 (string "5" converted to number)
console.log("5" + 2); // Output: "52" (number 2 converted to string)
⚠️ JS automatically coerces types when performing operations — this flexibility can lead to unexpected results.
💡 In Short:
JavaScript is dynamically typed because variable types are decided at runtime, and weakly typed because it allows implicit type conversions between different data types.