{"id":2100,"date":"2020-04-03T17:05:11","date_gmt":"2020-04-03T17:05:11","guid":{"rendered":"http:\/\/newmediaproduction.mynmi.net\/?page_id=2100"},"modified":"2023-07-26T01:31:36","modified_gmt":"2023-07-26T01:31:36","slug":"javascript-1-fcc-17","status":"publish","type":"page","link":"https:\/\/nmi.cool\/web\/javascript-1-fcc-17\/","title":{"rendered":"JavaScript 1 \u2013 FCC 16"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/www.alice-in-wonderland.net\/wp-content\/uploads\/1book2.jpg\" alt=\"\"\/><figcaption class=\"wp-element-caption\"><strong>Presenting:<\/strong> <strong>Alice&#8217;s Adventures in JavaScript <\/strong><br>(Illustration from Alice&#8217;s Adventures in Wonderland by Lewis Carroll, of course!)<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Overview<\/h2>\n\n\n\n<p>Welcome to your first Free Code Camp lessons in JavaScript! These lessons will introduce you to a few of JavaScript&#8217;s basic programming concepts, such as <strong>variables<\/strong>, <strong>arithmetic operations<\/strong>, <strong>strings<\/strong> and <strong>zero-based indexing<\/strong>. Due to the complex nature of JavaScript <a href=\"#footnote-1-2100\" id=\"note-1-2100\" rel=\"footnote\">1<\/a> it&#8217;s important to establish a solid foundation before diving into the deep end. These introductory lessons may seem abstract, but they&#8217;ll play an important role in later lessons. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">FCC + this Workbook<\/h4>\n\n\n\n<p>While you&#8217;re working through the Javascript portion of Free Code Camp, these next few sections in the Workbook will <strong>correspond<\/strong> to your FCC exercises. The Workbook readings will highlight key points, offer more context, and expand on the FCC examples.  You may want to open these readings in a separate window alongside FCC\u2014or perhaps you want to read them through before and after you complete the Free Code Camp sections. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">FCC Help Reminder<\/h4>\n\n\n\n<p>When going through the remainder of the FCC lessons, don&#8217;t get spooked. It&#8217;s easy to <strong>overthink <\/strong>what FreeCodeCamp is asking. Take advantage of FCC&#8217;s hints and videos. And, feel free to work with a friend (while keeping a safe, social distance, of course).<\/p>\n\n\n\n<p>You may also find it beneficial to peruse Free Code Camp&#8217;s <a rel=\"noreferrer noopener\" href=\"https:\/\/www.freecodecamp.org\/news\/the-complete-javascript-handbook-f26b2c71719c\/\" target=\"_blank\">JavaScript Beginner&#8217;s Handbook<\/a> as well. <\/p>\n\n\n\n<p>Once you&#8217;re ready to get started on FCC, start reading \u2b07\ufe0f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"comments\">Comments<\/h3>\n\n\n\n<p>By now, you&#8217;re pretty familiar with the habit of writing comments in your <a rel=\"noreferrer noopener\" aria-label=\"CSS (opens in a new tab)\" href=\"https:\/\/www.w3schools.com\/css\/css_comments.asp\" target=\"_blank\">CSS<\/a> and <a rel=\"noreferrer noopener\" aria-label=\"HTML (opens in a new tab)\" href=\"https:\/\/html.com\/tags\/comment-tag\/\" target=\"_blank\">HTML<\/a> files. Writing comments in JavaScript is no different. These are comments that JavaScript will ignore, but are helpful to keep yourself, your future self, and someday perhaps, your coworkers, organized and up-to-date. <\/p>\n\n\n\n<p>There&#8217;s two ways to write comments in JavaScript:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ This is an in-line comment.<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* This is a\nmulti-line comment *\/<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Variables<\/h3>\n\n\n\n<p>Just like in your math classes, <strong>variables<\/strong> in JavaScript are used to store values. Unlike your math classes, though, variables in JavaScript are <em>variable<\/em>, that is, they are subject to change. In other words, variables are used to store different values at different times. <\/p>\n\n\n\n<p>Computers require very detailed, very specific instructions to carry out commands. Essentially, variables are telling the computer, <em>remember<\/em> this piece of information at this moment in time. Store this specific knowledge away for now. <\/p>\n\n\n\n<p>To <strong>declare a variable<\/strong>, write <code>var<\/code> followed by the variable name, and end your statement with a semicolon: <\/p>\n\n\n\n<p><code>var madHatter;<\/code><\/p>\n\n\n\n<p>Variables cannot use spaces or start with a number, and of course, casing matters <a href=\"#footnote-2-2100\" id=\"note-2-2100\" rel=\"footnote\">2<\/a>. It&#8217;s best practice to use <strong>camelCase<\/strong>, where multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalized, such as <code>myMadQueen<\/code>, <code>yourDoorMouse<\/code>.<\/p>\n\n\n\n<p>To <strong>assign a value<\/strong> to a variable, use the <strong>assignment operator<\/strong> <code>=<\/code>.<\/p>\n\n\n\n<p><code>madHatter = 3<\/code><\/p>\n\n\n\n<p class=\"has-light-gray-background-color has-background\">A word of caution with the assignment operator: Everything to the right of the equals sign = is evaluated first. More on that later! <\/p>\n\n\n\n<p>Typically, you declare a variable and assign it a value at the same time, called <strong>initializing <\/strong>a variable: <\/p>\n\n\n\n<p><code>var cheshireCatShoppingCart = 1;<\/code><\/p>\n\n\n\n<p>In this case, we are establishing a variable and giving it a value, all at once.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic math operations<\/h3>\n\n\n\n<p>Performing math operations is a keystone of programming languages including JavaScript. When we rely on a tool like JavaScript to perform arithmetic operations, we&#8217;re able to process numerical data quicker, more accurately, and at much longer volumes. <\/p>\n\n\n\n<p>So much of the web involves processing numerical data. Think of prices, quantities, totals, shipping, in stock, out of stock, sales, taxes\u2014and all the calculations needed to give users accurate information on all the various combinations of all of those elements!<\/p>\n\n\n\n<p>Numbers in Javascript represent\u2014you guessed it!\u2014numeric data. Here&#8217;s some of the basic arithmetic operations you can do with JavaScript: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Addition <code>+<\/code><\/li>\n\n\n\n<li>Subtraction <code>-<\/code><\/li>\n\n\n\n<li>Multiplication <code>*<\/code><\/li>\n\n\n\n<li>Division <code>\/<\/code><\/li>\n\n\n\n<li>Use decimals (sometimes called <em>floats) <\/em><code>2.5<\/code><\/li>\n<\/ul>\n\n\n\n<p>You can <strong>increment<\/strong> (add one) with the <code>++<\/code> operator.<\/p>\n\n\n\n<p> <code>i = i + 1<\/code> becomes <code>i++;<\/code>  <\/p>\n\n\n\n<p>And <strong>decrement<\/strong> (decrease a variable by one) with the <code>--<\/code> operator.<\/p>\n\n\n\n<p><code>i = i - 1;<\/code> is the same as <code>i--;<\/code> <\/p>\n\n\n\n<p>The <strong>remainder<\/strong> operator <code>%<\/code> gives the remainder of the division of two numbers. <\/p>\n\n\n\n<p>In the example below, the variable <code>remainder<\/code> becomes equal to the remainder of 16 divided by 7: <\/p>\n\n\n\n<p><code>remainder = 16 % 7 <\/code><\/p>\n\n\n\n<p>Read more on <a rel=\"noreferrer noopener\" aria-label=\"arithmetic operators (opens in a new tab)\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Arithmetic_Operators#Increment_()\" target=\"_blank\">arithmetic operators<\/a> in JavaScript. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Compound assignments<\/h3>\n\n\n\n<p>Remember how I said everything to the right of the assignment operator <code>=<\/code> is evaluated first? <\/p>\n\n\n\n<p><code>myVar = myVar + 5;<\/code><\/p>\n\n\n\n<p>This above example does two things: It assigns a variable and performs a mathematical equation. This assigns <code>myVar<\/code>, and adds the value of 5 to it, all in one fell swoop. <\/p>\n\n\n\n<p>Since this is such a common occurrence  in JavaScript, there are operators which do both a mathematical operation and assignment in one step, such as the&nbsp;<code>+=<\/code>&nbsp;operator.<\/p>\n\n\n\n<p>You&#8217;ve heard it before, but programmers are lazy. We like to write as little code as possible, especially if it&#8217;s something as small as an arithmetic operation that could be repeated who knows how many times in a program. <\/p>\n\n\n\n<p>Instead of writing: <\/p>\n\n\n\n<p><code>a = a + 12;<\/code> <\/p>\n\n\n\n<p>It&#8217;s shorter to write:<\/p>\n\n\n\n<p><code>a += 12<\/code><\/p>\n\n\n\n<p>The <code>+=<\/code>&nbsp;operator is replacing the <code>= a +<\/code>. <\/p>\n\n\n\n<p>If this is confusing, please be sure to check out the FCC video under Get Help for <a rel=\"noreferrer noopener\" aria-label=\"Basic JavaScript: Compound Assignment With Augmented Addition (opens in a new tab)\" href=\"https:\/\/www.freecodecamp.org\/learn\/javascript-algorithms-and-data-structures\/basic-javascript\/compound-assignment-with-augmented-addition\" target=\"_blank\">Basic JavaScript: Compound Assignment With Augmented Addition<\/a>.<\/p>\n\n\n\n<p>Compound assignment with augmented subtraction works much the same: <\/p>\n\n\n\n<p><code>b = b - 15<\/code>  is the same thing as <code>b -= 15<\/code><\/p>\n\n\n\n<p>And so on and so fourth with augmented multiplication <code>*=<\/code> and augmented division <code>\/=<\/code>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Strings<\/h3>\n\n\n\n<p>Words have power, too. Strings in JavaScript can be used to creating custom welcome messages, alerts, prompts, as well sort items, share information, modify messages, and&nbsp;much more. Think of every time a website or an app told you something: <code>Game over<\/code>. <code>You've got mail!<\/code> <\/p>\n\n\n\n<p><strong>String literals <\/strong>consistent of characters (text, numbers , punctation symbols, <a href=\"https:\/\/www.javascript.com\/learn\/strings\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"even emojis (opens in a new tab)\">even emojis<\/a> \u26a1\ufe0f) enclosed in single or double quotes. It&#8217;s easy to remember if you think of a string as being <em>a string of characters.<\/em><\/p>\n\n\n\n<p>To transform <code>var madHatter<\/code> into a string:<\/p>\n\n\n\n<p> <code>var madHatter = \"Take some more tea.\"<\/code> <\/p>\n\n\n\n<p>Likewise, strings can also consistent of numbers, so long as they are still wrapped in either double or single quotes. <\/p>\n\n\n\n<p><code>var myHeads = '1'<\/code><\/p>\n\n\n\n<p>It&#8217;s all fun and games working with single and double quotes until you want to include a <em>literal quote<\/em> <code>\"<\/code> or <code>'<\/code> inside the string. You do this by placing a backslash <code>\\<\/code> in front of the quote. This tells JavaScript: &#8220;Don&#8217;t end this string! Just keep this quotation mark as <em>part<\/em> of the string, rather than the ending.&#8221; <\/p>\n\n\n\n<p><code>var sampleAliceStr = \"The March Hare said, \\\"It wasn\u2019t very civil of you to sit down without being invited,\\\" to Alice.\";<\/code><\/p>\n\n\n\n<p>Would output the following: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The March Hare said, \"It wasn\u2019t very civil of you to sit down without being invited,\" to Alice.<\/pre>\n\n\n\n<p>This allows us to place quotes in our strings. This is often referred to as <em>escaping <\/em>a quote. <\/p>\n\n\n\n<p>And remember, you have to use the backslash <code>\\<\/code> to escape a quote. Don&#8217;t get confused with the forward slash <code>\/<\/code> which is used for other things, such as <a href=\"#comments\">comments<\/a>. <\/p>\n\n\n\n<p>String\u00a0values in JavaScript may be written with single or double quotes, as long as you start and end with the <strong>same<\/strong> type of quotation mark. If you start with a double quote, you must end with a double quote. If you start with a single quote, you must end with a single quote.  <\/p>\n\n\n\n<p>You may want to use both in a string\u2014one as a literal quote and one as your actual string quotes. If you do this, you don&#8217;t have to rely on <code>\\<\/code> to escape quotes. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Concatenation ? + ?<\/h3>\n\n\n\n<p>To <em>concatenate <\/em>is to combine or link things together, such as a name plus a greeting: <code>Good afternoon,<\/code> <code>Anna!<\/code><\/p>\n\n\n\n<p> In JavaScript, we can concatenate multiple strings together to form one string. This is accomplished via the concatenation operator: <code>+<\/code>  <\/p>\n\n\n\n<p><code>'Well, some go this way, and some go that way, ' <strong>+<\/strong> 'but as for me, myself, personally, I prefer the short-cut.'<\/code><\/p>\n\n\n\n<p>Concatenation does not automatically add spaces, so you will need to add spaces manually. <\/p>\n\n\n\n<p>You can concatenate strings with our good friend, the plus equals operator  <code>+=<\/code>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var ourStr = \"Well, some go this way, and some go that way, \";\nourStr += \"but as for me, myself, personally, I prefer the short-cut.\";<\/code><\/pre>\n\n\n\n<p>Now, <code>ourStr <\/code>is equal to:<\/p>\n\n\n\n<p> <code>Well, some go this way, and some go that way, but as for me, myself, personally, I prefer the short-cut.<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Bracket notation<\/h3>\n\n\n\n<p>Bracket notation <code>[] <\/code>is a way to identify a character at a specific <code>index<\/code> within a string. Each character in a string is given an index number\u2014essentially, its that character&#8217;s place in line. <\/p>\n\n\n\n<p>What is most important to remember is that computers don&#8217;t think the same way we do. In the example below, bracket notation <code>[0]<\/code> is being used to show us what the <em>first <\/em>character is in the string <code>Alice<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var firstName = \"Alice\";\nvar firstLetter = firstName&#091;0]; \/\/ firstLetter is \"A\"<\/code><\/pre>\n\n\n\n<p>Modern programming languages such as JavaScript use <em>zero-based<\/em> indexing, which is a fancy way of saying they start counting at <strong>0 rather than 1<\/strong>. This is worth remembering. <\/p>\n\n\n\n<p>In <code>var firstName = \"Alice\";<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A = [0]<\/li>\n\n\n\n<li>L = [1]<\/li>\n\n\n\n<li>I = [2]<\/li>\n\n\n\n<li>C = [3]<\/li>\n\n\n\n<li>E = [4]<\/li>\n<\/ul>\n\n\n\n<p>Occasionally, Free Code Camp might ask for the <em>third<\/em> letter in something, say, a last name. If you are using bracket notation to get the third letter, your bracket notation would look like: <code>[2]<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">To sum it up<\/h4>\n\n\n\n<p>Your first Free Code Camp JavaScript expedition should look a little bit like Alice&#8217;s Adventures in Wonderland: Mystical, confusing, perhaps even a bit frightening, but overall, you&#8217;re starting to see that JavaScript is a programming language with huge potential. We&#8217;re just scratching the surface. Don&#8217;t lose your head! <\/p>\n<div class=\"footnotes\"><hr \/><ol><li id=\"footnote-1-2100\" class=\"footnote\"><p>spoiler alert: JavaScript can do everything!<a href=\"#note-1-2100\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-1.footnote--><li id=\"footnote-2-2100\" class=\"footnote\"><p>Kind of a reoccurring theme in this course.<a href=\"#note-2-2100\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-2.footnote--><\/ol><\/div><!--\/#footnotes-->","protected":false},"excerpt":{"rendered":"<p>Overview Welcome to your first Free Code Camp lessons in JavaScript! These lessons will introduce you to a few of JavaScript&#8217;s basic programming concepts, such as variables, arithmetic operations, strings and zero-based indexing. Due to the complex nature of JavaScript it&#8217;s important to establish a solid foundation before diving into the deep end. These introductory &hellip; <a href=\"https:\/\/nmi.cool\/web\/javascript-1-fcc-17\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript 1 \u2013 FCC 16<\/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-2100","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/2100","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=2100"}],"version-history":[{"count":3,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/2100\/revisions"}],"predecessor-version":[{"id":4362,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/2100\/revisions\/4362"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/media?parent=2100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}