Skip to content

Instantly share code, notes, and snippets.

@defields923
Forked from anonymous/index.html
Last active December 4, 2016 04:11
Show Gist options
  • Save defields923/e9aeb817e0eab6a3ce187dd826d49e03 to your computer and use it in GitHub Desktop.
Save defields923/e9aeb817e0eab6a3ce187dd826d49e03 to your computer and use it in GitHub Desktop.
Control FlowControl flow studies// source https://jsbin.com/xitune
// -----Control Flow----- //
/* If statements are used in Javascript to make decisions--whether to execute a following
code or not. An if statement first evaluates the boolean within parentheses and then executes
the code within curly braces if the boolean was true: */
"use strict";
if (typeof "Is this a string?" === "string") {
console.log("Yup, it was a string");
}
/* If the boolean resolves to false, the code is simply passed over. Further keywords can
be used along with the if keyword to create chains of if else statements: */
var arr = [];
if (typeof arr === "string") {
console.log("I will be skipped :( ");
} else if (typeof arr === "number") {
console.log("I'm gonna be skipped, too! :((( ");
} else if (Array.isArray(arr)) {
console.log("Finally!");
}
/* lastly, a final else keyword can conclude the chain, providing a default result if none of
the provided statements resolve to true. */
arr = {};
if (typeof arr === "string") {
console.log("I will be skipped ☹ ");
} else if (typeof arr === "number") {
console.log("I'm gonna be skipped, too! ☹☹☹ ");
} else if (Array.isArray(arr)) {
console.log("Finally!");
} else {
console.log("You lied to me! I'll never forgive you.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment