"A" > "a"
//=> false
"b" > "a"
//=> true
12 > "12"
//=> false
12 >= "12"
//=> true
"dog" == "dog";
//=> true
1 == true;
//=> true
1 === true;
//=> false
true === true;
//=> true
"hello" === "hello"
//=> true
There are also != and !== operators, which are the negative versions of == and ===.
Does the value value coerces to true when evaluated in a boolean context.
function logTruthiness (val) {
if (val) {
console.log("Truthy!");
} else {
console.log("Falsy.");
}
}
// Outputs: "Truthy!"
logTruthiness(true);
// Outputs: "Truthy!"
logTruthiness({});
// Outputs: "Truthy!"
logTruthiness([]);
// Outputs: "Truthy!"
logTruthiness("some string");
// Outputs: "Truthy!"
logTruthiness(3.14);
// Outputs: "Truthy!"
logTruthiness(new Date());
// Outputs: "Falsy."
logTruthiness(false);
// Outputs: "Falsy."
logTruthiness(null);
// Outputs: "Falsy."
logTruthiness(undefined);
// Outputs: "Falsy."
logTruthiness(NaN);
// Outputs: "Falsy."
logTruthiness(0);
// Outputs: "Falsy."
logTruthiness("");
!!1
//=> true
!!0
//=> false
!!-1
//=> true
!![]
//=> true
!!{}
//=> true
!!null
//=> false
!!""
//=> false
true && true
//=> true
true && false
//=> false
false && false
//=> false
true || false
//=> true
false || true
//=> true
false || false
//=> false
!(true)
//=> false
if (expr) { code }
if (1 > 0) {
console.log("hi");
}
//=> hi
if (expr) { code } else { other code }
if (0 > 1) {
console.log("hi");
} else {
console.log("bye");
}
//=> bye
var name = "kittens";
if (name === "puppies") {
name += "!";
} else if (name === "kittens") {
name += "!!";
} else {
name = "!" + name;
}
name === "kittens!!"
//=> true
// BAD
if (x = 3) {
console.log("boo");
}
// GOOD
if (x === 3) {
console.log("boo");
}
var age = 12;
//=> undefined
var allowed = (age > 18) ? "yes" : "no";
//=> undefined
allowed
//=> "no"
function whatCanIDo(age){
if (age < 16) {
console.log('You can go to school!')
} else if (age >= 16 && age < 18) {
console.log('You can drive!');
} else if (age >= 18 && age < 21) {
console.log('You can vote!');
} else if (age >= 21 && age < 25) {
console.log('You can drink alcohol!');
} else if (age >= 25 && age < 35) {
console.log('You can rent a car!');
} else if (age >= 35 && age < 62) {
console.log('You can run for president!');
} else if (age >= 62) {
console.log('You can collect social security!');
} else {
console.log('Please enter a correct age value');
}
}
var food = "apple";
switch(food) {
case 'pear':
console.log("I like pears");
break;
case 'apple':
console.log("I like apples");
break;
default:
console.log("No favorite");
}
//=> I like apples
var grade = 'C';
if (grade === 'A') {
console.log('Awesome job');
} else if (grade === 'B') {
console.log('Good job');
} else if (grade === 'C') {
console.log('Okay job');
} else if (grade === 'D') {
console.log('Not so good job');
} else if (grade === 'F') {
console.log('Bad job');
} else {
console.log('Unexpected grade value entered');
}
var grade = 'C';
switch (grade) {
case 'A':
console.log('Awesome job');
break;
case 'B':
console.log('Good job');
break;
case 'C':
console.log('Okay job');
break;
case 'D':
console.log('Not so good job');
break;
case 'F':
console.log('Bad job');
break;
default:
console.log('Unexpected grade value entered');
}
var grade = 'C';
switch (grade) {
case 'A':
case 'B':
case 'C':
console.log('You passed!')
break
case 'D':
case 'F':
console.log('You failed')
break
default:
console.log('Unexpected grade value entered')
}
=> You passed!
while (true) {
// an infinite loop!
}
// Try this in your browser!
// What do you think will happen?
var input = 0;
do {
console.log(input++);
} while (input < 10);
var result = "";
var i = 0;
do {
i = i + 1;
result = result + i;
} while (i < 5);
console.log(result);
// expected result: "12345"
var a = [1, 2, 3, 4, 5];
for (var i = 0; i < a.length; i++) {
console.log(i);
}
["dog", "cat", "hen"].forEach(function(currentValue, index, array) {
console.log("I want a ", currentValue);
console.log(array[index]);
});
Assignment 1: 99 Bottles of Beer
Assignment 2: Random Address Generator