reading-notes

Operators and Loops

Notes

Operators

JavaScript has both binary and unary operators, and one special ternary operator, the conditional operator. A binary operator requires two operands, one before the operator and one after the operator:

For example, 3 + 4 or x * y. This form is called an infix binary operator, because the operator is placed between two operands. All binary operators in JavaScript are infix.

A unary operator requires a single operand, either before or after the operator:

For example, x++ or ++x. The operator operand form is called a prefix unary operator, and the operand operator form is called a postfix unary operator. ++ and – are the only postfix operators in JavaScript — all other operators, like !, typeof, etc. are prefix. [^1]

Assignment Operators

Simple Ex: x = f()

Many compound assignment operators

Name Shorthand operator Meaning

[^1]

If an expression evaluates to an object, then the left-hand side of an assignment expression may make assignments to properties of that expression. [^1]

Comparison Operators

Logical Operators

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. The logical operators are described in the following table.

Operator Usage Description

The following code shows examples of the && (logical AND) operator.

The following code shows examples of the   (logical OR) operator.

The following code shows examples of the ! (logical NOT) operator.

Loops

For Loop

“when there is a preset number of times that loop is going to run. That is a for loop. whether it’s one or a 1 million. It’s gonna stop when you want it to.” - Kassie

Structure:

for (initial value; condition must be true; increment/decrement){ code to execute }

let students = 13;

for (let i = 0; i < 5; i++){ console.log(‘hello to student #’, i); }

While loop

“a ‘while loop’ is something you want to use when you don’t know how many times it’s going to take. So if you are trying to get input, from your user and you want to keep asking them until they give you an acceptable value. That is a while, loop. You don’t know how many times it’s going to take. Passwords are a ‘while’ loop”. - Kassie

Structure: while(this is true){execute this code}

EX:

while (5 < 10){ Console.log('infinite looooop) }

References

  1. “Expressions and operators” developer.mozilla.org

  2. “Loops and iteration” developer.mozilla.org

Answers

  1. What is an expression in JavaScript?
    • an expression is a valid unit of code that resolves to a value
  2. Why would we use a loop in our code?
    • Loops offer a quick and easy way to do something repeatedly
  3. When does a for loop stop executing?
    • a specified condition evaluates to false
  4. How many times will a while loop execute?
    • A while statement executes its statements as long as a specified condition evaluates to true.