💡 Day 2 — Variables, Constants & Data Types
Welcome back, Rahul! 🎯 Now that you know what JavaScript is, let’s explore the **core foundation of programming** — storing and managing data using variables, constants, and data types.
📦 1️⃣ What Are Variables?
Variables are containers that store data in memory so you can use or modify it later. In JavaScript, variables are declared using:
var— old way (function-scoped)let— modern way (block-scoped)const— for values that never change
var name = "Krishna"; let age = 23; const country = "India"; console.log(name, age, country);
🔍 2️⃣ Naming Rules
- Must start with a letter, underscore (_), or $.
- Cannot start with a number.
- Case-sensitive (
Name≠name).
🧮 3️⃣ Data Types in JavaScript
JavaScript has **primitive** and **non-primitive** data types.
Primitive Types:
- String → text (
"Hello") - Number → numeric values (
42,3.14) - Boolean → true or false
- Undefined → declared but not assigned
- Null → intentional empty value
let user = "krishna"; let score = 95; let isPassed = true; let grade; let certificate = null; console.log(typeof user); // string console.log(typeof score); // number console.log(typeof isPassed); // boolean console.log(typeof grade); // undefined console.log(typeof certificate); // object
🧩 4️⃣ String Interpolation (Template Literals)
Use backticks (`) and placeholders (${}) to embed variables directly inside strings.
let name = "Rahul";
let age = 23;
console.log(`My name is ${name} and I am ${age} years old.`);
🧠 5️⃣ Dynamic Typing
JavaScript is a dynamically typed language — meaning you don’t need to specify data types; they’re assigned automatically.
let value = 10; console.log(typeof value); // number value = "Ten"; console.log(typeof value); // string
🧮 6️⃣ Practice Task
- Create three variables for your name, age, and city.
- Use template literals to print them in a full sentence.
- Try changing variable values and observe output.
🎯 Summary
Today, you learned how to store, change, and describe data in JavaScript. Next, we’ll explore **Operators & Expressions** — where you start performing real logic and calculations.
Comments
Post a Comment