Lesson Goals
- Learn about the different operators and their functions.
- Become familiar with JavaScript syntax.
Arithmetic Operators
If variable a holds 10 and variable b holds 15, then:
Operator | Operation | Description | Expression | Result |
---|---|---|---|---|
+ | addition | adds two operands | a + b |
25 |
- | subtraction | subtracts second operand from the first | a - b |
-5 |
* | multiplication | multiplies the operands | a * b |
150 |
/ | division | divides first operand by second operand | b / a |
1 |
% | modulus (remainder) | returns the remainder after integer division | b % a |
5 |
Increment and Decrement Operators
If variable a holds the value 42, then:
Operator | Operation | Description | Expression | Equivalent to... | a 's value afterwards |
---|---|---|---|---|---|
++ | increment | adds one to the value its int operand holds |
a++ |
a = a + 1 |
43 |
‐‐ | decrement | subtracts one from the value its int operand holds |
a-- |
a = a - 1 |
41 |
Loose Equality Operators
If variable a holds 10, then:
Operator | Operation | Description | Expression | Result |
---|---|---|---|---|
== | equal to | evaluates true if the two values are equal | a == 10 |
a == '10'
a == 11
a == '11'
| true
true
false
false != | not equal to | evaluates true if the two values are not equal | a != 10
a != '10'
a != 11
a != '11'
| false
false
true
true
Strict Equality Operators
If variable a holds 10, then:
Operator | Operation | Description | Expression | Result |
---|---|---|---|---|
== | strict equal to | evaluates true if the two values and types are equal | a === 10 |
|
a === '10' |
true |
false != | strict not equal to | evaluates true if the two values and types are not equal | a !== 10
a !== '10'
| false
true
Relational Operators
If variable a holds 10 and variable b holds 15, then:
Operator | Operation | Description | Expression | Result |
---|---|---|---|---|
> | greater than | evaluates to true of the first operand is greater than the second operand | a > b |
false |
>= | greater than or equal to | evaluates to true of the first operand is greater than or equal to the second operand | a >= b |
|
a >= 10 |
false |
true < | less than | evaluates to true of the first operand is less than the second operand | a < b
| true <= | less than or equal to | evaluates to true of the first operand is less than or equal to the second operand | a <= b
a <= 10
| true
true
Logical Operators
If variable a holds true
and variable b holds false
, then:
Operator | Operation | Description | Expression | Result |
---|---|---|---|---|
&& | conditional AND | evaluates to true if both operands are true; |
||
otherwise, evaluates to false | a && b |
|||
a && true |
false |
true || | conditional OR | evaluates to true if either operand is true;
otherwise, evaluates to false | a | | b
b | | false
| true
false
Translating English to JavaScript
Let's translate English statements into JavaScript. First, we'll do one together.
Start by writing the statements as comments. Here's an example:
// Jessica is 23 years old.
const jessicaAge = 23;
// Sam is 47.
const samAge = 47;
// Jessica is younger than Sam.
console.log(jessicaAge < samAge);
Let's break it down...
Assigning a value for Jessica's age:
The word "is" means equals. How do we represent assignment?
// Jessica is 23 years old.
If Jessica "is 23", then Jessica's age = 23:
const jessicaAge = 23;
Comparing Jessica's and Sam's ages:
To say Jessica is younger than Sam means we are comparing their ages. Which operator would we use to compare them?
// Jessica is younger than Sam.
We use the "less than" relational operator to perform this comparison:
console.log(jessicaAge < samAge);
Let's look at the syntax
The statement:
const jessicaAge = 23;
reads as "Jessica's age is 23."
log
is a method, so it ends with a set of opening and closing parentheses:
console.log(jessicaAge < samAge)
Inside the parentheses, we find the method arguments. For the log
method, this is what we want to print. In this case, that's whether Jessica is younger than Sam.
Your turn! Try translating these word problems into JavaScript.
- Lisa is cooking muffins. The recipe calls for 7 cups of sugar. She has already put in 2 cups. How many more cups does she need to put in?
- At a restaurant, Mike and his three friends decided to divide the bill evenly. If each person paid $13 then what was the total bill?
- How many packages of diapers can you buy with $40 if one package costs $8?
- Last Friday Trevon had $29. Over the weekend he received some money for cleaning the attic. He now has $41. How much money did he receive?
- Last week Julia ran 30 miles more than Pranav. Julia ran 47 miles. How many miles did Pranav run?
- How many boxes of envelopes can you buy with $12 if one box costs $3?
- After paying $5.12 for a salad, Norachai has $27.10. How much money did he have before buying the salad?
Just keep coding! Just keep coding!
- 331 students went on a field trip. Six buses were filled and 7 students traveled in cars. How many students were in each bus?
- Aliyah had $24 to spend on seven pencils. After buying them she had $10. How much did each pencil cost?
- The sum of three consecutive numbers is 72. Print these numbers consecutively to the console.
- The sum of three consecutive even numbers is 48. What is the smallest of these numbers?
- Maria has boxes. She buys seven more. A week later, half of her boxes are destroyed in a fire, leaving her with 22 boxes. With how many did she start?