Variables, Data Types & Operators
Variables, Data Types & Operators
"If you've ever wondered how apps store your name, your score in a game, or your age — the answer is variables."
Part 1: Variables
Think of a variable as a box where you can store information. You label the box so you know what's inside — and whenever you need that information, you just use the label. In JavaScript, we create variables using three keywords: var, let, and const.
1. let — The most common one
let is used when the value can change later. The = sign means 'store this value'.
let name = "Vivek";
let age = 22;
console.log(name); // Output: Vivek
console.log(age); // Output: 22Since we used let, we can change the value later:
let city = "Chennai";
city = "Bengaluru"; // ✅ Allowed with let
console.log(city); // Output: Bengaluru2. const — When the value won't change
const is short for constant. If you try to change it, JavaScript will throw an error. Use it for things that never change, like a date of birth.
const pi = 3.14159;
const country = "India";
console.log(pi);
console.log(country);
const birthYear = 2002;
birthYear = 2003; // ❌ Error! Assignment to constant variable.3. var — The old way
var is the old way used before 2015. Today, we prefer let and const because they're safer.
var score = 100;
console.log(score); // Output: 100Naming Rules
The golden rule: make your names meaningful. age is better than x!
// ✅ Good names
let myName // camelCase — recommended
let userAge // starts with a letter
let _score // underscore is fine
let $price // dollar sign is fine
// ❌ Bad names
// let 1name // can't start with a number
// let my-name // hyphens not allowed
// let let // can't use reserved keywordsPart 2: Data Types
Every value in JavaScript has a type. Let's look at the most important ones.
String (Text)
Wrapped in single quotes, double quotes, or backticks.
let firstName = "Vivek";
let greeting = 'Hello, World!';
let template = `My name is ${firstName}`;Number
Both whole numbers and decimals.
let age = 22;
let price = 99.99;
let temperature = -5;Boolean
True or false. Like a light switch.
let isLoggedIn = true;
let hasSubscription = false;Undefined & Null
Undefined: box exists but is empty. Null: intentionally empty.
let score; // undefined
let selectedUser = null;Checking Types with typeof
console.log(typeof "Vivek"); // string
console.log(typeof 22); // number
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof null); // object <- famous JS quirk!Part 3: Operators
Operators perform actions on values.
1. Arithmetic & Assignment
let a = 10;
let b = 3;
console.log(a + b); // 13
console.log(a % b); // 1 (Remainder/Modulus)
// Assignment
let score = 10;
score += 5; // Same as score = score + 5 (15)2. Comparison
Always use === (strict equality). == ignores the type and can cause bugs!
console.log(10 > 5); // true
console.log(10 === 10); // true (strict equal)
// == vs ===
console.log(5 == "5"); // true ⚠️ Loose
console.log(5 === "5"); // false ✅ Strict3. Logical (AND, OR, NOT)
let isLoggedIn = true;
let hasSubscription = false;
console.log(isLoggedIn && hasSubscription); // false (AND)
console.log(isLoggedIn || hasSubscription); // true (OR)
console.log(!isLoggedIn); // false (NOT)4. Increment/Decrement & String Concatenation
let count = 0;
count++; // count is now 1
count--; // count is back to 0
// String Concatenation
let fName = "Vivek", lName = "Kumar";
let full = `${fName} ${lName}`; // Better with backticks!