/* BOOLEANS AND CONDITIONALS ... and else if statements
I hope this is not too simple. Larry mentioned that some of the best teaching solutions are very simple. I don't know why but it took me some time to understand the if/boolean relationship.
Typing booleans directly into the console will give you it’s value:
"rose" == "rose"
true
"cat" == "dog";
false
"cat" > "dog";
false
true == false;
false
true == true
true
Compare two values and get a boolean value, but what can you do with it.
The if statement analyzes and acts on data. If you enter your user name and password correctly, a system will let you onto their site and if you don’t, it will lock you out. It analyses the data and accepts or rejects it as true or false.
if(someCondition) {
doThis();
}
If the someCondition evaluates to true, it runs the inner code, doThis() in the brackets. If that evaluates to false then the inner code is skipped.
To demonstrate this :
*/
if (true) {
console.log("Yes this is true.")
}
// Try switching out true for false and see what happens
if (false) {
console.log("False and I will not run.")
}
// A more realistic example would be to have a user name passed in from an html text field but we can simulate that with a variable.
var userName = "Lucille";
if (userName == "Lucille") {
console.log("Welcome to our site");
}
/*Change the variable and that code will not run.
if (condition1) {
block of code to be executed if condition1 is true
} else if (condition2) {
block of code to be executed if the condition1 is false and condition2 is true
} else {
block of code to be executed if the condition1 is false and condition2 is false
}
One or the other of these two blocks will always run. The condition will evaluate to true or false. The first block of code is true and the second false ... unless of course you add more conditionals with else ifs. */
var userName = "Lucillee";
if (userName == "Lucille") {
console.log("Welcome to our site");
}...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.