Unit 2 of 20

Variables & data types (let, const, strings, numbers, booleans)

Updated Jun 2026

Imagine you’re building a quiz app. You need to keep track of the player’s score — it starts at zero, goes up when they get a question right, and gets displayed at the end.

How does your program remember that number as it changes? That’s exactly what variables are for.

A variable is a name you give to a piece of information so your code can hold onto it, use it, and update it later.

In JavaScript, you create a variable using either let or const:

  • Use const when the value stays the same — like the name of the game, which never changes.
  • Use let when the value will change — like a score that goes up as the game progresses.

Concept 1 — Declaring a variable

To create a variable in JavaScript, you use the keyword let followed by a name of your choosing:

let score;

This tells JavaScript: “remember something called score.” The variable exists now, but it isn’t holding any value yet.

Concept 2 — Assigning a value

To give a variable a value, you use the symbol =

let score = 0;

In JavaScript, = means “set this to” — not “equals” in the mathematical sense. You’re not making a comparison, you’re storing a value.

Concept 3 — let vs const

JavaScript gives you two keywords for declaring variables: let and const. The difference comes down to whether the value will change as your application runs.

let score = 0;             // will change as the game progresses
const gameName = "Quiz App";  // never changes
  • Use const by default.
  • Switch to let only when you know the value will need to change.

If you try to reassign a const variable, JavaScript will throw an error:

const gameName = "Quiz App";
gameName = "Other App";  // ERROR: cannot reassign a const

But if you do the same for a let variable, JavaScript will not complain:

let score = 0;
score = 10;  // No error

You may come across a third keyword — var — in older JavaScript code. It works differently from let and const and can cause subtle bugs. In modern JavaScript, var is best avoided entirely.

Concept 4 — Using and printing variables

Once a variable has a value, you can use it anywhere in your code just by writing its name. For instance, console.log(variable_name) is going to print variable_name to the console. In the example below, the first line declares a variable with the name “score” and assigns it the value of 10. The second line displays the value that is stored in the variable whose name is “score”, hence displays 10.

let score = 10;
console.log(score);

Concept 5 — Numbers

JavaScript has one number type for both integers and decimals.

const age = 25;
const price = 9.99;
const total = age + price; // 34.99

Basic arithmetic works exactly as you’d expect:

console.log(10 + 3);  // 13
console.log(10 - 3);  // 7
console.log(10 * 3);  // 30
console.log(10 / 3);  // 3.333...
console.log(10 % 3);  // 1  (remainder / modulo)
console.log(10 ** 2); // 100 (exponent)

Watch out: adding a number and a string gives you a string, not a number. "5" + 3 produces "53", not 8. This is one of JavaScript’s famous quirks.

Concept 6 — Strings

A string is a piece of text. Wrap it in single quotes or double quotes.

const greeting = "Hello";
console.log(greeting);

const language = 'JavaScript';
console.log(language);

… or backticks. Backticks (template literals) are the most powerful — they let you embed variables directly into a string using ${}

const message = `Welcome to ${language}!`;
console.log(message); // Welcome to JavaScript!

Useful string tricks:

const word = "hello";
console.log(word.length);        // 5
console.log(word.toUpperCase()); // HELLO
console.log(word.includes("ell")); // true

Concept 7 — Booleans

A boolean is simply true or false. Booleans are the result of comparisons and drive all decision-making in JavaScript.

const isLoggedIn = true;
const hasSubscription = false;

console.log(5 > 3);   // true
console.log(5 === 3); // false
console.log(5 !== 3); // true

Concept 8 — null, undefined, and typeof

Two special values represent “nothing”:

  • null — intentionally empty. You set this on purpose.
  • undefined — unintentionally empty. A variable declared but not yet assigned a value.
let score;           // undefined — declared but no value yet
let winner = null;   // null — intentionally set to nothing

Use typeof to check what type a value is:

console.log(typeof "hello");    // string
console.log(typeof 42);         // number
console.log(typeof true);       // boolean
console.log(typeof undefined);  // undefined
console.log(typeof null);       // object (a known JS quirk)

Try it yourself

Open your browser console (F12) and try these:

const firstName = "Sam";
const age = 28;
const isStudent = true;

console.log(`${firstName} is ${age} years old.`);
console.log(`Student: ${isStudent}`);
console.log(typeof firstName);
console.log(typeof age);

Quick reference

TypeExampleNotes
String"hello", 'hi', `hi ${name}`Text
Number42, 3.14Integers and decimals
Booleantrue, falseYes/no, on/off
nullnullIntentionally empty
undefinedundefinedNot yet assigned
letlet x = 1Reassignable variable
constconst x = 1Fixed variable