Skip to content

Instantly share code, notes, and snippets.

@dweill
Created March 26, 2017 23:22
Show Gist options
  • Save dweill/d9ba97bc8c8cd67219d4038a8581ed4c to your computer and use it in GitHub Desktop.
Save dweill/d9ba97bc8c8cd67219d4038a8581ed4c to your computer and use it in GitHub Desktop.
JS Bin control-flow // source https://jsbin.com/kebepew
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="control-flow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/* 1. Control Flow:
* Once we have evaluted a boolean a expression, we need to let the computer
* know what it do with it. We can do this easily with an if-else statement.
*/
var socks = "2";
if(socks === 2) {
console.log("Found my socks!");
}
else {
console.log("Where the heck are my dang socks?");
}
/* The above prints to "Where the heck are my dang socks?" because we used the strict
* comparison operator. Therefore it skipped "Found my socks!", because the statment was
* false, and it would only print "Found my sock!" if the statement was true.
*/
//Let's add an else if, and change our comparison operator.
var socks = 1;
if(socks >= 2) {
console.log("Found them!");
}
else if(socks >= 1) {
console.log("Where's lefty?");
}
else {
console.log("Gosh darnit!");
}
/*The above prints "Where's lefty?" because socks is not greater than or equal to 2
* It skips that event, and proceeds to the next event, "Where's lefty?" where
* it evaluates as true, and therefore stops and enacts the event, in this
* case console.log.
*/
//We can do the same thing with a switch statement.
var socks = 2;
switch(socks) {
case 2:
console.log("Sweet!");
break;
case 1:
console.log("Come on!");
break;
default:
console.log("Crud!");
}
/*This is a switch statment which is just a different method of setting up
* a conditional event.
*/
</script>
<script id="jsbin-source-javascript" type="text/javascript">/* 1. Control Flow:
* Once we have evaluted a boolean a expression, we need to let the computer
* know what it do with it. We can do this easily with an if-else statement.
*/
var socks = "2";
if(socks === 2) {
console.log("Found my socks!");
}
else {
console.log("Where the heck are my dang socks?");
}
/* The above prints to "Where the heck are my dang socks?" because we used the strict
* comparison operator. Therefore it skipped "Found my socks!", because the statment was
* false, and it would only print "Found my sock!" if the statement was true.
*/
//Let's add an else if, and change our comparison operator.
var socks = 1;
if(socks >= 2) {
console.log("Found them!");
}
else if(socks >= 1) {
console.log("Where's lefty?");
}
else {
console.log("Gosh darnit!");
}
/*The above prints "Where's lefty?" because socks is not greater than or equal to 2
* It skips that event, and proceeds to the next event, "Where's lefty?" where
* it evaluates as true, and therefore stops and enacts the event, in this
* case console.log.
*/
//We can do the same thing with a switch statement.
var socks = 2;
switch(socks) {
case 2:
console.log("Sweet!");
break;
case 1:
console.log("Come on!");
break;
default:
console.log("Crud!");
}
/*This is a switch statment which is just a different method of setting up
* a conditional event.
*/</script></body>
</html>
/* 1. Control Flow:
* Once we have evaluted a boolean a expression, we need to let the computer
* know what it do with it. We can do this easily with an if-else statement.
*/
var socks = "2";
if(socks === 2) {
console.log("Found my socks!");
}
else {
console.log("Where the heck are my dang socks?");
}
/* The above prints to "Where the heck are my dang socks?" because we used the strict
* comparison operator. Therefore it skipped "Found my socks!", because the statment was
* false, and it would only print "Found my sock!" if the statement was true.
*/
//Let's add an else if, and change our comparison operator.
var socks = 1;
if(socks >= 2) {
console.log("Found them!");
}
else if(socks >= 1) {
console.log("Where's lefty?");
}
else {
console.log("Gosh darnit!");
}
/*The above prints "Where's lefty?" because socks is not greater than or equal to 2
* It skips that event, and proceeds to the next event, "Where's lefty?" where
* it evaluates as true, and therefore stops and enacts the event, in this
* case console.log.
*/
//We can do the same thing with a switch statement.
var socks = 2;
switch(socks) {
case 2:
console.log("Sweet!");
break;
case 1:
console.log("Come on!");
break;
default:
console.log("Crud!");
}
/*This is a switch statment which is just a different method of setting up
* a conditional event.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment