Lesson 2 of 15

Variables

Variables

JavaScript has two modern ways to declare variables: let and const.

let

let declares a variable you can reassign:

let score = 0;
score = 10;    // OK — reassigning
score = score + 5;  // OK
console.log(score);  // 15

const

const declares a constant that cannot be reassigned:

const pi = 3.14159;
const name = "Alice";
// pi = 3;  // Error: Assignment to constant variable

Use const by default. Use let only when you need to reassign.

Types

Variables in JavaScript can hold any type:

let count = 42;          // number
let price = 9.99;        // number (no separate int/float)
let message = "hello";   // string
let active = true;       // boolean
let empty = null;        // null — intentional absence of value
let missing;             // undefined — no value assigned yet

Your Task

Declare three variables: city (string "Paris"), population (number 2161000), and capital (boolean true). Print each one.

JavaScript loading...
Loading...
Click "Run" to execute your code.