CoursePart 1 — JavaScript for PlaywrightLesson 1.3
Lesson 1.3Part 1 — JavaScript for Playwright

let and const

Storing information with the right keyword — and why we skip var.

Lesson 1.3of 24
Next

Step 1

The idea

01

Imagine two kinds of notepads:

A whiteboard — you write something, then erase it and write something else. The value can change.

Permanent marker on a wall — once written, it stays. You can read it any time, but you can't change it.

In JavaScript, let is the whiteboard and const is the permanent marker. Both store information. The difference is whether the value can change later.

We skip var entirely — it's the old way from 1995 and has confusing behaviour. Nobody writes new code with it.

Step 2

See it run

02
Output
Priya
Hyderabad
Beginner
variables.js
const name = "Priya";      // won't change — use const
let city = "Hyderabad";    // might change — use let
let level = "Beginner";    // might change as they learn

console.log(name);
console.log(city);
console.log(level);

// Later in the program, city can change:
city = "Mumbai";
console.log(city);         // Mumbai
Terminal
$ node variables.js

Step 3

Now you try

03

Copy the file above. Then try to change the const value:

js
const name = "Priya";
name = "Rahul";             // ← try adding this line

Run it. You'll see:

Output
TypeError: Assignment to constant variable.

That error is JavaScript protecting you — you declared something as permanent and then tried to change it. Now delete that line and run again. No error.

💡Tip
The error message itself is the lesson. JavaScript tells you exactly what went wrong.

Step 4

Why a tester cares

04

In any real program, you use const for things that are fixed and let for things that update. Here's a retry counter — a common pattern in automation scripts:

retry.js
const MAX_RETRIES = 3;   // const — this limit never changes
let attempts = 0;         // let — this increases each time
let success = false;      // let — flips to true when done

while (attempts < MAX_RETRIES && !success) {
  attempts++;
  console.log(`Attempt ${attempts}...`);
  if (attempts === 2) {
    success = true;  // simulating a successful attempt
  }
}

console.log(`Finished after ${attempts} attempt(s). Success: ${success}`);
Terminal
$ node retry.js

Notice how MAX_RETRIES is a great use of const — you set it once and it never changes. attempts and success are let because they change as the program runs.

Recap

3 key points
  • 1const = permanent marker. Declare once, never reassign. Use it for most things.
  • 2let = whiteboard. Can be reassigned later. Use it when the value needs to change.
  • 3Never use var — it has confusing scoping rules that cause real bugs.