Lesson notes
condition formally looks like this:
IF (condition) THEN
do something
IF (other_condition) THEN
do some other thing
IF (none of those conditions) THEN
do something else
JavaScript function that accepts a number and returns its absolute value:
const abs = (num) => {
if (num > 0) {
return num;
} else if (num < 0) {
return -num;
} else {
return 0;
}
}
Conditions can be either true
or false
. For example, (num > 0)
is true when num is 9 or 15, but it's false when num is -18 or, say, 0.
Things that answer TRUE or FALSE are called predicates.
Other math predicates in JavaScript include:
===
!==
>
<
>=
<=
Examples:
512 === 512; // true
512 === 988; // false
512 !== 378; // true
512 !== 512; // false
512 > 500; // true
512 > 689; // false
512 < 900; // true
512 < -30; // false
512 >= 512; // true
512 >= 777; // false
512 <= 512; // true
512 <= 600; // true
512 <= 5; // false
AND (&&
):
A | B | A AND B |
---|---|---|
TRUE | TRUE | TRUE |
TRUE | FALSE | FALSE |
FALSE | TRUE | FALSE |
FALSE | FALSE | FALSE |
OR (||
):
A | B | A OR B |
---|---|---|
TRUE | TRUE | TRUE |
TRUE | FALSE | TRUE |
FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE |
NOT (!
):
A | NOT A |
---|---|
TRUE | FALSE |
FALSE | TRUE |
Alternative way of implementing abs
function:
const abs = (num) => {
if (num === 0 || num > 0) {
return num;
} else {
return -num;
}
}
Think of another way of implementing the same function using >=
sign.
Same function means "the function behaves the same way from the outside, but the insides (the implementation) can be different".
Recommended watching
- AND OR NOT - Logic Gates Explained - Computerphile
- See How Computers Add Numbers In One Lesson
Optional
JavaScript and many other languages also have a short-hand version of the if statement: it's called conditional or ternary operator:
condition ? expression : expression
Here there is only one condition and two options: one for true and one for false.
const absValue = (num === 0 || num > 0) ? num : -num;
We create an absValue
and assign it a value. This value depends on the condition: if condition is true, then num
is used, otherwise -num
is used.
Lesson transcript
So far we've been using our programs as glorified calculators. Of course, they can do more, and the next big idea is to make computers make decisions based on some information.
Check this out: in mathematics there is a concept of absolute value. This is how it's defined:
Don't worry, it's simple: if number is positive, then its absolute value is the same as the number; if number is negative, then its absolute value is the negation of the number. Basically, drop the minus if it's there and that's it.
One way to think of it is: it's the distance from zero.
Imagine you want to describe a black box — a function — that accepts a number and returns its absolute value. You'll need to make a rule inside the box, something like this:
IF number is greater than 0 THEN
return number
IF number is less than 0 THEN
return -number
IF number is 0 THEN
return 0
This is a condition, and formally it looks like this:
IF (condition) THEN
do something
IF (other_condition) THEN
do some other thing
IF (none of those conditions) THEN
do something else
Let's make our function for real now:
const abs = (num) => {
if (num > 0) {
return num;
} else if (num < 0) {
return -num;
} else {
return 0;
}
}
This function has one argument — it accepts one thing from the outside.
Then we have the if
keyword, then goes the condition in brackets, then — a block of instructions that will be running if that condition is met. Next is another condition with else if
. else if
means "if previous condition is not met but this new condition IS met, then do this next block of instructions".
There can be multiple "else if" blocks, sometimes you have many options.
So now, after we've taken care of positive and negative numbers, there is one more option left: what if number is zero. Note that we don't check for zero explicitly, we just say else
. It means "if none of the conditions above are met, then do this next block of instructions". You can safely think that if number is not positive and not negative, it can only be zero. But sometimes we make mistakes when thinking about conditions and options, and many problems in programming come from incorrect conditions.
These conditions in brackets are things that can be either true
or false
. For example, (num > 0)
is true when num is 9 or 15, for example, but it's false when num is -18 or, say, 0.
As you see, greater than
and less than
math symbols kind of give answers — YES or NO, TRUE or FALSE. There are other things that can give TRUE or FALSE answers:
===
!==
>
<
>=
<=
And here are few more examples:
512 === 512; // true
512 === 988; // false
512 !== 378; // true
512 !== 512; // false
512 > 500; // true
512 > 689; // false
512 < 900; // true
512 < -30; // false
512 >= 512; // true
512 >= 777; // false
512 <= 512; // true
512 <= 600; // true
512 <= 5; // false
The branch of math that studies these true and false statements is called Boolean algebra. In general, statements of any nature, not just number systems, can be true or false. For example, "I am a human being" is true, while "China is located in South America" is false.
JavaScript has true
and false
values, and you can actually use them in conditions. For example, you can say if true
, and this condition will always be met — because true
is always true
.
There are many aspects and details in Boolean algebra, but in programming we are mostly concerned about three basic operations: AND, OR, NOT.
AND is when you want two conditions to be true. "I am a human being AND horses eat grass" is true, because both statements are true. "I am a human being AND pigs can fly" is false, becase one statement is true, but other one is false, so together, this whole thing joined by AND is false.
The symbol for AND is double ampersand &&
. And this is the so called truth table for AND, it's like a cheat sheet:
A | B | A AND B |
---|---|---|
TRUE | TRUE | TRUE |
TRUE | FALSE | FALSE |
FALSE | TRUE | FALSE |
FALSE | FALSE | FALSE |
So, only TRUE AND TRUE give TRUE, and all other combinations include FALSE, so they result is FALSE.
OR is when you want at least one condition to be true. Following the same examples, "I am a human being OR horses eat grass" is true. "I am a human being OR pigs can fly" is also true. Even though pigs don't fly, I AM a human being, so one of the two statements is true, which makes this whole thing joined by OR to be true.
The symbol for OR is two vertical bars ||
.
A | B | A OR B |
---|---|---|
TRUE | TRUE | TRUE |
TRUE | FALSE | TRUE |
FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE |
If there is true, then the result is true.
NOT is simple — it's the opposite. NOT true is false, NOT false is true. "NOT pigs can fly" is true, because "pigs can fly" is false, and NOT false is true.
The symbol for NOT is an exclamation mark !
.
A | NOT A |
---|---|
TRUE | FALSE |
FALSE | TRUE |
In programming there are usually multiple ways to do the same thing. We can re-write our absolute value function differently and get the same result. Let's try to do it using these new ideas:
const abs = (num) => {
if (num === 0 || num > 0) {
return num;
} else {
return -num;
}
}
So now we have only two conditions:
- If number is zero OR number is greater than 0, then return the number.
- In any other case return -number.
You can think of yet another way of writing the same function using "greater than or equal" sign, for example. When I say "same function", I mean that the function behaves the same way from the outside, but the insides — the implementation, can be different.
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.