Readability
Anyone can look at the code and easily decipher what is part of the if statement and what is not, even if they have never seen it before. The indentation provides a visual distinction for where different statements will run.
Just like in Blocks, using ||logic:Logic||
in JavaScript allows for games that react
to user input and the state of the game.
Just like in Blocks, a boolean value can be stored as a variable in JavaScript.
let x = true
is equivalent to
let x: boolean = true;
The ||logic:if then||
and ||logic:else||
blocks were amongst the most important
in the ||logic:Logic||
category in the previous course,
and remain just as important in JavaScript.
In JavaScript, these can be expressed as:
if (condition) {
console.log("The condition is true");
}
and
if (condition) {
console.log("The condition is true");
} else {
console.log("The condition is false");
}
In these snippets, ||variables:condition||
is a boolean value
(an expression that evaluates to ||logic:true||
or ||logic:false||
).
In Blocks, these often took the form of elongated hexagons:
let x = true
In blocks, there were many ways to compare two numbers. In JavaScript, these same comparisons are available.
Name | Symbol | JavaScript |
---|---|---|
Equal To | = | value1 == value2 |
Not Equal To | ≠ | value1 != value2 |
Less Than | < | value1 < value2 |
Less Than or Equal To | ≤ | value1 <= value2 |
Greater Than | > | value1 > value2 |
Greater Than or Equal To | ≥ | value1 >= value2 |
||logic:if||
console.log
(if anything)let num: number = -5;
if (num < 0) {
console.log("num is negative!");
}
let num: number = 1;
if (num == 1) {
console.log("num is one!");
} else {
console.log("num is not one!");
}
Notice how the code above uses curly braces ({
and }
)
and indentation to separate the code that is contained within each logical block.
This is done for two major reasons:
||logic:if||
statement to check if the number is less than 2||logic:if||
section), print “Hello!” to the console||logic:else||
section), print “Goodbye!” to the consoleBoolean operators allow boolean values to be manipulated.
Name | Order | JavaScript |
---|---|---|
Not | 1st | !bool1 |
And | 2nd | bool1 && bool2 |
Or | 3rd | bool1 || bool2 |
Just like in math, the order of operations becomes important when there are multiple operators being used at once.
Evaluate the value stored in the variable ||variables:bool4||
given the following code:
let bool1: boolean = false;
let bool2: boolean = true;
let bool3: boolean = false;
let bool4: boolean = bool1 && bool2 || !bool3;
Following the order of operations, the value assigned to
||variables:bool4||
is evaluated as follows
||variables:bool3||
, which evaluates to ||logic:true||
||variables:bool1||
and ||variables:bool2||
,
which evaluates to ||logic:false||
||logic:true||
||logic:or||
||logic:false||
)
which evaluates to ||logic:true||
This results in ||variables:bool4||
storing ||logic:true||
.
This result can be changed by adding parentheses:
let bool1: boolean = false;
let bool2: boolean = true;
let bool3: boolean = false;
let bool4: boolean = bool1 && (bool2 || !bool3);
Following the order of operations, the value assigned to
||variables:bool4||
is evaluated as follows:
||variables:bool3||
, which evaluates to ||logic:true||
||variables:bool2||
or the result of step 1,
which evaluates to ||logic:true||
||variables:bool1||
and the result of step 3,
which evaluates to ||logic:false||
This results in ||variables:bool4||
storing ||logic:false||
instead.
||logic:if||
console.log
(if anything)let val1: number = 5;
let val2: number = 0;
if (val1 > 2 && val2 == 0) {
console.log("The condition is true");
}
This expression can be evaluated by hand by breaking it into smaller subproblems. The animation below shows it being broken up into single comparisons, piece by piece.
let num: number = 5;
let bool: boolean = true;
if (num > 2 || bool) {
console.log("The condition is true");
}
||variables:a||
stores 7
||variables:b||
stores 8
||logic:if||
statement to perform two comparison on these variable||variables:a||
is less than 4||variables:b||
is not equal to 9||logic:or||
(||
)||logic:true||
, print “You got it!” to the console||logic:false||
, print “Try again!” to the consolebool1
and bool2
. When will they be the same, when will they be different?!bool1 && !bool2
!(bool1 || bool2)