reading-notes

JavaScript Basics: Giving Orders and Doing Repetitions

Hey there! If you’ve ever wondered how computers make decisions or perform the same task a thousand times without getting tired, it all comes down to two main concepts: operators and loops. Here is a breakdown of what I’ve been learning.

1. Giving Values and Making Comparisons

In JavaScript, we use operators to handle data. Think of them like the verbs or symbols in a math sentence.

2. Loops: The Art of Repetition

Loops are like a computerized version of a simple command, such as “take five steps to the east”. They allow us to repeat an action multiple times without writing the same code over and over again.


Common Questions

What is an expression in JavaScript? At a high level, an expression is any valid unit of code that resolves to a specific value. For example, 3 + 4 is an expression because it resolves to 7.

Why would we use a loop in our code? We use loops because they offer a quick and easy way to perform the same action repeatedly. They save time and prevent us from having to write out the same command multiple times.

When does a for loop stop executing? A for loop will continue to run its instructions until its specified condition evaluates to false. Once that condition is no longer met, the loop terminates immediately.

How many times will a while loop execute? A while loop executes its statements as long as the specified condition remains true. This means it could run many times, or it could even run zero times if the condition is false from the very start.


A helpful way to think about it: Imagine you are a coach at a track. An Assignment operator is like handing a baton to a runner—you are giving them something to hold. A Comparison operator is like the photo-finish camera—it simply reports “true” or “false” on who crossed first. A for loop is telling a runner to “run exactly 4 laps,” while a while loop is telling them to “keep running as long as the sun is up.”