console.log(true);
console.log(false);
Javascript gives us a few operators for comparing values.
>=, <=, >, <
inequality===
equality!==
not equal.
log(5 > 6);
log(5 < 6);
log(5 === 6);
log(5 !== 6);
log(6 >= 6);
What do comparison operators result in?
Sometimes comparing two values is not enough. These are logical operators that are used to combine booleans.
!
not&&
logical and||
logical or.
log( 5 < 6 && 6 < 7 );
log('-------------------------');
log( !(2 === 3) );
log('-------------------------');
log( 2 > 3 || "red" === "red" );
if
statements are used for "branching" in code.if
will only be executed if the value in the parenthesis is truthy.
var x = 5;
if (x > 3) log("x is greater than 3");
If you want to run more than one statement conditionally, use a block
. That just means group them together using braces.
var x = 5;
if (x > 3) {
log("x is greater than 3");
log("display this too.");
}
I actually prefer always using braces, even if you're only putting one statement in your if
.
The if
statement has an optional else
statement that is executed if the expression given to if
is falsey.
var x = 5;
if (x > 3) {
log("x is greater than 3");
}
else {
log("x is not greater than 3");
}
You can combine if else statements to test several different conditions that are mutually exclusive
var isRobot = false;
var isHuman = true;
var isDog = false;
if (isRobot) {
log("10101010101");
}
else if (isHuman) {
log("hello!");
}
else if (isDog) {
log("woof woof");
}
else {
log("...");
}
while
loop is the most basic looping construct.
var counter = 0;
while (counter < 10) {
log(counter);
counter++;
}
log("Final value of counter: " + counter);
Here's another example:
var num = Math.random();
while (num < 0.7) {
log(num);
num = Math.random();
}
log("Greater than 0.7: " + num);
Note that this loop is an example of a loop that runs a specific number of times (20 in this case).
var counter = 0;
while (counter < 20) {
// do something
counter++; // Increment counter by 1
}
Loops like this are so common in programming, that there's a "shortcut" way of writing them.
Instead of writing:
var counter = 0;
while (counter < 20) {
// do something
counter++; // Increment counter by 1
}
You can use a for
loop, like this:
for (var counter = 0; counter < 20; counter++) {
// do something
}
The for
loop is made of three parts:
for (intialization; condition; increment) {
//code to execute
}
First the intialization expression is executed, usually to set a loop counter
In general:
for
loop if you know exactly how many times the loop will run (for example, counting up to 10).while
loop if you don't know how many times the loop will run (for example, checking the result of a random number, or user input)0, null, undefined, "", false, NaN
true
and the string 'false'
.
// Falsey. Won't show.
if (0) {
log('0 is truthy!');
}
if (null) {
log('null is truthy!');
}
if (undefined) {
log('undefined is truthy!');
}
if ("") {
log('"" is truthy!');
}
if (false) {
log('false is truthy!');
}
if (NaN) {
log('NaN is truthy!');
}
// Truthy. Will show.
if ("false") {
log('"false" is truthy!');
}
if (1234) {
log('1234 is truthy!');
}
if ("hello") {
log('"hello" is truthy!');
}
Table of Contents | t |
---|---|
Exposé | ESC |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Notes | 2 |
Help | h |