Tutelage

Lessons › coding and computer science · taught by Leo

JavaScript basics: variables, if/else, and loops

Learn how JavaScript runs step by step, stores values, makes decisions, and repeats actions with loops.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

JavaScript runs step by step

JavaScript is the language of actions on a web page. It tells the browser what to do, one step at a time.

The big idea is simple: you write instructions, the computer reads them, and then something changes on the page.

Picture code as a recipe. The browser follows the recipe in order.

Predict what JavaScript does first

If JavaScript sees two lines of code, what does it do first?

  1. The second line
  2. The first line
  3. Both at once
Show answer

The first line: Yes. JavaScript usually reads code from top to bottom, one step at a time.

Variables store information

A variable is a named container for information. It helps you save a value and use it later.

Here, score is the name, and 5 is the value inside it.

You can think of the name as the label on the box.

let score = 5

A worked variable example

Let's build one example slowly. Start with a variable named points and give it 3.

Next, add 2 to the current value. That means points becomes 5.

Now the computer stores 5 in points.

Finally, console dot log prints the value so you can see it.

What changes the variable now

After let points = 3, what does points = points + 2 do?

  1. It changes points to 5
  2. It changes points to 3
  3. It deletes points
Show answer

It changes points to 5: Correct. It takes the old value, adds 2, and stores the new result.

Why assignment is not a math equation

In math, equals means two sides are the same. In JavaScript, the equal sign often means assign this value.

That difference is huge. JavaScript can update a variable over time.

So when you see one equal sign, think store it here.

=

If and else choose a path

Think of an if statement like a fork in the road.

If something is true, one block runs. If not, the else block runs.

This is how programs make decisions.

The condition is the question, and the code block is the action.

Condition decides the path

First the condition is checked.

If it is true, JavaScript follows the yes path.

If it is false, it follows the no path.

Then it runs the matching action.

Choose the branch

If a condition is false, what happens in an if-else statement?

  1. The if block runs
  2. The else block runs
  3. Nothing can run
Show answer

The else block runs: Exactly. False sends the program to the else side.

Loops repeat a pattern

A loop is for repeating code without rewriting it many times.

The program checks a condition, runs the block, then checks again.

That is why loops are so useful for counts and repeated actions.

while condition

A tiny loop example

Here is a small loop. It starts at 1 and keeps going while n is less than or equal to 3.

Inside the loop, n increases by 1. That update is what makes the loop move forward.

Without the update, the loop could run forever.

What prevents an endless loop

Inside a loop, what usually has to change?

  1. The condition must never change
  2. A value must move toward stopping
  3. The computer must guess
Show answer

A value must move toward stopping: Yes. Something inside the loop has to change so the condition can become false.

Keep these three ideas together

Here is the short checklist. Variables store values.

If statements make decisions.

Loops repeat actions until a stop condition is reached.

If you remember those three jobs, you already know a lot of JavaScript.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app