{"id":304,"date":"2022-04-14T15:45:36","date_gmt":"2022-04-14T15:45:36","guid":{"rendered":"https:\/\/melfaeforest.xhere.eu.org\/?p=304"},"modified":"2022-08-15T17:34:35","modified_gmt":"2022-08-15T17:34:35","slug":"javascript-quick-notebook-summary-part-1","status":"publish","type":"post","link":"https:\/\/melfaeforest.xhere.eu.org\/?p=304","title":{"rendered":"JavaScript Quick Notebook Summary &#8211; Part 1"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>Fundamentals<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">To import a Javascrpt (JS) file into an HTML file, you use the following in the &lt;head&gt; section:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script src=\"(the file name).js\"&gt;&lt;\/script&gt;<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>The Var, the Let, and the Const variables<\/strong><\/p>\n\n\n\n<p>There are three main types of variables: var, let, and const.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var is an outdated means of assigning variables. The value assigned to it can be changed later.\n\nlet is the more modern version of var. Like var, the value it is assigned to can be changed later.\n\nconst is the final primary variable. const variables CANNOT be changed after assignment. They should be used over var or let, UNLESS you plan for their assigned variable to be changed.<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>Using variables for calculating + The DRY Principle<\/strong><\/p>\n\n\n\n<p>There are some basic operators you can use in JavaScript.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Instead of typing the two age const variables like this...\nconst now = 2037;\nconst ageJones = 2037 - 1991;\nconst ageSarah = 2037 - 2000;\n\n\/\/ You can do this instead!\nconst now = 2037;\nconst ageJones = now - 1991;\nconst ageSarah = now - 2000;<\/pre>\n\n\n\n<p>You can easily use variables to calculate things for other variables, if-statements, etc, as long as the datatypes of the saved variable&#8217;s contents is able to be used in those calculations. It&#8217;s also preferable that you do this whenever you can, because of the DRY principle that most coding languages use. <\/p>\n\n\n\n<p>The DRY principle is an acronym for &#8220;Don&#8217;t Repeat Yourself&#8221;, and it is exactly what it&#8217;s name suggests: If you can avoid repeating typing the same code in multiple places, then you should avoid it and find some way to compact your code for re-usability like the above example. There&#8217;s two reasons for this: A) It makes your code easier to read and understand, and B) If you have to change something in one place, you don&#8217;t have to change it everywhere else you used the code in.<\/p>\n\n\n\n<p><strong>Basic Math Operators<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Basic math operators\nconsole.log(2 * 3, 4 \/ 2, 2 ** 3);<\/pre>\n\n\n\n<p>Above is a console.log() that has some basic math operators, those being the multiplication operator (2 * <code>3<\/code>), the division operator (<code>4 \/ 2<\/code>), and the power-of operator (<code>2 ** 3<\/code>). The multiplication operator is identified through the singular <code>*<\/code> between two numbers; likewise the division operator is the same, only with <code>\/<\/code> instead of <code>*<\/code>. The final basic math operator is the power-of operator, which is identified by <code>**<\/code> (two <code>*<\/code> put together)<\/p>\n\n\n\n<p>To combine different strings together, the most basic way of doing so is by concatenating them.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Concatenating strings\nconst firstWord = 'Concatenating';\nconst secondWord = 'Strings';\nconsole.log(firstWord + ' ' + secondWord);<\/pre>\n\n\n\n<p>It&#8217;s not exactly pretty to look at, nor is it easy to type, but it works for the time being until one learns about template literals, which will help massively. <\/p>\n\n\n\n<p><strong>Coercion<\/strong><\/p>\n\n\n\n<p>While still on this topic, it&#8217;ll be a good thing to note that JavaScript has something called type coercion, or the automatic changing of one datatype to another (such as a number to a string).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Type coercion example\nconst number5 = 5;\nconst string9 = '9';\nlet sum = number5 + string9;\n\n\/\/ The console will log \"59\"\nconsole.log(sum);<\/pre>\n\n\n\n<p>Type coercion will automatically convert one datatype into another when the situation requires it; in this case, it converted the <code>number5<\/code> variable into a string in order to concatenate it and the <code>string9<\/code> variable together to form &#8220;59&#8221;. The compiler could&#8217;ve chose to make them both numbers before returning a sum of <code>14<\/code>, but it did not. To return this result, you have to implicitly (manually) convert the <code>'9'<\/code> into a number using the <code>Number()<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Now this will give the expected number 14.\nsum = number5 + Number(string9);<\/pre>\n\n\n\n<p><strong>Assignment Operators<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Will allow you to assign the operation result to x. In this case, it's 15\nlet x = 10 + 5\n\n\/\/\/\/ Some handy shorthands for math operands + variable assigning\n\/\/ Shorthand for \"x = x + y\"\nx += y\n\n\/\/ Shorthand for \"x = x - y\"\nx -= y\n\n\/\/ Shorthand for \"x = x * y\"\nx *= y\n\n\/\/ Shorthand for \"x = x \/ y\"\nx \/= y\n\n\/\/\/\/ Increment shorthands\n\/\/ Increases a counter by 1\nx++;\n\n\/\/ Decreases a counter by 1\nx--;<\/pre>\n\n\n\n<p>(Note for <code>x++<\/code> \/ <code>x--<\/code>, they won&#8217;t really come up until we learn about loops later.)<\/p>\n\n\n\n<p><strong>Comparison Operators<\/strong><\/p>\n\n\n\n<pre id=\"block-b8e05469-b68e-496d-9dd5-20662b4f8fbf\" class=\"wp-block-preformatted\">\/\/\/\/ &gt;, &lt;, ==, ===, &gt;=, &lt;=\n\/\/ Note: '==' and '===' will be discussed down below, and will be skipped here\n\n\/\/ '&gt;' is the greater-than symbol, it checks if item 1 is greater than item 2\n\n\/\/ '&lt;' is the less-than symbol, it checks if item 1 is smaller than item 2\n\n\/\/ '&gt;=' is the greater-than-or-equal symbol, it checks if item 1 is greater than OR equal to item 2\n\n\/\/ '&lt;=' is the less-than-or-equal symbol, it checks if item 1 is less than OR equal to item 2<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>Datatypes<\/strong><\/p>\n\n\n\n<p>There are multiple types in JavaScript (and any coding language for that matter). You can use &#8220;typeof&#8221; + &#8220;(a value)&#8221; to see these types. As another thing, JavaScript has dynamic typing, users DO NOT have to manually assign the data type to a variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Boolean values are either true or false and nothing else. Used for taking decisions. Putting '!' in front of a boolean will invert the boolean result.\n\nString is anything between single or double-quotes. They represent physical text such as letters, spaces, symbols, etc.\n\nNumber represent floating-point (decimals) and integers (rounded numbers).\n\nUndefined represents values that haven't been defined yet. (Such as <code>var age;<\/code>)\n\nNull also means empty value, much like undefined\n\nSymbol means characters that represents exclusively symbols. (\"%\", \"#\", \"*\", etc.)\n\nBigInt is like Number, but used for larger lengths of integers that Number can't hold.<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>Concatenating Strings<\/strong><\/p>\n\n\n\n<p>When using strings, you either have to do two things to import outside data into the string. Either you have to concatenation the string, I.E:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ A basic concatenation \nconst numm = 25;\nconsole.log(\"Hello\" + numm + \"World!\");<\/pre>\n\n\n\n<p>OR you could use template literals. Template literals allows you to just import the data (mostly variables) into the string directly without having to do the complicated string splitting above. To do a template literal, you have to swap out the quotation marks in the string with the grave accent (the symbol above the TAB key), and the data you want to get inserted is wrapped in &#8220;${}&#8221; (a dollar sign + open and close curly brackets)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ A basic template literal\nconst numm = 25;\nconsole.log(<code>`Hello ${numm} World!<\/code>`);<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>The If &amp; If-Else Statements<\/strong><\/p>\n\n\n\n<p>To take decisions in JavaScript, one of the best bets is to use an if-else statement. If-else statements are simple: They check to see if a value is true or not, and if they are, they execute the code inside their code block and exit right afterwards. If not, then they check any other conditions in any &#8220;else&#8221; statements following the initial if-statement.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ A basic if-else statement\nconst javescriptFun = true;\nif (javascriptFun) {\n  console.log('JavaScript is fun! :D');\n} else {\n  console.log('Why would you say JavaScript is not fun? :(');\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>Comparing datatypes with &#8216;==&#8217; and &#8216;===&#8217;<\/strong><\/p>\n\n\n\n<p>When comparing data types, you could use &#8216;==&#8217; or &#8216;===&#8217; to return a boolean value. &#8216;==&#8217; means &#8220;roughly equal&#8221; or &#8220;lazy type checking&#8221;, which only compares the actual values of what&#8217;s being compared and not their datatypes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ An example of '==', which does a lazy comparison check. With '==', the console will log true because the actual values are the same\nconsole.log('123' == 123);<\/pre>\n\n\n\n<p>On the other hand, &#8216;===&#8217; is often referred to as &#8220;strict type checking&#8221;; using this comparison WILL check the datatype of what&#8217;s being compared, in addition to checking their values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ An example of '===', which will yield false because their datatypes don't match\nconsole.log('123' === 123);<\/pre>\n\n\n\n<p>Its often better to use the strict checking to prevent errors from occurring down the line.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>Truthy &amp; Falsy Values<\/strong><\/p>\n\n\n\n<p>In JavaScript, there&#8217;s truthy and falsy values. Truthy values are any value type that ISN&#8217;T one of the five following falsy values:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">null, undefined, 0, '' (empty string), NaN<\/pre>\n\n\n\n<p>Using Boolean() on any of these falsy values will yield false; In fact, putting one of these falsy values as a condition in an if-else statement will cause unexpected results.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Since height is a falsy value, \"Height is UNDEFINED! &gt;:(\" will be logged to the console\nconst height = 0;\nif (height) {\n  console.log('Yay! Height is defined! :D');\n} else {\n  console.log('Height is UNDEFINED! &gt;:(');\n}<\/pre>\n\n\n\n<p>It is important to know if a value is truthy or falsy, as that will cause unexpected errors in the code if one doesn&#8217;t know them or how to deal with them.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>The Ternary Operator<\/strong><\/p>\n\n\n\n<p>The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (<code>?<\/code>), then an expression to execute if the condition is truthy followed by a colon (<code>:<\/code>), and finally the expression to execute if the condition is falsy. This operator is frequently used as an alternative to an if-else statement. <strong><em>&#8211; The MDN Web Documents.<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example of a ternary operator\nconst truth = true;\nconst lie = false;\n\n\/\/ The console logs 'he speaks the truth!' since the variable \"truth\" is a truthy value (true, in this case)\nconsole.log(truth ? 'he speaks the truth!' : 'He lies!')\n\n\/\/ The console logs 'he lies!' since the variable \"lie\" is a falsy value (false, in this case)\nconsole.log(lie ? 'he speaks the truth!' : 'He lies!')<\/pre>\n\n\n\n<p>the ternary operator is used to make quick and easy comparisons all in one line, compared to if-else statements which requires a minimum of five lines. However, ternary operators can ONLY contain the three operands required to use it, no more, no less, making its usage limited.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fundamentals To import a Javascrpt (JS) file into an HTML file, you use the following in the &lt;head&gt; section: &lt;script src=&#8221;(the file name).js&#8221;&gt;&lt;\/script&gt; The Var, the Let, and the Const variables There are three main types of variables: var, let, and const. var is an outdated means of assigning variables. The value assigned to it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":25,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":528,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions\/528"}],"wp:attachment":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}