{"id":2141,"date":"2020-04-05T12:22:38","date_gmt":"2020-04-05T12:22:38","guid":{"rendered":"http:\/\/newmediaproduction.mynmi.net\/?page_id=2141"},"modified":"2022-07-02T02:35:26","modified_gmt":"2022-07-02T02:35:26","slug":"javascript-3-fcc-19","status":"publish","type":"page","link":"https:\/\/nmi.cool\/web\/javascript-3-fcc-19\/","title":{"rendered":"JavaScript 3 &#8211; FCC 18"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/04\/fcc19_alice.png\" alt=\"\" class=\"wp-image-2144\"\/><figcaption>Alice is continuing her adventure with JavaScript!  <\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Boolean Values <\/h2>\n\n\n\n<p>Simply put, a boolean is another data type, just like a string, array, or number.  Unlike other data types, a boolean can only have two values: <code>true<\/code> or <code>false<\/code>. <a href=\"#footnote-1-2141\" id=\"note-1-2141\" rel=\"footnote\">1<\/a><\/p>\n\n\n\n<p>It can be helpful to think of a boolean as a toggle, or an on-off switch. When the toggle is in the &#8220;on&#8221; position, the value is true. When the toggle is in the &#8220;off&#8221; position the value is false. <\/p>\n\n\n\n<p>Here&#8217;s an example of a boolean value in a function: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function offWithHisHead() {\n    return false; \n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conditional Logic: If Statements <\/h2>\n\n\n\n<p>We&#8217;ll see boolean values come into play when we use <code>if<\/code> statements. In this context, <code>if<\/code> is a keyword that controls the flow of the program using booleans. This is the syntax of an <code>if<\/code> statement: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (condition) {\n      code block;\n  }\n... next bit of code... <\/pre>\n\n\n\n<p>Here&#8217;s how we would interpret that <code>if<\/code> statement: &#8220;If the condition is true, then execute the code block.&#8221; On the other hand, if the condition were false, the code block wouldn&#8217;t be executed, and the program would move on to execute the next bit of code. <\/p>\n\n\n\n<p>This might feel a little abstract right now, but as you move through the Free Code Camp exercises in this section, you&#8217;ll get a lot of practice with <code>if <\/code>statements, and they should make a lot more sense! <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Comparison Operators <\/h2>\n\n\n\n<p>JavaScript gives us a whole set of operators that allow us to compare two numerical values. These operators will always return a boolean true\/false value. This sounds a little complicated, but we basically asking simple questions like: &#8220;is <code>a<\/code> greater than <code>b<\/code>&#8221; or &#8220;is <code>c<\/code> equal to <code>d<\/code>.&#8221; We&#8217;re just asking those questions with JavaScript! <\/p>\n\n\n\n<p>One thing to be aware of here, is that <strong>some<\/strong> of the operators will actually convert the data type for you, so that the number <code>1<\/code> would be read the same as the string <code>\"1\"<\/code>. When this happens, it&#8217;s called <strong>type coercion<\/strong>. You&#8217;ll see what we mean in a second. <\/p>\n\n\n\n<p><strong>Equality operator <code>==<\/code><\/strong> : compares two values and returns true or false based on its findings. It will convert data types for you, so <code>1<\/code> and <code>\"1\"<\/code> will be read as the same value.<a href=\"#footnote-2-2141\" id=\"note-2-2141\" rel=\"footnote\">2<\/a> <\/p>\n\n\n\n<p><strong>Strict equality operator <code>===<\/code><\/strong> : This is similar to the equality operator, but it will <strong>not<\/strong> convert the data type for you, so if one value is the number <code>1<\/code> and the other is the string <code>\"1\"<\/code> they will <strong>not<\/strong> be considered equal. <\/p>\n\n\n\n<p><strong>Inequality operator <code>!=<\/code><\/strong> : This is simply the opposite of the equality operator and will return the value &#8220;true&#8221; when two values are not equal. The inequality operator will convert data types. <\/p>\n\n\n\n<p><strong>Strict inequality operator <code>!==<\/code><\/strong> : Chances are good that you see where we are going here! The strict inequality operator returns a value of true when two values are not equal. It does <strong>not<\/strong> convert the data type for you.<\/p>\n\n\n\n<p>Let&#8217;s take another look.  <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>a<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>b<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>a == b<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>a === b<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code> a != b<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>a !== b<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>2<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>3<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>false <\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>false<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>true<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>true<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>4<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>\"4\"<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>true<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>false<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>false<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>true<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>4<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>4<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>true<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>true<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>false<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>false<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Type coercion<\/strong> comes into play when <code>a = 4<\/code> and <code>b = \"4\"<\/code>. The equality operator and inequality operator treat those two values as the same data type, but the strict equality and strict inequality operators do not. <\/p>\n\n\n\n<p>Why do we need comparison operators? They are incredibly useful when we&#8217;ll try to manipulate more advanced data structures down the line. They are also really handy in <code>if<\/code> statements. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  function testComparison(val) {\n    if (val == 3) { \n      return \"Equal\";\n    }\n    return \"Not Equal\";\n  }\n  \n  testComparison(3);<\/pre>\n\n\n\n<p>What we&#8217;re seeing in the example above is a <code>function<\/code> called <code>testComparison<\/code> with one parameter: <code>val<\/code>. The statement inside the function reads: if <code>val<\/code> is equal to the number <code>3<\/code> then return the string <code>\"Equal,\"<\/code> otherwise, return the string <code>\"Not Equal.\"<\/code><\/p>\n\n\n\n<p>Okay, let&#8217;s tackle some other comparison operators. These should look pretty familiar, so we&#8217;ll move through them quickly. <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Operator<\/strong><\/td><td><strong>Interpretation<\/strong><\/td><\/tr><tr><td><code>a &lt; b<\/code><\/td><td>true when <code>a<\/code> is less than <code>b<\/code><\/td><\/tr><tr><td><code>a &gt; b<\/code><\/td><td>true when <code>a<\/code> is greater than <code>b<\/code><\/td><\/tr><tr><td><code>a &lt;= b<\/code><\/td><td>true when <code>a<\/code> is less than or equal to <code>b<\/code><\/td><\/tr><tr><td><code>a &gt;= b<\/code><\/td><td>true when <code>a<\/code> is greater than or equal to <code>b<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>It&#8217;s helpful to note that all four of these operators will convert data types for you. We can use these operators in a function just like the other comparison operators. Here&#8217;s an example: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> function perspective(glass) {\n    if (glass &lt; 50) { \n      return \"Half Empty\";\n    }\n    return \"Half Full\";\n  }\n  \n  perspective(70);<\/pre>\n\n\n\n<p>What will happen when we pass the argument 70?<a href=\"#footnote-3-2141\" id=\"note-3-2141\" rel=\"footnote\">3<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Logical And (<code>&amp;&amp;<\/code>) and Logical Or (<code>||<\/code>)Operators<\/h2>\n\n\n\n<p>Let&#8217;s take it a step further. Say we wanted to compare two different boolean values at the same time. The <em>logical and<\/em> operator will allow us to do that. It will return <code>true<\/code> if <strong>both<\/strong> operands are true. <\/p>\n\n\n\n<p>The <em>logical and<\/em> operator is displayed as two ampersands: <code>&amp;&amp;<\/code>. Here&#8217;s an example of it in action: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (a &gt; 10 <strong>&amp;&amp;<\/strong> a &lt; 100) {\n    return \"Correct\";\n}\nreturn \"Incorrect\";<\/pre>\n\n\n\n<p>What we&#8217;re saying there is &#8220;if <code>a<\/code> is greater than 10 and less than 100 return &#8220;Correct,&#8221; otherwise, return &#8220;Incorrect.&#8221; In other words, both the statement to the left and right of the <code>&amp;&amp;<\/code> operator must be true for the boolean value to equal true. <\/p>\n\n\n\n<p>The <em>logical or<\/em> operator also allows us to compare two different boolean values at the same time, but it will return <code>true<\/code> if either statement is true. The <em>logical or<\/em> operator is made of two pipe symbols: <code>||<\/code>.<a href=\"#footnote-4-2141\" id=\"note-4-2141\" rel=\"footnote\">4<\/a> Here&#8217;s how it works in an <code>if<\/code> statement. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (b &lt; 10 <strong>||<\/strong> b &gt; 100) {\n    return \"Correct\";\n}\nreturn \"Incorrect\";<\/pre>\n\n\n\n<p>Here we will return &#8220;Correct&#8221; if <code>b<\/code> is less than 10 or if <code>b<\/code> is greater than 100. Simple enough, right?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Else Statements<\/h2>\n\n\n\n<p>At this point, we&#8217;ve seen how <code>if<\/code> statements work: in the examples above, when the if statement is true, we execute the code following the statement. An <code>else<\/code> statement works with the <code>if <\/code>statement and allows us to move on to execute the next bit of code when an <code>if<\/code> statement is false. Basically we&#8217;re saying &#8220;if this is true do this thing, or else, do this next thing.&#8221;  Here&#8217;s the syntax of an <code>else<\/code> statement: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (condition) {\n    code block;\n} else {\n    new code block;\n}  <\/pre>\n\n\n\n<p>We can easily chain together multiple <code>if<\/code> statements by using <code>else if<\/code> statements. Here&#8217;s what that would look like: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> if (height &gt; 100) {\n    return \"Alice ate something\";\n  } <strong>else if<\/strong> (height &lt; 5) {\n    return \"Alice drank something\";\n  } else {\n    return \"Down the rabbit hole again\";\n  }<\/pre>\n\n\n\n<p>You&#8217;re not limited in the number of <code>else if<\/code> statements you use, so as you can imagine, your code can get quite lengthy and complex. It&#8217;s important to remember that the function is executed from top to bottom, so the order of the statements can matter quite a bit. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Switch Statements <\/h2>\n\n\n\n<p>Switch statements are similar to <code>if<\/code> statements, in fact, they can can be used to simplify complex <code>else if<\/code> statements in certain instances. A switch statement looks for the value of variable among several options (or cases); it uses <code>break<\/code> statement to tell JavaScript to stop looking for matches among the cases. Let&#8217;s look at an example: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  switch(character) {\n    case \"tweedle dee\":\n    case \"tweedle dum\":\n        danger = \"low\";\n        break;\n    case \"MaddHatter\":\n        danger = \"medium\";\n        break;\n    case \"Queen of Hearts\":\n        danger = \"high\";\n  }<\/pre>\n\n\n\n<p>This <code>switch<\/code> statement switches on the value of the variable <code>character<\/code>. By that we mean, that it compares the value of <code>character<\/code> to the values of the cases. If the value of <code>character<\/code> is equal to any one of the cases then any code for any case written below the matching case will execute until either a break statement occurs or there are no more cases. <\/p>\n\n\n\n<p>In the above example, if the <code>character<\/code> is set to either <code>\"tweedle dee\" <\/code>or <code>\"tweedle dum\"<\/code> then the <code>danger<\/code> will be set to <code>\"low\"<\/code>. After which, the <code>switch<\/code> breaks when it reaches the <code>break<\/code> statement. On the other hand, if we set <code>character<\/code> to <code>\"Queen of Hearts,\"<\/code> the danger will be <code>\"high\"<\/code>. Note that you do not need a <code>break<\/code> statement after the last case. <\/p>\n\n\n\n<p>You can add a <code>default<\/code> statement after the last <code>case<\/code> which will be executed if the value doesn&#8217;t match any of the cases. Without a <code>default<\/code> it is possible that none of the cases match the variable we are switching on. For example, if <code>character<\/code> is &#8220;Cheshire Cat&#8221; in the example above, it will not match any of the cases and we wouldn&#8217;t update our <code>danger<\/code> result. Here&#8217;s what the default statement would look like: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  switch(character) {\n    case \"tweedle dee\":\n    case \"tweedle dum\":\n        danger = \"low\";\n        break;\n    case \"MaddHatter\":\n        danger = \"medium\";\n        break;\n    case \"Queen of Hearts\":\n        danger = \"high\";\n        break;\n    <strong>default<\/strong>: \n        danger = \"unknown\"        \n  }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Return Statements<\/h2>\n\n\n\n<p>This might seem kind of tacked on and easy to overlook in the Free Code Camp lessons, but it&#8217;s a really important (and simple!) concept. A return statement is similar to a break statement. While a break statement ends a switch statement, a return statement ends any function.  <\/p>\n\n\n\n<p>A function can actually have multiple return statements, which can be useful when you want to limit further code execution to scenarios where it makes sense. For instance consider this function: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  reciprocater(y) {\n      if (y == 0) {\n       return 0\n      }\n      return 1\/y\n  }<\/pre>\n\n\n\n<p>This function takes in a value <code>y<\/code> and returns it reciprocal <code>1\/y<\/code>, however, if <code>y<\/code> is <code>0<\/code>, then <code>1\/y<\/code> is undefined. The early <code>return<\/code> statement ensures that we do not try to divide <code>1<\/code> by <code>0<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Operator Quick Reference <\/h2>\n\n\n\n<p>Here&#8217;s a quick reference of comparison operators that we looked at in this group of Free Code Camp lessons: <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>Operator<\/strong><\/td><td><strong>Interpretation<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>==<\/code> <\/td><td>The <strong>equality operator<\/strong> is true when values are the same regardless of data type <\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>===<\/code><\/td><td>The <strong>strict equality operator<\/strong> is true when the values AND data type are the same<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>!=<\/code><\/td><td>The <strong>inequality operator<\/strong> is true when two values are not equal regardless of data type<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>!==<\/code><\/td><td>The <strong>strict inequality operator<\/strong> is true when the values and\/or data type is not the same<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>a &lt; b<\/code><\/td><td>True when <code>a<\/code> is less than <code>b<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>a &gt; b<\/code><\/td><td>True when <code>a<\/code> is greater than <code>b<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>a &lt;= b<\/code><\/td><td>True when <code>a<\/code> is less than or equal to <code>b<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>a &gt;= b<\/code><\/td><td>True when <code>a<\/code> is greater than or equal to <code>b<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>&amp;&amp;<\/code><\/td><td>The <strong><em>logical and<\/em> operator<\/strong> is true when the operands to the left <strong>and<\/strong> right of it are true<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>||<\/code><\/td><td>The <strong><em>logical or<\/em> operator <\/strong>is true when the operands to the left <strong>or<\/strong> right of it is true<\/td><\/tr><\/tbody><\/table><\/figure>\n<div class=\"footnotes\"><hr \/><ol><li id=\"footnote-1-2141\" class=\"footnote\"><p> Be aware that boolean values don&#8217;t have quotes around them: so it&#8217;s true, not &#8220;true.&#8221; If quotes are present, it will be read as a string value and not a boolean value.<a href=\"#note-1-2141\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-1.footnote--><li id=\"footnote-2-2141\" class=\"footnote\"><p>Don&#8217;t get this confused with the assignment operator (<code>=<\/code>) which is used to assign a value to a variable as in <code>var = 7<\/code>.<a href=\"#note-2-2141\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-2.footnote--><li id=\"footnote-3-2141\" class=\"footnote\"><p>Our glass will be &#8220;Half Full&#8221;, optimism for the win!<a href=\"#note-3-2141\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-3.footnote--><li id=\"footnote-4-2141\" class=\"footnote\"><p> hold shift and backslash on most keyboards<a href=\"#note-4-2141\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-4.footnote--><\/ol><\/div><!--\/#footnotes-->","protected":false},"excerpt":{"rendered":"<p>Boolean Values Simply put, a boolean is another data type, just like a string, array, or number. Unlike other data types, a boolean can only have two values: true or false. It can be helpful to think of a boolean as a toggle, or an on-off switch. When the toggle is in the &#8220;on&#8221; position, &hellip; <a href=\"https:\/\/nmi.cool\/web\/javascript-3-fcc-19\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript 3 &#8211; FCC 18<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2141","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/2141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/comments?post=2141"}],"version-history":[{"count":3,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/2141\/revisions"}],"predecessor-version":[{"id":3692,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/2141\/revisions\/3692"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/media?parent=2141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}