Last active
February 23, 2022 20:49
-
-
Save rizalp/5467456 to your computer and use it in GitHub Desktop.
JavaScript: Control Structure: Boolean, If, Switch, Loop
#JavaScript #loop #control #boolean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = 5 == 6; // false | |
foo = "6" == 6; // true. Because the number 6 are first converted into a string | |
foo = "6" === 6; // false. It will also check equality of data type. Always use this operator | |
/* | |
&& (AND) | |
|| (OR) | |
! (NOT) | |
*/ | |
/*Truthy and falsy data types*/ | |
if (0) {}; //falsy. Zero is considered false | |
if (9) {}; //as long as it is not 0, it will be true | |
if ("") {}; //falsy | |
if (" ") {}; //as long as there's character in it, it will be true | |
if ([]) {}; //true | |
if ({}) {}; //true | |
if (undefined) { //undefined is false. as well as null | |
alert("hi"); | |
} | |
if (true) {} else if {} else if {} else {}; | |
(5 > 4) ? true : false; | |
var order_size = "medium"; | |
switch (order_size) { | |
case "small": | |
alert("small"); | |
break; | |
case "medium": | |
alert("medium"); | |
break; | |
case "large": | |
alert("large"); | |
break; | |
case "extra large": | |
alert("extra large"); | |
break; | |
default: | |
alert("something else?"); | |
} | |
var order_size = "medium"; | |
switch (order_size) { | |
case "small": | |
alert("small"); | |
break; | |
case "medium": | |
case "large": | |
case "extra large": | |
alert("medium, large, or extra large"); | |
break; | |
default: | |
alert("something else?"); | |
} | |
var temp = 18, | |
msg = (temp > 10) ? "Today was warm" : "Today was cold"; | |
// typeof is used to check the variable's type | |
alert(typeof null); //object | |
//Error protection syntax | |
/*Different web browsers include different properties on the error | |
object. but the main ones are name and message, which are the type of error and | |
a description of what happened, respectively. You can throw your | |
own errors using the throw keyword and creating an error object. | |
You can create this error object as an object literal, or you can use | |
the error constructor functions.*/ | |
try { | |
// code that might cause an error here | |
} catch (e) { | |
// deal with the error here | |
} finally { | |
// do something to finalize it | |
} | |
function make_coffee(requested_size) { | |
var sizes = ["large", "medium", "small"], | |
coffee; | |
if (sizes.indexOf(requested_size) < 0) { | |
throw new Error("We don't offer that size"); | |
} | |
coffee = { size: required_size, | |
added: ["sugar", "sugar", "cream" ] | |
}; | |
return coffee; | |
} | |
try { | |
make_coffee("extra large"); | |
} catch (e) { | |
console.log(e.message); | |
} | |
var names = ["Jeremy", "Jeffrey", "Jackey", "Jennifer"]; | |
for (var i = 0, length = names.length; i < length; i++) { //efficient | |
alert(names[i]); | |
}; | |
var len = names.length, | |
i = 0; | |
while (i < len) { | |
i++; | |
} | |
do { | |
var element = names.pop(); | |
alert(element); | |
} while (names.length > 0); | |
/*In JavaScript, running through a simple loop is a lot cheaper than calling a function multiple times. | |
In Essence, minimize recursive call*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment