Write down what the following statements will return. Try to figure this out before putting the commands in the chrome console.
- 2 == “2”;
true
- 2 === 2;
true
- 10 % 3;
1
- 10 % 3 === 1;
true
- true && false;
false
- false || true;
true
- true || false;
true
Answer the following questions about this code block:
let isLearning = true;
if(isLearning){
console.log("Keep it up!");
} else {
console.log("Pretty sure you are learning....");
}
- What should the above code console.log?
console.log
will output "Keep it up!" becauseisLearning
is truthy.
- Why do we not need to specify if(isLearning === true)? Why does if(isLearning) work on its own?
true
is truthy and is what if statements check for.
let firstVariable;
let secondVariable = "";
let thirdVariable = 1;
let secretMessage = "Shh!";
if(firstVariable){
console.log("first");
} else if(firstVariable || secondVariable){
console.log("second");
} else if(firstVariable || thirdVariable){
console.log("third");
} else {
console.log("fourth");
}
-
What should the above code console.log? Why?
third
firstVariable
is falsey and is skipped,secondVariable
is also falsey and the firstelse if
is skipped.thirdVariable
is truthy, so "third" is printed. The else is skipped because the condition was met.
-
What is the value of firstVariable when it is initialized?
undefined
-
Is the value of firstVariable a “truthy” value? Why?
- No, it is falsey.
firstVariable
isundefined
- No, it is falsey.
-
Is the value of secondVariable a “truthy” value? Why?
- No, it is falsey. Empty strings are falsey.
-
Is the value of thirdVariable a “truthy” value? Why?
- Yes, it is. Any number except
0
is truthy.
- Yes, it is. Any number except
-
Research here
Math.random
Write an if statement that console.log’s “Over 0.5” if Math.random returns a number greater than 0.5.
Otherwise console.log “Under 0.5”.
let rand = Math.random(); if (rand > 0.5) { console.log("Over 0.5"); } else { console.log("Under 0.5"); }
-
What is a falsey value? List all the falsey values in JavaScript.
- A falsey value is a value that is
false
in a conditional statement. undefined
,null
,0
,""
(any empty string),NaN
, andfalse
.
- A falsey value is a value that is