JavaScript Lesson 2: Variables (var, let, const)
Variables store data. JavaScript has three ways to declare them. Knowing which one to use matters — and it’s simpler than it sounds.
let — For Values That Change
let age = 25;
let score = 0;
let userName = "Alice";
// Can change the value
age = 26;
score = score + 10;
console.log(age); // 26
console.log(score); // 10
const — For Values That Never Change
const PI = 3.14159;
const MAX_USERS = 100;
const SITE_NAME = "BeginnerCoder";
// PI = 3.14; // ERROR! Cannot reassign a const
// Best practice: use const by default.
// Only use let when the value will change.
var — The Old Way (Avoid It)
var oldWay = "I am var";
// var has weird scoping issues. Do not use.
// Always prefer let or const in modern JS.
Rules for Variable Names
// JavaScript uses camelCase (not snake_case like Python)
let firstName = "Alice"; // camelCase
let totalScore = 100;
let isLoggedIn = true;
// Rules:
// - Start with letter, $, or _
// - No spaces
// - Case sensitive (name !== Name)
let name = "Alice";
let Name = "Bob"; // different variable!
console.log() — Your Best Friend
let x = 10;
let y = 20;
let sum = x + y;
console.log(sum); // 30
console.log("Sum is:", sum); // Sum is: 30
console.log(x, y, sum); // 10 20 30
// Use template literals (backtick strings)
console.log(`${x} + ${y} = ${sum}`); // 10 + 20 = 30
🏋️ Practice Task
In your browser console or a JS file: create a const for your name, a let for your current score (start at 0), and a let for your level (start at 1). Add 50 to score. Add 1 to level. Print all three with console.log.
💡 Hint: Use const name = “…” and let score = 0. Then score += 50 and level += 1.