<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prashant Sharma</title>
    <description>The latest articles on DEV Community by Prashant Sharma (@gutsytechster).</description>
    <link>https://dev.to/gutsytechster</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F319612%2F2cf775cd-2107-4143-a15c-412ff6ef0090.jpg</url>
      <title>DEV Community: Prashant Sharma</title>
      <link>https://dev.to/gutsytechster</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gutsytechster"/>
    <language>en</language>
    <item>
      <title>[freeCodeCamp] ES6 - Arrow Function, Rest Parameter, Spread Operator</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Sat, 22 Aug 2020 17:27:24 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-es6-arrow-function-rest-parameter-spread-operator-2j3c</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-es6-arrow-function-rest-parameter-spread-operator-2j3c</guid>
      <description>&lt;p&gt;Heya fellows! Here comes another JavaScript post that covers some amazing ES6 features which are widely used in current JavaScript environment. This is in continuation of my JavaScript learning from &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This post is going to be a bookmark for you. So, just start reading.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Anonymous functions&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;In JavaScript, we don't always need to give a name to a function. A function without a name is called an &lt;em&gt;anonymous function&lt;/em&gt;. These are often used when we don't need to reuse them.&lt;/p&gt;

&lt;p&gt;One can write an anonymous function as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myFunc = function() {
  const myVar = "value";
  return myVar;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Arrow functions&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;ES6 provides a cleaner and concise way to write an anonymous function with the help of arrow functions. The above function definition can also be written as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myFunc = () =&amp;gt; {
  const myVar = "value";
  return myVar;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Does it look the same? Focus on the &lt;code&gt;=&amp;gt;&lt;/code&gt; arrow and &lt;code&gt;()&lt;/code&gt; before that.&lt;/p&gt;

&lt;p&gt;This can further be written into a one-liner, when there is no function body and only a single return value. Arrow function syntax allows omitting the &lt;code&gt;return&lt;/code&gt; statement and the brackets surrounding the code, in such cases.&lt;/p&gt;

&lt;p&gt;Since the above function only declares a variable and return that variable. The above function can be re-written as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myFunc = () =&amp;gt; "value";
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will return the &lt;code&gt;"value"&lt;/code&gt; by default. Isn't it awesome? :)&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;u&gt;Arrow functions with parameters&lt;/u&gt;
&lt;/h3&gt;

&lt;p&gt;We can also pass arguments to an arrow function, just like any other regular function. The parameters can be placed within the parentheses as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const adder = (num) =&amp;gt; num++;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above method takes an argument &lt;code&gt;num&lt;/code&gt;, adds 1 to it and return the updated value. Don't forget, there is an implicit return already.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;:- &lt;em&gt;If an arrow function requires a single argument, the parenthesis around the parameter can be removed.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The above can also be written as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const adder = num =&amp;gt; num++;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That doesn't mean that you can't pass multiple arguments to an arrow function. You can pass as many arguments as you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiplier = (a, b) =&amp;gt; a * b;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Default parameters&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;ES6 allows the function to be more flexible with the use of default parameters. The default parameters are used when no arguments are specified i.e. when the parameter's value is &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It'll be easier to understand with an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const greeting = (name = "Anonymous") =&amp;gt; "Hello" + name;

console.log(greeting("Prashant")); // Hello Prashant;
console.log(greeting());  // Hello Anonymous;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can see, that when I provided the argument &lt;code&gt;"Prashant"&lt;/code&gt;, the &lt;code&gt;name&lt;/code&gt; parameter used that value. However, when I called the function without any argument, the default value is used.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Rest Parameters in function&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We can provide the variable number of arguments to the function with the help of rest parameters. The arguments provided, are stored in an array which can be manipulated from inside of the function.&lt;/p&gt;

&lt;p&gt;Consider this piece of code, from freeCodeCamp's lesson&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function howMany(...args) {
  return "You have passed " + args.length + " arguments.";
}

console.log(howMany(0, 1, 2)); // You have passed 3 arguments.

console.log(howMany("string", null, [1, 2, 3], { })); // You have passed 4 arguments
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Rest parameters are defined using the three dots followed by the array variable i.e. &lt;code&gt;...args&lt;/code&gt; is a rest parameter.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Spread Operator&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;ES6 provides us with the spread operator, which allows us to expand arrays &lt;strong&gt;in-place&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's understand the benefits of the spread operator and how to use them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Quest - Finding Maximum of an array&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before anything, let me introduce the &lt;code&gt;Math.max()&lt;/code&gt; function. By the looks of it, you can guess that it would return the maximum of the elements provided to the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.max(1, 2, 3, 4);  // returns 4 as it is the maximum
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This works perfectly. However, this function takes comma-separated arguments, not an array. If we were to store elements in an array and then try to use this function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, 3, 4];
Math.max(arr);  // returns NaN
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since this function doesn't recognize &lt;code&gt;arr&lt;/code&gt; as a valid argument. The output makes sense.&lt;/p&gt;

&lt;p&gt;In ES5, if we were to find the maximum of elements present in an array, we would need to use &lt;code&gt;Math.max.apply()&lt;/code&gt; function as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.max.apply(null, arr); // returns 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we can get the maximum number present in an array without explicitly providing each array value as a separate argument to &lt;code&gt;Math.max()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With the use of the spread operator, we can use the array and still don't need to use the &lt;code&gt;apply()&lt;/code&gt;, which makes the code more readable and easier to maintain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, 3, 4];
Math.max(...arr);  // returns 4 now
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What exactly happened here? &lt;code&gt;...arr&lt;/code&gt; expanded the &lt;code&gt;arr&lt;/code&gt; array in-place i.e. it spreads the element of an array. This is exactly how we use the spread operator to expand an array. Therefore&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.max(...arr) ≡ Math.max(1, 2, 3, 4)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You might wonder that it looks exactly like the Rest parameter. Yes, it does, syntax-wise. However, when you need to &lt;strong&gt;collect&lt;/strong&gt; values into an array, you use rest parameter(collecting values passed via arguments) and when you need to &lt;strong&gt;spread&lt;/strong&gt; values(reassigning values to an array), you use spread operator. You may see it as different names for different operations.&lt;/p&gt;

&lt;p&gt;The values of an array are substituted in place with the help of the spread operator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Another Example&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's consider one more example of its use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ar1 = [1, 2, 3, 4]
const arr2 = [5, 6, 7, 8]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you would want to merge these two arrays into a single one, you can use the spread operator as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr3 = [...arr1, ...arr2];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, the values of &lt;code&gt;arr1&lt;/code&gt; and &lt;code&gt;arr2&lt;/code&gt; have been spread in-place. Now, even if you were to change the values of &lt;code&gt;arr1&lt;/code&gt; or &lt;code&gt;arr2&lt;/code&gt;, the &lt;code&gt;arr3&lt;/code&gt; would always return the corresponding updated array. Isn't it another awesome feature? :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:- You should know that spread operator works only in-place. For e.g. as an argument to a function or in an array literal. The following piece of code will not work&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const spreaded = ...arr3;   // throws an error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;This post covered some great ES6 features. The use of arrow function is very common nowadays and its use along with the rest parameter and spread operator makes it something really awesome.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;u&gt;
&lt;/u&gt;&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/"&gt;Introduction to the ES6 Challenges&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this post helped you to understand some other JS awesome features. Well, it's time to say Good Bye! Meet you in the next post. Till then be curious and keep learning.called&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>100daysofcode</category>
      <category>webdev</category>
    </item>
    <item>
      <title>[freeCodeCamp] ES6 - var, let and const</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Sat, 08 Aug 2020 09:08:31 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-es6-var-let-and-const-3f03</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-es6-var-let-and-const-3f03</guid>
      <description>&lt;p&gt;Hi there folks! Continuing the JavaScript learning from &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;, this time we move ahead from the Basic JavaScript and would start with the ES6. ECMAScript 6 or ES6 is the major JS version which introduced a variety of helpful features, released in 2015. Going with this, we will explore those features and how to use them in upcoming posts as well.&lt;/p&gt;

&lt;p&gt;This post mainly covers the caveats regarding the variable declaration and how ES6 changes its behaviour. So, let's start without delaying anything.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Difference between &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; keywords&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;The usage of &lt;code&gt;var&lt;/code&gt; had some issues, which we'll come across little by little. However, one potential issue is that variables declared with &lt;code&gt;var&lt;/code&gt; can be overridden without any errors. For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myName = Prashant;
var myName = Akash; 
console.log(myName);  // Akash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you may see, we've overridden the variable &lt;code&gt;myName&lt;/code&gt;, but JavaScript didn't raise any error. With application with larger source code, we may accidentally overwrite a variable, which we don't intend do. This could lead to unexpected behaviours and difficulties in debugging.&lt;/p&gt;

&lt;p&gt;To resolve this, ES6 provides the keyword &lt;code&gt;let&lt;/code&gt; for variable declaration. If we were to use &lt;code&gt;let&lt;/code&gt; instead of &lt;code&gt;var&lt;/code&gt; in the above example, the second initialization would lead to an error.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We can also use the &lt;code&gt;"use_strict"&lt;/code&gt; to enable the strict mode. This would catch common mistakes and unsafe actions.&lt;br&gt;
For e.g.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"use strict";&lt;/code&gt;&lt;br&gt;
&lt;code&gt;x = 3.14; // throws an error because x is not declared&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Scope differences between &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; keywords&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;If we go back and try to remember the behaviour of the &lt;code&gt;var&lt;/code&gt; keyword w.r.t scopes, we know that any variable declared with it is global and if declared within a function, it's scope is limited to that function only.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; behaves in a similar way, but with some extra features. For e.g. when we declare a loop variable using the &lt;code&gt;var&lt;/code&gt; keyword, that becomes global. However, declaring the same, using &lt;code&gt;let&lt;/code&gt; would result in its scope limited to the block of the loop.&lt;/p&gt;

&lt;p&gt;Let's see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [1, 2, 3, 4];
for (var i = 0; i &amp;lt; arr.length; i++) {
   arr[i]++;
}
console.log(i);  // returns 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You may notice that the variable &lt;code&gt;i&lt;/code&gt; is accessible outside the loop. If you were to use it again elsewhere, it'd be using the updated value, which might result in unexpected behaviour. However, with &lt;code&gt;let&lt;/code&gt;, this doesn't happen i.e. the loop variable &lt;code&gt;i&lt;/code&gt; will only be accessible within the loop block and not outside of it.&lt;/p&gt;

&lt;p&gt;Let's see another example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function checkScope() {
  'use strict';
  let i = 'function scope';
  if (true) {
    let i = 'block scope';
    console.log('Block scope i is: ', i);
  }
  console.log('Function scope i is: ', i);
  return i;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before we discuss the output of the above code, a doubt may arise to you. You may ask, that &lt;code&gt;let&lt;/code&gt; should not allow the re-declaration of the variable &lt;code&gt;i&lt;/code&gt; at all. The catch here is that it restricts the same declaration within the same scope. In the above piece of code, the second initialization is within a local scope. Hence &lt;code&gt;let&lt;/code&gt; does not raise any error. &lt;/p&gt;

&lt;p&gt;Yes, if you would've tried to initialize it outside of the &lt;code&gt;if&lt;/code&gt; block, it should have a complaint about that. Now, let's move to the output of the above code.&lt;/p&gt;

&lt;p&gt;The output of this function would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Block scope i is:  block scope
Function scope i is:  function scope
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And can you guess which value of &lt;code&gt;i&lt;/code&gt; would be returned? Think!&lt;/p&gt;

&lt;p&gt;The answer is &lt;code&gt;function scope&lt;/code&gt; would be returned, as the variable &lt;code&gt;i&lt;/code&gt; within the local scope of &lt;code&gt;if&lt;/code&gt; condition is not visible outside of its block. Hence the global &lt;code&gt;i&lt;/code&gt; is returned.&lt;/p&gt;

&lt;p&gt;What if I change the code slightly to this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function checkScope() {
  'use strict';
  let i = 'function scope';
  if (true) {
    i = 'block scope';
    console.log('Block scope i is: ', i);
  }
  console.log('Function scope i is: ', i);
  return i;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What do you think, would be returned now? Before that, did you notice the change? I removed the &lt;code&gt;let&lt;/code&gt; keyword from inside of &lt;code&gt;if&lt;/code&gt; block. That's it. Now, think!&lt;/p&gt;

&lt;p&gt;This time, &lt;code&gt;block scope&lt;/code&gt; would be returned. I see you ask why? Well, it's because this time instead of declaring a new variable with the same name in scope, we are instead, overriding the global variable &lt;code&gt;i&lt;/code&gt;. Hence, the changes to it are reflected outside of the function.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Variable declaration using the &lt;code&gt;const&lt;/code&gt; keyword&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;ES6 not only provides the &lt;code&gt;let&lt;/code&gt; keyword to declare a variable but also a &lt;code&gt;const&lt;/code&gt; keyword to do so. The difference is that the variables declared with &lt;code&gt;const&lt;/code&gt; are &lt;strong&gt;read-only&lt;/strong&gt;. It means that once the variable is declared, it cannot be reassigned. These variable act as a constant value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is a common practice to declare constants using the uppercase letters with words separated with underscores.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The use of &lt;code&gt;const&lt;/code&gt; helps us to avoid accidentally changing the constants within a program. An example of its use can be seen as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const PI = 3.14;
PI = 2.17; // throws an error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Mutating arrays declared with const&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We should know that objects, arrays or functions would be mutable when assigned to a variable using &lt;code&gt;const&lt;/code&gt;. It is actually the variable identifier, which cannot be reassigned.&lt;/p&gt;

&lt;p&gt;Let's see an example for an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const s = [4, 5, 6, 7];
s = [1, 2, 3]; // throws an error, as assignment to a const variable
s[4] = 8; // this would work fine
console.log(s);  // [4, 5, 6, 8]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The thing which is to be noticed here is that the values within the array are mutable. Even though we change the array elements, the variable &lt;code&gt;s&lt;/code&gt; would still point to the same memory location. Only the value to that memory location has been changed.&lt;/p&gt;

&lt;p&gt;Yes, if we would try to point &lt;code&gt;s&lt;/code&gt; to some other memory location i.e. some other array or value, it would throw an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Preventing object mutation&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;From the above, we found that &lt;code&gt;const&lt;/code&gt; declaration doesn't actually protect the data from mutation. It only prevents the variable identifier to point to some other location instead.&lt;/p&gt;

&lt;p&gt;However, if we want to prevent data mutation within an object, JavaScript provides the function &lt;code&gt;Object.freeze&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After the object is frozen, any attempt to add, update or delete the properties would be rejected without any error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function freezeObj() {
  let MATH_CONSTANTS = {
    PI: 3.14
  };

  Object.freeze(MATH_CONSTANTS);

  MATH_CONSTANTS.PI = 99;  // ignored and mutation won't be allowed
  return MATH_CONSTANTS.PI;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above would still return 3.14. However, if you'd use the strict mode, it will throw an error.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;At the end of this post, we found the major differences between &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords and where and why of using them. Further, we found that we can also prevent any data mutation using &lt;code&gt;Object.freeze()&lt;/code&gt; method which comes in handy at times.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/"&gt;Introduction to the ES6 Challenges&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll meet next time with other ES6 features so that we can improve our JS knowledge further. Till then be curious and keep learning. :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>100daysofcode</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>[freeCodeCamp] Basic JavaScript - Recursion, random numbers, parseInt function</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Mon, 27 Jul 2020 08:35:36 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-basic-javascript-recursion-random-numbers-parseint-function-2mb5</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-basic-javascript-recursion-random-numbers-parseint-function-2mb5</guid>
      <description>&lt;p&gt;Yo! How are you, people? I hope everyone is doing great. This time, it is another short post covering different concepts in brief. This is in continuation of my JavaScript learning from &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;. I am really enjoying it and I am certain you are as well. :)&lt;/p&gt;

&lt;p&gt;Let's continue towards the actual content without making any delay.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Recursion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;We have recently learned about JavaScript &lt;a href="https://dev.to/gutsytechster/freecodecamp-basic-javascript-loops-m6m"&gt;loops&lt;/a&gt;. The loops can be replaced by the concept of Recursion.&lt;/p&gt;

&lt;p&gt;Recursion implies the concept where a function can be written in terms of itself. For example - when you want to find a &lt;a href="https://en.wikipedia.org/wiki/Factorial"&gt;factorial&lt;/a&gt; of a number, you can write it using the loop as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function factorial(n) {
  var result = 1;
  for(var i = 1; i &amp;lt;= n; i++) {
     result *= i;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result would store the result as &lt;code&gt;1 * 2 * 3 * 4 * 5&lt;/code&gt; ie &lt;code&gt;120&lt;/code&gt; if &lt;code&gt;n = 5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The above works perfectly fine. But as we are trying to grasp the concept of recursion, we aim to write this function in term of itself. Before that, let's understand the factorial concept&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5! = 5 * 4 * 3 * 2 * 1
4! = 4 * 3 * 2 * 1
3! = 3 * 2 * 1
2! = 2 * 1
1! = 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I have written down the factorial expansion of number from &lt;code&gt;5&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;. Can you see a pattern in it? I am sure you can, just give it some time.&lt;/p&gt;

&lt;p&gt;Can we write the above structure like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5! = 5 * 4!  // 5 * (5 - 1)!
4! = 4 * 3!  // 4 * (4 - 1)!
3! = 3 * 2!  // 3 * (3 - 1)!
2! = 2 * 1!  // 2 * (2 - 1)!
1! = 1 * 0!  // 1 * (1 - 1)!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we have written the &lt;code&gt;n!&lt;/code&gt; in terms of &lt;code&gt;(n - 1)!&lt;/code&gt;. This pattern is used by recursion. We can rewrite the above function as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function factorial(n) {
  if (n &amp;lt;= 1) {
     return 1;   // as 1! = 1
  } else {
     return n * factorial(n - 1);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the condition &lt;code&gt;n &amp;lt;= 1&lt;/code&gt; is called a &lt;strong&gt;&lt;em&gt;base condition&lt;/em&gt;&lt;/strong&gt; and every recursive function should have a base condition to mark the end of the recursive loop.  When the value of &lt;code&gt;n&lt;/code&gt; would reach to &lt;code&gt;1&lt;/code&gt;, we get the value of the &lt;code&gt;1!&lt;/code&gt; and then this can be substituted to evaluate the value of &lt;code&gt;2!&lt;/code&gt; and like this, up to &lt;code&gt;n!&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The function calls can be seen as(for &lt;code&gt;n = 5&lt;/code&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;factorial(5) -&amp;gt; 5 * factorial(4) -&amp;gt; 5 * 4 * factorial(3) -&amp;gt; 5 * 4 * 3 * factorial(2) -&amp;gt; 5 * 4 * 3 * 2 * factorial(1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When the function call reaches to &lt;code&gt;factorial(1)&lt;/code&gt;, the function returns a finite value, instead of another recursive call, as it was doing up till now. This is then, substituted to get the final value.&lt;/p&gt;

&lt;p&gt;I hope, I was able to explain the concept in simple words.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Generating random numbers in JavaScript&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;Generating random fraction&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript has a &lt;code&gt;Math.random()&lt;/code&gt; function which generates a random decimal number in the range [0, 1). The brackets imply that it includes 0, but excludes 1. So we may get a 0 as an output from this function, but not 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function generateRandomNumber() {
    return Math.random();  // returns a random number -&amp;gt; 0.78379758
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;Generating random whole numbers&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can also generate whole numbers within a range by applying some mathematics. Let's see&lt;br&gt;
    - Using &lt;code&gt;Math.random()&lt;/code&gt; to generate a random decimal.&lt;br&gt;
    - Multiplying that with the upper bound of range ie &lt;code&gt;n&lt;/code&gt;.&lt;br&gt;
    - Using &lt;code&gt;Math.floor&lt;/code&gt; to round the decimal to the nearest whole number.&lt;/p&gt;

&lt;p&gt;That's it. With this process, we can get a random whole number between &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n -1&lt;/code&gt; as &lt;code&gt;n&lt;/code&gt; won't be counted in the initial &lt;code&gt;Math.random()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;An example to generate the random number between 0 to 20 would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function generateRandomWholeNumber() {
   return Math.floor(Math.random() * 20);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This would generate a random number between [0, 20) for us. :)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;Generating random whole numbers within a range&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Until now, we could generate the whole number from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n&lt;/code&gt;. But what if we also want to use some other minimum value, other than &lt;code&gt;0&lt;/code&gt;. It's quite possible, using another mathematics trick.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.floor(Math.random() * (max - min + 1)) + min
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the formula to generate a random whole number between &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; values.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;&lt;code&gt;parseInt&lt;/code&gt; function in JavaScript&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;parseInt&lt;/code&gt; function in JavaScript is used to convert a string into an integer. For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var intValue = parseInt("00321");  // return 321
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above statement showed that the final value is an integer. If a string couldn't be converted to an integer, it returns &lt;code&gt;NaN&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;parseInt&lt;/code&gt; method, takes an optional argument &lt;code&gt;radix&lt;/code&gt;, which specifies the base of the number provided in the string. It can have values between &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;36&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var intValue = parseInt("1011", 2);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above statement won't return &lt;code&gt;1011&lt;/code&gt; integer value, but &lt;code&gt;11&lt;/code&gt;. This is because we have provided base &lt;code&gt;2&lt;/code&gt; which tells that it is a binary number, whose decimal equivalent is &lt;code&gt;11&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;At the end of this post, we got the basic idea about Recursion and how does one can implement it. Apart from it, we also learned about generating random numbers in JavaScript and using parseInt function to convert a value to an integer value.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/"&gt;Introduction to JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=lkOXlVgKHYM&amp;amp;list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&amp;amp;index=13&amp;amp;t=0s"&gt;Conquering freeCodeCamp – Basic JavaScript (Part 2) – Live Stream #12&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This was a short post, but with this, we have covered the basic JavaScript section of freeCodeCamp. We'll meet next time with another post covering the concepts from the next section. Till then be curious and keep learning. :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>100daysofcode</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>[freeCodeCamp] Basic JavaScript - Loops</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Wed, 15 Jul 2020 11:06:05 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-basic-javascript-loops-m6m</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-basic-javascript-loops-m6m</guid>
      <description>&lt;p&gt;Hey fellas! Let's keep continue what we've learned in the previous posts. This is another post in our JavaScript learning via &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;. In this post, we are going to cover various loop constructs, JavaScript provides us with. How one uses them and implements them.&lt;/p&gt;

&lt;p&gt;Let's start with the post.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Loops&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;Loops are basically used when we want to run some piece of code multiple times. There are various ways to achieve that. Let's discuss them separately&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h2&gt;&lt;u&gt;&lt;code&gt;for&lt;/code&gt; loop&lt;/u&gt;&lt;/h2&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop is one of the commonly used loops in any programming language. The for loop has a definite structure which is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for ([initialization]; [condition]; [final-expression])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are three main components to the &lt;code&gt;for&lt;/code&gt; loop which are - initialization, condition, and final-expression. Each of these expressions is optional to be used.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;initialization&lt;/code&gt; is executed only once before the loop executes. It is basically the initialization of the loop variable.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;condition&lt;/code&gt; is the statement which is executed from the starting to the end of the loop. The loop will keep executing until this &lt;code&gt;condition&lt;/code&gt; evaluates to &lt;code&gt;true&lt;/code&gt;. Once this condition evaluates to &lt;code&gt;false&lt;/code&gt;, the loop will be terminated.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;final-expression&lt;/code&gt; is executed at the end of each loop iteration, which changes the loop variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var sum = 0;
for( var i = 0; i &amp;lt; 5; i++){
  sum += i;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above loop will execute &lt;code&gt;5&lt;/code&gt; times, adding the current value to &lt;code&gt;i&lt;/code&gt; to the &lt;code&gt;sum&lt;/code&gt; in each iteration.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We initialize the loop variable &lt;code&gt;i&lt;/code&gt; with &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It'll then check for the condition &lt;code&gt;i &amp;lt; 5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;As the condition evaluates to &lt;code&gt;true&lt;/code&gt;, it enters the loop body and adds the value of &lt;code&gt;i&lt;/code&gt; to &lt;code&gt;sum&lt;/code&gt; by executing &lt;code&gt;sum += i&lt;/code&gt; statement.&lt;/li&gt;
&lt;li&gt;As soon as the last statement ends, the control goes to the &lt;code&gt;final-expression&lt;/code&gt; i.e. &lt;code&gt;i++&lt;/code&gt; which increments &lt;code&gt;i&lt;/code&gt; by &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After this, the condition is checked again with the updated value of &lt;code&gt;i&lt;/code&gt; and the loop keeps on executing until the condition evaluates to &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the last iteration, the value of &lt;code&gt;i&lt;/code&gt; would be &lt;code&gt;5&lt;/code&gt;. This would fail the condition &lt;code&gt;i &amp;lt; 5&lt;/code&gt; and the control will come out of the loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the end of the loop, the sum will hold the value &lt;code&gt;0+1+2+3+4&lt;/code&gt; i.e. &lt;code&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h2&gt;&lt;u&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/u&gt;&lt;/h2&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; loop runs repeatedly as long as a specified condition evaluates to &lt;code&gt;true&lt;/code&gt;. Once it returns &lt;code&gt;false&lt;/code&gt;, the execution stops. The above example can also be written using the &lt;code&gt;while&lt;/code&gt; loop as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var sum = 0;
var i = 0;
while (i &amp;lt; 5) {
  sum += i;
  i++;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's break it down into smaller steps&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We set the variable &lt;code&gt;i = 0&lt;/code&gt;, which act as an &lt;code&gt;initialization&lt;/code&gt; step just as in &lt;code&gt;for&lt;/code&gt; loop.&lt;/li&gt;
&lt;li&gt;We run the &lt;code&gt;while&lt;/code&gt; loop until the value of &lt;code&gt;i&lt;/code&gt; is less than &lt;code&gt;5&lt;/code&gt;.  The condition &lt;code&gt;i &amp;lt; 5&lt;/code&gt; can be seen as the &lt;code&gt;condition&lt;/code&gt; expression like in &lt;code&gt;for&lt;/code&gt; loop.&lt;/li&gt;
&lt;li&gt;We define the &lt;code&gt;final-statement&lt;/code&gt; i.e. &lt;code&gt;i++&lt;/code&gt; which must be executed so that the loop variable changes and condition evaluates to &lt;code&gt;false&lt;/code&gt; at some point in time.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Make sure to update the loop variable within the body of the loop itself. If you don't, the loop condition will never evaluate to &lt;code&gt;false&lt;/code&gt; and the loop will be executed infinitely.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h2&gt;&lt;u&gt;&lt;code&gt;do...while&lt;/code&gt; loop&lt;/u&gt;&lt;/h2&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apart from the &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loops, JavaScript also offers a &lt;code&gt;do...while&lt;/code&gt; loop. It is called so because it &lt;code&gt;do&lt;/code&gt; the one pass of the loop at least once irrespective of the condition and then keep on executing till the &lt;code&gt;while&lt;/code&gt; condition evaluates to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's re-write the above example as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var sum = 0;
var i = 1;
do{
  sum += i;
  i++;
} while (i &amp;lt; 0);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, if you would notice, the condition &lt;code&gt;i &amp;lt; 0&lt;/code&gt; will be &lt;code&gt;false&lt;/code&gt; when &lt;code&gt;i = 1&lt;/code&gt;. However, the loop will still execute once. The first iteration would execute irrespective of the condition. Once the flow reaches the &lt;code&gt;while&lt;/code&gt; condition, the loop terminates.&lt;/p&gt;

&lt;p&gt;I hope, you understood the difference between the normal &lt;code&gt;while&lt;/code&gt; and &lt;code&gt;do...while&lt;/code&gt; loop. The &lt;code&gt;while&lt;/code&gt; loop in such case would've aborted the loop without executing at all.&lt;/p&gt;

&lt;p&gt;Notice the semicolon(&lt;code&gt;;&lt;/code&gt;) after the &lt;code&gt;while&lt;/code&gt; condition. You should end a &lt;code&gt;do...while&lt;/code&gt; loop like this.&lt;/p&gt;




&lt;p&gt;Any of the loop's final statement doesn't always need to increment by 1. It can be &lt;code&gt;i--&lt;/code&gt; or &lt;code&gt;i+=2&lt;/code&gt;  etc. i.e. it can be any valid expression. Just make sure that this always leads to the loop condition to evaluate to &lt;code&gt;false&lt;/code&gt; at some point in time.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Iterating through an array&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Iterating an array is one of the most common and widely used tasks to do, which involves the usage of loops. One can do that using the &lt;code&gt;for&lt;/code&gt; loop as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [10, 9, 8, 7, 6];
for (var i = 0; i &amp;lt; arr.length; i++) {
   console.log(arr[i]);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above piece of code would print each element of the array to the console. Notice the condition &lt;code&gt;i &amp;lt; arr.length&lt;/code&gt;, we are not using &lt;code&gt;&amp;lt;=&lt;/code&gt; because the last element of the array will have an index &lt;code&gt;length - 1&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Nesting &lt;code&gt;for&lt;/code&gt; loops&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loops can be nested to iterate through a multi-dimensional array. Suppose we have an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [
  [1,2], [3,4], [5,6]
];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we want to print each element of the sub-array, The approach would be to first iterate over the outer array using some loop variable, let's say &lt;code&gt;i&lt;/code&gt;. Now for each &lt;code&gt;arr[i]&lt;/code&gt; which would be an array itself, for e.g. &lt;code&gt;arr[0] = [1, 2]&lt;/code&gt;, we'll take another loop variable &lt;code&gt;j&lt;/code&gt;, which would iterate through the inner arrays.&lt;/p&gt;

&lt;p&gt;Let's code it down to understand bit clearly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (var i = 0; i &amp;lt; arr.length; i++){
  for (var j = 0; j &amp;lt; arr[i].length; j++){
     console.log(arr[i][j]);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Did you understand? I am sure the outer loop would not be an issue. While going through the inner loop, we go through the length of each sub-array i.e. &lt;code&gt;arr[i].length&lt;/code&gt; and further, we access each individual element of &lt;code&gt;arr&lt;/code&gt; using the nested bracket notation.&lt;br&gt;
For e.g. &lt;code&gt;arr[0][0]&lt;/code&gt; would be &lt;code&gt;1&lt;/code&gt;. Similarly, &lt;code&gt;arr[0][1]&lt;/code&gt; would be &lt;code&gt;2&lt;/code&gt;. Using the same pattern, we print out each element to the console.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;In the end, we learned various ways to implement loops in JavaScript and performing iterations through an array. Loops are one of the basic fundamentals of any programming language and are useful in daily programming.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/"&gt;Introduction to JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=lkOXlVgKHYM&amp;amp;list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&amp;amp;index=13&amp;amp;t=0s"&gt;Conquering freeCodeCamp – Basic JavaScript (Part 2) – Live Stream #12&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s meet next time with another JavaScript post covering some other fundamentals. Till then be curious and keep learning.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>100daysofcode</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>[freeCodeCamp] Basic JavaScript - Objects</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Fri, 10 Jul 2020 08:24:51 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-basic-javascript-objects-1i60</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-basic-javascript-objects-1i60</guid>
      <description>&lt;p&gt;Howdy folks! Let's continue from the previous posts of the series focused on learning JavaScript. We are learning JavaScript fundamentals from the &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;. I am sure, you are enjoying this as well.&lt;/p&gt;

&lt;p&gt;This post will cover the basics of Objects in JavaScript. Let's know about them without any delay.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Objects&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;Objects in JavaScript are used to store data in a structured way. We can see them as an array with the difference being that instead of using indexes to access or modify the data, objects use &lt;code&gt;properties&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, you may ask, what are &lt;code&gt;properties&lt;/code&gt;? These can be referred to as a key among the key-value pairs that exist in an object. It will be more clear with an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var cat = {
  "name": "Whiskers",
  "legs": 4,
  "tails": 1,
  "enemies": ["Water", "Dogs"]
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;cat&lt;/code&gt; is an object. It contains the key-value pairs separated by a colon(&lt;code&gt;:&lt;/code&gt;). The values to the left of the colon are what we call as &lt;code&gt;properties&lt;/code&gt;. And values to the right of the colon are its value.&lt;/p&gt;

&lt;p&gt;Notice the semicolon at the end of object definition(just after the closing bracket).&lt;/p&gt;

&lt;p&gt;As you may notice, the value for the property &lt;code&gt;enemies&lt;/code&gt; is an array. This implies that values can be anything. It may even be an object itself.&lt;/p&gt;

&lt;p&gt;Now, some quirks about properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Although, in the above example, the properties are defined as strings. But it isn't necessary. You may keep an integer value as a property as well.&lt;/li&gt;
&lt;li&gt;If the property is a single word string, you can even emit quotes around it, for e.g.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var someObject = {
   user: "Prashant"
}:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;user&lt;/code&gt; is a valid property. This happens because JavaScript will automatically typecast them into strings.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Accessing object properties&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We can access an object property using two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;u&gt;Using Dot(&lt;code&gt;.&lt;/code&gt;) notation&lt;/u&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we know the name of the property, we can access the value associated with it using the dot notation. Let's consider the above &lt;code&gt;cat&lt;/code&gt; object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat.name; // returns "Whiskers"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;u&gt;Using Bracket(&lt;code&gt;[]&lt;/code&gt;) notation&lt;/u&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just like arrays, we can access object properties using square brackets, by enclosing the property name. For e.g. the above statement can also be written as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat["name"];  // returns "Whiskers"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The main use cases for this particular notation are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the property name has spaces in it. As we can't use dot notation in that case.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When we want to access the property, which is stored as a value in a variable.&lt;br&gt;
For e.g.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var dogs = {
  Fido: "Mutt",  Hunter: "Doberman",  Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[myDog];
console.log(myBreed); // "Doberman"
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, we stored the property &lt;code&gt;Hunter&lt;/code&gt; as a variable and then accessed it using the variable name instead of the property name directly.&lt;br&gt;
This is also useful when the property name is collected dynamically.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Updating Object properties&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We can update object properties, by setting it to something else. Yes, it's as simple as that. We know how to update the variable, just like that.&lt;/p&gt;

&lt;p&gt;Let's see a small example, for quick understanding&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var cat = {
  "name": "Whiskers",
  "legs": 4,
  "tails": 1,
  "enemies": ["Water", "Dogs"]
};
cat.name = "Camper";
cat["name"] = "Camper";
cat.name; // Now it will return "Camper" :)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, you can set the values by using any of the dot or bracket notations.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Adding properties to an Object&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Adding a property to an existing object is exactly similar to how we update an object. We set the property name to value and since the property doesn't exist, JavaScript will create that for us. Isn't that awesome? :)&lt;/p&gt;

&lt;p&gt;Let's add a voice property to our cat object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat.voice = "meww";
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next time, when you'll try to access the &lt;code&gt;voice&lt;/code&gt; property, you'd get &lt;code&gt;meww&lt;/code&gt; in return.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Deleting properties of an Object&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We can also delete a property of an object. This can be achieved using the &lt;code&gt;delete&lt;/code&gt; keyword. Let's understand with an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;delete cat.voice;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This would delete the property &lt;code&gt;voice&lt;/code&gt; of &lt;code&gt;cat&lt;/code&gt; object.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Objects find their usage when the data in the form of a dictionary is to be maintained.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Checking object for a property&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We might need to know if some property of an object exists or not. This can be easily done with the &lt;code&gt;.hasOwnProperty(propertyName)&lt;/code&gt; method. The method name is itself quite self-described.&lt;/p&gt;

&lt;p&gt;We can use this method as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat.hasOwnProperty("voice"); // returns false as we have deleted it
cat.hasOwnProperty("legs"); // return true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;Objects can be used to handle flexible data. An example from freeCodeCamp itself for a complex object can be seen as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var ourMusic = [
  {
    "artist": "Daft Punk",
    "title": "Homework",
    "release_year": 1997,
    "formats": [ 
      "CD", 
      "Cassette", 
      "LP"
    ],
    "gold": true
  }
];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;ourMusic&lt;/code&gt; is an array of objects, currently containing the single object. Further, &lt;code&gt;format&lt;/code&gt; property is also an array, which can be seen as a nested array. This is just one example of how objects can be useful to store a structured but flexible data.&lt;/p&gt;

&lt;p&gt;Accessing nested array just like above can be achieved by chaining the array bracket and dot notation. For e.g., if we have to get the 2nd format of the 1st musical album, we'd do something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var format = ourMusic[0].formats[1]; // returns "Cassette"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;With the end of this short post, we have acquired some knowledge about the Objects in JavaScript. They play an important role in daily JavaScript coding. I am sure, it'll be really helpful to anyone learning JavaScript.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/"&gt;Introduction to JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=lkOXlVgKHYM&amp;amp;list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&amp;amp;index=13&amp;amp;t=0s"&gt;Conquering freeCodeCamp – Basic JavaScript (Part 2) – Live Stream #12&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s meet next time with another JavaScript post covering some other fundamentals. Till then be curious and keep learning.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>100daysofcode</category>
      <category>react</category>
      <category>angular</category>
    </item>
    <item>
      <title>[freeCodeCamp] Basic JavaScript - Conditional Logic, Logical Operators, switch statement</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Sun, 28 Jun 2020 10:06:19 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-basic-javascript-conditional-logic-logical-operators-switch-statement-3g36</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-basic-javascript-conditional-logic-logical-operators-switch-statement-3g36</guid>
      <description>&lt;p&gt;Hey fellas! Guess what? This time we are going to dive into one of the main concepts in any programming language i.e. implement conditional logic. This is in continuation of my JavaScript learning from &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;. We've learned quite a few concepts in the series and going to know more about them. &lt;/p&gt;

&lt;p&gt;In the previous &lt;a href="https://dev.to/gutsytechster/freecodecamp-basic-javascript-queues-boolean-comparison-operators-47ce"&gt;post&lt;/a&gt; in the series, we learned about &lt;code&gt;Boolean&lt;/code&gt; and comparison operators which are used as the entry point for conditional logic.&lt;/p&gt;

&lt;p&gt;Let's start without any delay!&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Implement Conditional Logic&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h2&gt;&lt;u&gt;&lt;code&gt;if&lt;/code&gt; statement&lt;/u&gt;&lt;/h2&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can execute a statement when some condition meets using the &lt;code&gt;if&lt;/code&gt; statement. If the condition is met, then the code within the &lt;code&gt;if&lt;/code&gt; block would be executed.&lt;/p&gt;

&lt;p&gt;The condition will always return either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. Let's see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myCondition(condition) {
  if (condition) {
    return "True";
  return "False";
}
myCondition(true);  // returns "True"
myCondition(false);  // returns "False"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you may see that when the condition is &lt;code&gt;true&lt;/code&gt;, the statement &lt;code&gt;return "True"&lt;/code&gt; is executed. When we provide the condition as &lt;code&gt;false&lt;/code&gt;, the statement outside of &lt;code&gt;if&lt;/code&gt; statement is executed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h2&gt;&lt;u&gt;&lt;code&gt;else&lt;/code&gt; statement&lt;/u&gt;&lt;/h2&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An &lt;code&gt;else&lt;/code&gt; statement is used to execute some piece of code when the specified condition within the &lt;code&gt;if&lt;/code&gt; statement does not hold &lt;code&gt;true&lt;/code&gt;.  In such cases, we define an &lt;code&gt;else&lt;/code&gt; statement along with an &lt;code&gt;if&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num = 5;
if (num &amp;lt; 5) {
  return true;
} else {
  return false:
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since the condition &lt;code&gt;num &amp;lt; 5&lt;/code&gt; would return &lt;code&gt;false&lt;/code&gt;, the block within the &lt;code&gt;if&lt;/code&gt; statement is not executed, but the flow goes within the &lt;code&gt;else&lt;/code&gt; block.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h2&gt;&lt;u&gt;&lt;code&gt;else if&lt;/code&gt; statement&lt;/u&gt;&lt;/h2&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we have multiple statements to be checked, we can create an &lt;code&gt;if&lt;/code&gt; - &lt;code&gt;else if&lt;/code&gt; - &lt;code&gt;else&lt;/code&gt; ladder.&lt;/p&gt;

&lt;p&gt;The first condition would go with &lt;code&gt;if&lt;/code&gt; statement, subsequent conditions can go with multiple &lt;code&gt;else if&lt;/code&gt; statements and finally an &lt;code&gt;else&lt;/code&gt; statement, which would be executed if none of the conditions is met.&lt;/p&gt;

&lt;p&gt;A small example of this can be seen as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (num &amp;gt; 15) {
  return "Bigger than 15";
} else if (num &amp;lt; 5) {
  return "Smaller than 5";
} else {
  return "Between 5 and 15";
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Please ensure the order of statements when executing the &lt;code&gt;if&lt;/code&gt;- &lt;code&gt;else if&lt;/code&gt;- &lt;code&gt;else&lt;/code&gt; ladder, the conditions are checked from top to bottom, and once entered in a block, all other conditions won't be checked. They will simply be skipped.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Ternary Operator&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript also provides an operator for a one-liner if-else statement. Its syntax is like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;condition ? statement-if-true : statement-if-false;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's consider the following example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (num &amp;lt; 5) {
  return true; 
} else { 
  return false;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This can be written using the ternary operator as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return num &amp;lt; 5 ? true : false;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If the condition evaluates to &lt;code&gt;true&lt;/code&gt;, the expression after &lt;code&gt;?&lt;/code&gt; is executed otherwise the expression after &lt;code&gt;:&lt;/code&gt; is executed.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Logical Operators in JavaScript&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;When we need to test more than one thing at a time, we can use logical operators instead of using multiple &lt;code&gt;if&lt;/code&gt; statements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;AND operator (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;)&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The AND operator returns &lt;code&gt;true&lt;/code&gt; if both of its operands returns &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt; otherwise. It's pretty straightforward. Let's jump to an example.&lt;/p&gt;

&lt;p&gt;Suppose we have the following piece of code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num = 5;
if (num &amp;gt; 1) {
  if (num &amp;gt; 4) {
    return true;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above piece of code can be simplified and can be written in the following way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num = 5;
if (num &amp;gt; 1 &amp;amp;&amp;amp; num &amp;gt; 4) {
  return true;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Did you get it now? It will check for the two conditions provided to it and if they individually return &lt;code&gt;true&lt;/code&gt;, the whole condition would return &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;OR operator (&lt;code&gt;||&lt;/code&gt;)&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The OR operator returns &lt;code&gt;true&lt;/code&gt; if any of the operands returns &lt;code&gt;true&lt;/code&gt;.  For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num = 5;
if (num &amp;gt; 1 || num &amp;lt; 4) {
   return true;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above example, the condition &lt;code&gt;num &amp;gt; 1&lt;/code&gt; would return &lt;code&gt;true&lt;/code&gt; as &lt;code&gt;5&lt;/code&gt; is indeed greater than &lt;code&gt;1&lt;/code&gt;. However, the other condition would return &lt;code&gt;false&lt;/code&gt; as &lt;code&gt;5&lt;/code&gt; is not less than &lt;code&gt;4&lt;/code&gt;. But since one of the conditions used with OR operator evaluates to &lt;code&gt;true&lt;/code&gt;, the whole condition would return &lt;code&gt;true&lt;/code&gt;, and the statement within the &lt;code&gt;if&lt;/code&gt; block will be executed.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Switch statement&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;JavaScript provides a &lt;code&gt;switch&lt;/code&gt; statement, which works as if you would use multiple &lt;code&gt;if&lt;/code&gt; statements with each condition having a check against strict equality operator &lt;code&gt;===&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The argument passed to the &lt;code&gt;switch&lt;/code&gt; statement can have multiple values with each value would be treated as a case. Let's see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch(lowercaseLetter) {
  case "a":
    console.log("A");
    break;
  case "b":
    console.log("B");
    break;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;lowercaseletter&lt;/code&gt; can have multiple &lt;code&gt;case&lt;/code&gt;(s), when a case is matched, the statement(s) under that case is executed. Please make sure to write a &lt;code&gt;break&lt;/code&gt; statement at the end of each case, which tells JavaScript to stop executing, otherwise, all other cases after the matched case would be executed, until it finds the &lt;code&gt;break&lt;/code&gt; statement or the number of cases ends.&lt;/p&gt;

&lt;p&gt;If the value of &lt;code&gt;lowercaseletter&lt;/code&gt; is &lt;code&gt;"a"&lt;/code&gt;, then it would go with the first &lt;code&gt;case&lt;/code&gt; statement and if it comes out to be &lt;code&gt;"b"&lt;/code&gt;, then it would go with the second &lt;code&gt;case&lt;/code&gt; statement.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Using &lt;code&gt;default&lt;/code&gt; in the switch statement&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;At times, we may not be able to decide all cases. In such a situation, we can define a &lt;code&gt;default&lt;/code&gt; case that would be executed, if the &lt;code&gt;switch&lt;/code&gt; value does not find any matching case. You can think of it as an &lt;code&gt;else&lt;/code&gt; statement in an &lt;code&gt;if&lt;/code&gt;-&lt;code&gt;else&lt;/code&gt; ladder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;default&lt;/code&gt; is a keyword in JavaScript, i.e. it has a special meaning. Let's see an example of this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch(value){
  case option1:
     statement1;
     break;
  case option2:
     statement2;
     break;
  default:
     defaultstatement;
     break;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Multiple Identical Options in the Switch statement&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;It is possible that we have the same set of statements to be executed for multiple cases. If we represent that in an &lt;code&gt;if&lt;/code&gt;-&lt;code&gt;else&lt;/code&gt; statement, it would be a situation like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var val;
if (val === 1 || val === 2 || val === 3) {
   console.log("Stop");
} else if (val === 4) {
    console.log("Start");
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we have to represent the above &lt;code&gt;if&lt;/code&gt;-&lt;code&gt;else&lt;/code&gt; statement using the &lt;code&gt;switch&lt;/code&gt;-&lt;code&gt;case&lt;/code&gt; statements, it would look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch(val) {
  case 1:
  case 2:
  case 3:
    console.log("Stop");
    break;
  case 4:
    console.log("Start");
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we have used the quirk that in absence of a &lt;code&gt;break&lt;/code&gt; statement, the subsequent &lt;code&gt;case&lt;/code&gt;(s) are executed until a &lt;code&gt;break&lt;/code&gt; statement is found or the number of cases ends.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Returning Boolean from a function&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We already know that a function can &lt;code&gt;return&lt;/code&gt; value and it can be anything. However, when you want to return a boolean value i.e. either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. One way you'd think to do is like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isEqual(a, b) {
  if (a === b){
    return true;
  } else {
   return false;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And this is perfectly fine and works, which matters the most. However, you can achieve the same with another better approach. Can you think of it? think, think...&lt;/p&gt;

&lt;p&gt;Now, stop thinking. Let's see the better way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isEqual(a, b) {
  return a === b;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Tada! We already know that &lt;code&gt;a === b&lt;/code&gt; would return a boolean value, which is the only thing we want, don't we? :)&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;With the end of this a bit long post, we have acquired knowledge about how we can implement the conditional logic using various JavaScript constructs. Apart from it, we found a better way to return a boolean from a function(trust me, you'd do this more often than you think.)&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/"&gt;Introduction to JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=lkOXlVgKHYM&amp;amp;list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&amp;amp;index=13&amp;amp;t=0s"&gt;Conquering freeCodeCamp - Basic JavaScript (Part 2) - Live Stream #12&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s meet next time with another JavaScript post covering other JavaScript concepts. Till then be curious and keep learning!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>[freeCodeCamp] Basic JavaScript - Queues, Boolean, Comparison Operators</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Wed, 24 Jun 2020 18:41:46 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-basic-javascript-queues-boolean-comparison-operators-47ce</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-basic-javascript-queues-boolean-comparison-operators-47ce</guid>
      <description>&lt;p&gt;Howdy fellows! Here is yet another JavaScript post covering the fundamentals. This is in continuation of my JavaScript learning from &lt;a href="https://www.freecodecamp.org/" rel="noopener noreferrer"&gt;freeCodeCamp&lt;/a&gt;. The last post in the series can be found &lt;a href="https://dev.to/gutsytechster/freecodecamp-basic-javascript-functions-edj"&gt;here&lt;/a&gt;, where we have learned about functions in JavaScript.&lt;/p&gt;

&lt;p&gt;Let's get started then.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Queues&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;Queues are the abstract data structure, in which items are kept in a specific order. The items are added from the back to the queue and taken out from the front of the queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn09spl1gv2y6xqqafb36.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn09spl1gv2y6xqqafb36.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;center&gt;Queue data structure&lt;/center&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The above image describes the queue very well. You can see two terms in it - &lt;code&gt;enqueue&lt;/code&gt; and &lt;code&gt;dequeue&lt;/code&gt;. Let's know what these are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When an element is pushed to the queue, the operation is known as &lt;code&gt;enqueue&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When an element is popped out of the queue, the operation is known as &lt;code&gt;dequeue&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A queue can also be seen as an array, with some restrictions.&lt;/p&gt;

&lt;p&gt;The following example will help you understand it better&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var  arr = [1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding a number to the end of an array can be done with &lt;code&gt;push()&lt;/code&gt; method. Similarly popping out from the front of an array can be done using &lt;code&gt;shift()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.push(4);  // enqueue, now array [1, 2, 3, 4]
arr.shift();  // dequeue, now array [2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;u&gt;Boolean&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;Boolean&lt;/code&gt; is one of the basic data types in JavaScript. &lt;code&gt;Boolean&lt;/code&gt; can only have two values which are either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remember that &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; are keywords in JavaScript. If you'll put them into the quotes, they'll be treated as normal strings.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Comparison Operators&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;There are various comparison operators in JavaScript, let's have a look at them&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Equality Operator&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript has an equality operator &lt;code&gt;==&lt;/code&gt;. It compares two values and returns either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To compare two different data types(for e.g. &lt;code&gt;numbers&lt;/code&gt; and &lt;code&gt;string&lt;/code&gt;), it must convert one type to another. This is known as &lt;em&gt;Type Coercion&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some examples of equality operator are&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1   ==  1   // true
1   ==  2   // false
1   == '1'  // true
"3" ==  3   // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you may notice, the last two examples return &lt;code&gt;true&lt;/code&gt; irrespective of different data types i.e. this operator does not check for equality of data types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Strict Equality Operator&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Strict Equality operator is represented by &lt;code&gt;===&lt;/code&gt;. This not only checks for the value but also checks for the data type of its two operands. Let's see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 === 4;  // true
"4" === 4; // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second comparison returns &lt;code&gt;false&lt;/code&gt; because &lt;code&gt;"4"&lt;/code&gt; is a string while &lt;code&gt;4&lt;/code&gt; is a &lt;code&gt;number&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In JavaScript, we can get the type of a value using the &lt;code&gt;typeof&lt;/code&gt; operator as&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typeof "3"; // returns 'string'
typeof 3;  // returns 'number'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Inequality operator&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is the opposite of the equality operator. It returns &lt;code&gt;true&lt;/code&gt; when two values given to it are not equal, &lt;code&gt;false&lt;/code&gt; otherwise. Just like the equality operator, it doesn't check for the data type of its operands.&lt;/p&gt;

&lt;p&gt;Some examples of it can be seen as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 !=  2     // true
1 != "1"    // false 
1 != true   // false
0 != false  // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Strict Inequality Operator&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This would be clear by now. Strict inequality operator is represented by &lt;code&gt;!==&lt;/code&gt;. This not only checks for the value but also for the data type. It is just the opposite of the strict equality operator. Let's understand its usage with some examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 !==  3   // false
3 !== '3'  // true
4 !==  3   // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you may notice, that the second case returns &lt;code&gt;true&lt;/code&gt; because the data type for the values is different.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is highly advisable to use strict equality and strict inequality operator instead of their non-strict options available.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Greater than operator (&lt;code&gt;&amp;gt;&lt;/code&gt;)&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It compares two values and returns &lt;code&gt;true&lt;/code&gt; if the value to the left of it is greater than the value to the right. One thing to note down here is that it converts the data type of the values before comparing, just like the equality operator.&lt;/p&gt;

&lt;p&gt;A few examples can be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;7   &amp;gt; '3'  // true
2   &amp;gt;  3   // false
'1' &amp;gt;  9   // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Greater than or equals to operator (&lt;code&gt;&amp;gt;=&lt;/code&gt;)&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It checks if the value to the left of it is either greater or equal when compared to the value right of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;7   &amp;gt;= '3'  // true
2   &amp;gt;=  3   // false
'7' &amp;gt;=  9   // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Less than operator (&lt;code&gt;&amp;lt;&lt;/code&gt;)&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is quite obvious now. It checks if the value to the left of it is lesser than the value to the right of it. Returns &lt;code&gt;true&lt;/code&gt; if so, &lt;code&gt;false&lt;/code&gt; otherwise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'3' &amp;lt; 7  // true
3 &amp;lt; 2    // false 
'8' &amp;lt; 4  // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Less than or equals to operator (&lt;code&gt;&amp;lt;=&lt;/code&gt;)&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's directly go with an example for this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5   &amp;lt;= 5  // true
3   &amp;lt;= 2  // false
'8' &amp;lt;= 4  // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;With the end of this short post, we have acquired some knowledge about the Boolean data type and Queue data structure. Apart from it, we found out about type coercion and how they work when used with comparison operators. &lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/" rel="noopener noreferrer"&gt;Introduction to JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=lkOXlVgKHYM&amp;amp;list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&amp;amp;index=13&amp;amp;t=0s" rel="noopener noreferrer"&gt;Conquering freeCodeCamp - Basic JavaScript (Part 2) - Live Stream #12&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's meet next time with another JavaScript post covering other fundamentals of JavaScript. Till then be curious and keep learning.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>angular</category>
    </item>
    <item>
      <title>[freeCodeCamp] Basic JavaScript - Functions</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Sun, 14 Jun 2020 09:19:31 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-basic-javascript-functions-edj</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-basic-javascript-functions-edj</guid>
      <description>&lt;p&gt;Hello folks! I am here with another JavaScript post covering my learning and javascript fundamentals. Continuing my journey with &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;, this time I have learned about functions in JavaScript. The previous post in the series can be found &lt;a href="https://dev.to/gutsytechster/freecodecamp-basic-javascript-arrays-4bde"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's start with functions.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Functions&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;We can define functions in JavaScript using the keyword &lt;code&gt;function&lt;/code&gt;, followed by the function name.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Functions are used to divide the code into reusable parts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's see an example now&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction() {
  console.log("This is inside a function!");
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have named our function as &lt;code&gt;myFunction&lt;/code&gt; . The body of function starts with the opening curly bracket and ends with the closing curly bracket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: There is no semicolon after the closing curly bracket.&lt;/p&gt;

&lt;p&gt;We have also used the &lt;code&gt;console.log()&lt;/code&gt; statement within the function's body. We have not encountered this method yet. We'll be knowing about it in future posts, but to give you an idea, this method logs out whatever we pass to it within parentheses.&lt;/p&gt;

&lt;p&gt;There is a console in all the browsers which can be accessed using the key combination of &lt;code&gt;Ctrl + Shift + I&lt;/code&gt;. This will open a console session, where you could write JavaScript code and the output of it would be provided to you immediately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:- The key combination may differ in other browsers. It does work in Google Chrome and Mozilla Firefox.&lt;/p&gt;

&lt;p&gt;Calling a function is as simple as writing the following statement&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myFunction();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This would print the line &lt;code&gt;"This is inside a function!"&lt;/code&gt; onto the console.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Parameters and Arguments&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;A lot of times, a function takes a number of inputs to it which are known as &lt;strong&gt;&lt;em&gt;parameters&lt;/em&gt;&lt;/strong&gt;, and values for those inputs can be passed at the time of function calling which are known as &lt;strong&gt;&lt;em&gt;arguments&lt;/em&gt;&lt;/strong&gt;. Let's say we have a function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function functionWithArgs(a, b) {
  console.log(a - b);
}

functionWithArgs(10, 5);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are the parameters which act as placeholders for the values to be passed as arguments. These can be used within the function. The values &lt;code&gt;10&lt;/code&gt; and &lt;code&gt;5&lt;/code&gt;, which we have passed at the time of function calling are known as arguments.&lt;/p&gt;

&lt;p&gt;In the above function, the value will be passed as defined i.e. sequentially. The value of &lt;code&gt;a&lt;/code&gt; would be &lt;code&gt;10&lt;/code&gt; and the value of &lt;code&gt;b&lt;/code&gt; would be &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Scopes in function&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Scope in JavaScript refers to the visibility of a variable within a JavaScript code. There are two types of scopes in general&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Global Scope&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The variable defined outside of a function has a &lt;em&gt;global&lt;/em&gt; scope i.e. it can be used everywhere in the JavaScript code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any variable defined without the &lt;code&gt;var&lt;/code&gt; keyword, will be treated as global irrespective of where it is defined. This may lead to unwanted results. Therefore we should always use the keyword to define any variable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;&lt;u&gt;Local Scope&lt;/u&gt;&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A variable defined within the function body and the parameters defined in the function definition both have a &lt;em&gt;local&lt;/em&gt; scope which means they are only visible within its body. Trying to access them in the global scope would generate a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myTest() {
  var loc = "foo";
  console.log(loc);
}
myTest(); // logs "foo"
console.log(loc); // loc is not defined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you may notice, using &lt;code&gt;console.log(loc)&lt;/code&gt; would raise the error as it is not visible(accessible) outside of the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReferenceError: loc is not defined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Global Scope vs Local Scope&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;There can be cases, where a variable is declared in the global scope i.e. outside of any function and another variable with the same name is defined within the function i.e. inside a local scope.&lt;/p&gt;

&lt;p&gt;In such cases, the local scope takes precedence over the global scope. Let see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var someVar = "Hat";
function myFun() {
  var someVar = "Head";
  console.log(someVar);
} 
myFun(); // prints "Head"
console.log(someVar); // prints "Hat
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can notice, that calling the function prints the variable defined within it i.e. the one in the local scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Returning from a function&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;At times, a function does some processing and returns a value. Returning a value means that a function can send a value whenever called. Later, that value can be stored or used directly.&lt;/p&gt;

&lt;p&gt;A function returns the value using the &lt;code&gt;return&lt;/code&gt; keyword.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When a function reaches the &lt;code&gt;return&lt;/code&gt; statement, the execution of the current function stops and control goes to the calling location.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(num1, num2){
   return num1 + num2;
   console.log("It will never be executed!")
}
var result = add(2, 3);
console.log(result); // prints 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The variable &lt;code&gt;result&lt;/code&gt; stores the value returned by the function &lt;code&gt;add()&lt;/code&gt; and later can be used to print. Also, the &lt;code&gt;console.log&lt;/code&gt; statement within the function body won't be executed because it comes after the return statement.&lt;/p&gt;

&lt;p&gt;It can also be used without explicitly using a variable as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(add(2, 3));  // prints 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;console.log&lt;/code&gt; uses the value returned by &lt;code&gt;add(2, 3)&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;When a function doesn't return anything using the &lt;code&gt;return&lt;/code&gt; keyword, the default value returned by it is &lt;code&gt;undefined&lt;/code&gt;. For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function mul(num1, num2) {
  sum = num1 + num2;
}
console.log(mul(2, 3));  // prints undefined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Albeit the &lt;code&gt;sum&lt;/code&gt; has been modified but the function does not return anything. Therefore it prints out &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;With the end of this post, we have acquired some knowledge about functions and how to use them. Functions play an important role in any programming language as they help you to make reusable code and simplify the logic significantly.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/"&gt;Introduction to JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=ZCZ2u14xsS4&amp;amp;list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&amp;amp;index=12&amp;amp;t=0s"&gt;Conquering freeCodeCamp – Basic JavaScript (Part 1) – Live Stream #11&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's meet next time with some other JavaScript basics. Till then be curious and keep learning. :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>angular</category>
    </item>
    <item>
      <title>[freeCodeCamp] Basic JavaScript - Arrays</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Sat, 06 Jun 2020 10:19:38 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-basic-javascript-arrays-4bde</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-basic-javascript-arrays-4bde</guid>
      <description>&lt;p&gt;Hey, y'all! I believe you all are doing excellent. This is in continuation of my previous post on &lt;a href="https://dev.to/gutsytechster/freecodecamp-basic-javascript-strings-3i9k"&gt;strings&lt;/a&gt; and my JavaScript learning from &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;. This time, I acquired some knowledge about arrays in JavaScript.&lt;/p&gt;

&lt;p&gt;Let's begin without any delay!&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Defining JavaScript Arrays&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;When we want to store multiple related values sequentially, we make use of arrays. Instead of creating multiple variables for each value, we can assign all those values to an array.&lt;/p&gt;

&lt;p&gt;An array in JavaScript in enclosed in &lt;code&gt;[]&lt;/code&gt; i.e. square brackets with multiple values separated by comma(&lt;code&gt;,&lt;/code&gt;). Let's see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myArray = ["butter", "cheese", "milk"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;myArray&lt;/code&gt; is now an array.  It can contain values of different data types as well.&lt;/p&gt;

&lt;p&gt;One can also nest array within other arrays. These are called multi-dimensional arrays. For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myArray = [["John", 12], ["Maxwell", 18]]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here you can see, we have defined multiple arrays within an array. This is what we refer to as nested arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Accessing array elements&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We can access array elements in the same way we deal with string i.e. using indexes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Indexes in JavaScript starts with 0&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myArray[0]; // returns the first element of an array
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Modifying Array elements&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Unlike strings, arrays are mutable i.e. you can change the values that exist within an array. For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myArray = [5, 4. 7, 2]; 
myArray[2] = 3; // now the array looks like [5, 4, 3, 2]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Accessing Nested Array elements&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Accessing nested array elements can be achieved by using multiple brackets where first pair of brackets refers to the outermost array and subsequent pairs would access the subsequent nested arrays. Let's clear it with an example from freeCodeCamp itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [
  [1,2,3],
  [4,5,6],
  [7,8,9],
  [[10,11,12], 13, 14]
];
arr[3]; // equals [[10,11,12], 13, 14]
arr[3][0]; // equals [10,11,12]
arr[3][0][1]; // equals 11
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Manipulating Arrays&lt;/u&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;
&lt;code&gt;push()&lt;/code&gt; method&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript provides &lt;code&gt;push()&lt;/code&gt; method to append a value to the end of an array. Let see its usage with a short example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [1, 2];
arr.push(3); // arr is now [1, 2, 3]
arr.push([4, 5]); // arr is now [1, 2, 3, [4, 5]]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;
&lt;code&gt;pop()&lt;/code&gt; method&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;pop()&lt;/code&gt; method works, just as opposed to &lt;code&gt;push()&lt;/code&gt; i.e. it pops out or removes the last element of an array and returns that element. When I say, return an element, it means we can store the value returned by &lt;code&gt;pop()&lt;/code&gt; in case we want.&lt;/p&gt;

&lt;p&gt;Considering the above updated array as an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.pop(); // Returns [4, 5], now array will be [1, 2, 3]
arr.pop(); // Returns 3 as it is the last element of updated array
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;
&lt;code&gt;shift()&lt;/code&gt; method&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We know that &lt;code&gt;pop()&lt;/code&gt; removes the last element of an array. But what if we want the first element to be removed? Well, we can use &lt;code&gt;shift()&lt;/code&gt; for that. It removes the first element and returns that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [1, 2, 3];
arr.shift(); // Returns 1, now array will be [2, 3]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;
&lt;code&gt;unshift()&lt;/code&gt; method&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;unshift()&lt;/code&gt; works just like &lt;code&gt;push()&lt;/code&gt; but instead of adding an element to the end, it adds an element to the starting of array.  Let's see an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [2, 3];
arr.unshift(1); // arr is now [1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;In this post, we got familiar with the fundamentals of arrays in JavaScript and how to manipulate and access them.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/"&gt;Introduction to JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=ZCZ2u14xsS4&amp;amp;list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&amp;amp;index=12&amp;amp;t=0s"&gt;Conquering freeCodeCamp – Basic JavaScript (Part 1) – Live Stream #11&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am keeping this post short for easy grasping of concepts. I will be talking about other JavaScript fundamentals in the next post. Till then, be curious and keep learning! :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>freecodecamp</category>
      <category>webdev</category>
      <category>dailylearning</category>
    </item>
    <item>
      <title>[freeCodeCamp] Basic JavaScript - Strings</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Tue, 02 Jun 2020 09:58:13 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-basic-javascript-strings-3i9k</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-basic-javascript-strings-3i9k</guid>
      <description>&lt;p&gt;Howdy fellows!&lt;/p&gt;

&lt;p&gt;I hope you all are fine at this time of the pandemic. I am trying to utilize as much of my time as I can. So, I have started learning JavaScript from &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;'s JavaScript curriculum. This is the second post in the series, which I am writing to keep notes of my learning, which might help someone else. You can find my previous post &lt;a href="https://dev.to/gutsytechster/freecodecamp-basic-javascript-comments-variables-and-mathematical-operations-69o"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's get started&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Strings&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;In JavaScript, a string can be written using any of the single or double quotes, as long as we start and end the string with the same quotation. For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myString = "My name is Prashant";
var anotherString = 'I study in college';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above example, both &lt;code&gt;myString&lt;/code&gt; and &lt;code&gt;anotherString&lt;/code&gt; are valid strings in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Escaping literal quotes in JavaScript&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;When we need to insert a single or double quote within a string, we escape that character by pre-pending it by &lt;code&gt;\&lt;/code&gt; i.e. a backslash.  For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myString = "It is a \"double quoted\" string";
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;would result in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;It is a "double quoted" string;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, you may not need to escape the string if your surrounding quotes are not same as what you want in the string. Let me give you an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myString = 'I am a "double quoted" string';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;would give the same result as above. As you may notice, here we didn't need to escape the quote as the surrounding quote is a single quote (&lt;code&gt;''&lt;/code&gt;), but what we used inside is a double quote.&lt;br&gt;
Vice -versa would be true as well i.e. you may keep the double quotes for surrounding your string and use single quote inside of your string without escaping.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;Other escape sequences in String&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Apart from using escape sequence for multiple quotes within the same string, escape sequences are also used to type out characters which we may not be able to do otherwise. For e.g. a tab.&lt;/p&gt;

&lt;p&gt;Some of the escape sequences present in JavaScript can be listed as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\'&lt;/code&gt; for single quotes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\"&lt;/code&gt; for double quotes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\\&lt;/code&gt; for backslash, when you want to use backslash as a character in a string&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\n&lt;/code&gt; for newline&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\r&lt;/code&gt; for carriage return&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\t&lt;/code&gt; for tab&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\b&lt;/code&gt; for word boundary&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\f&lt;/code&gt; for a form feed&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;String Concatenation&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We can concatenate two strings using the &lt;code&gt;+&lt;/code&gt; operator. For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myString = "My name is Prashant" + " and I love programming.";
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;would give result as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"My name is Prashant and I love programming".
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Make sure you give spaces where you want. Concatenation doesn't add spaces by itself. You may notice, I've provided a space in the second part of string concatenation.&lt;/p&gt;

&lt;p&gt;You can use the shorthand &lt;code&gt;+=&lt;/code&gt; for concatenation as well. For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myString = "My name is Prashant";
myString += " and I love programming";
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This would give the same result as above.&lt;/p&gt;

&lt;p&gt;We can use variables to store part of strings and then use them for concatenation. For e.g. the above example can also be written as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myName = "Prashant";
myHobby = "programming";

myString = "My name is " + myName + " and I love " + myHobby;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Finding the length of a string&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;To find the length of a string, we can make use of &lt;code&gt;length&lt;/code&gt; property available to &lt;code&gt;String&lt;/code&gt; data type as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myString = "Prashant";
myString.length; // This would give us 8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Look carefully, how I have used the property using the &lt;code&gt;.&lt;/code&gt;(dot)  with the variable.&lt;/p&gt;

&lt;p&gt;You may directly use the string to access its length property instead of storing it to a variable like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Prashant".length; // This would also give us 8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Accessing individual characters of a string&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We can access each character of a string using indexes. In JavaScript, we have indexes start from 0. Indexes are used along with the bracket notation to access the characters as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myStr = "Example";
myStr[0]; // This would give us the 1st character of myStr i.e. "E"
myStr[1]; // This would give use the 2nd character i.e. "x".
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This was easy, wasn't it?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;u&gt;Accessing the last character of a string&lt;/u&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you want to get the last character of the string, you may not know the last index of the string. In such cases, we can make use of the &lt;code&gt;length&lt;/code&gt; property, we just discussed above.&lt;/p&gt;

&lt;p&gt;We know that &lt;code&gt;length&lt;/code&gt; property gives us the length of a string. So can you now think of at what index would the last character of a string be? Yes, it would be the &lt;code&gt;length - 1&lt;/code&gt; as index starts with 0 in JavaScript.&lt;/p&gt;

&lt;p&gt;For e.g. in the above example, &lt;code&gt;Example&lt;/code&gt; has length &lt;code&gt;7&lt;/code&gt; but the last index of this string is &lt;code&gt;6&lt;/code&gt;. I hope, now you get it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myStr[myStr.length - 1];  // This would give you the last character of myStr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;u&gt;Accessing last to Nth character in a string&lt;/u&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a similar fashion as above, if you want to get the nth character from the last, you can access it using &lt;code&gt;myStr.length - n&lt;/code&gt;, when &lt;code&gt;n&lt;/code&gt; is the nth character from the last.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;String Immutability in JavaScript&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;In JavaScript, strings are immutable i.e. once created you cannot alter the contents of a string. For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myStr = "Pan";
myStr[0] = "C";
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;would result in the following error as we are trying to alter the contents of &lt;code&gt;myStr&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: Cannot assign to read only property '0' of string 'Pan'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But this doesn't mean that we can't change the value of &lt;code&gt;myStr&lt;/code&gt;. You can always reassign it to any other value. It's just that individual characters of a string cannot be changed.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;In this post, we got familiar with the fundamentals of a string in JavaScript and how to manipulate and access them.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;u&gt;References&lt;/u&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/"&gt;Introduction to JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=ZCZ2u14xsS4&amp;amp;list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&amp;amp;index=12&amp;amp;t=0s"&gt;Conquering freeCodeCamp – Basic JavaScript (Part 1) – Live Stream #11&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will be talking about other JavaScript fundamentals in the next post. Till then, be curious and keep learning! :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>freecodecamp</category>
      <category>strings</category>
      <category>webdev</category>
    </item>
    <item>
      <title>[freeCodeCamp] Basic JavaScript – Comments, Variables and Mathematical Operations</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Sat, 30 May 2020 09:35:19 +0000</pubDate>
      <link>https://dev.to/gutsytechster/freecodecamp-basic-javascript-comments-variables-and-mathematical-operations-69o</link>
      <guid>https://dev.to/gutsytechster/freecodecamp-basic-javascript-comments-variables-and-mathematical-operations-69o</guid>
      <description>&lt;p&gt;Hello folks!&lt;/p&gt;

&lt;p&gt;I have planned to complete the JavaScript curriculum from the &lt;a href="https://www.freecodecamp.org/"&gt;freeCodeCamp&lt;/a&gt;. I have always wanted to complete a course on JavaScript but couldn't do so due to procrastination, laziness and a lot of other reasons.&lt;/p&gt;

&lt;p&gt;But this time, I feel that not only learning but keeping a record of each concept and topic, will keep me motivated to continue the course and complete it until it ends. Another reason to choose the freeCodeCamp was to come across this &lt;a href="https://www.youtube.com/playlist?list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH"&gt;playlist&lt;/a&gt; by &lt;a href="https://twitter.com/florinpop1705"&gt;Florin pop&lt;/a&gt;. He has done an amazing job there.&lt;/p&gt;

&lt;p&gt;This will be a series of posts, which would include different topics, involved in the freeCodeCamp's JavaScript curriculum. Now, without any more delay, let's start with topics.&lt;/p&gt;

&lt;h1&gt;
  
  
  Comments
&lt;/h1&gt;

&lt;p&gt;We can comment JavaScript code using &lt;code&gt;//&lt;/code&gt; or &lt;code&gt;/* */&lt;/code&gt;. Although both of them are valid comment specifiers. We use &lt;code&gt;//&lt;/code&gt; for single-line comments and &lt;code&gt;/* */&lt;/code&gt;(start with &lt;code&gt;/*&lt;/code&gt; and end with &lt;code&gt;*/&lt;/code&gt;) for multi-line comments. For eg.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Single Line or inline comment
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* 
   Multiple
   Line of 
   Comments 
*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Declaring and Initializing Variables
&lt;/h1&gt;

&lt;p&gt;We know, that variables are used to store data. In JavaScript, we mainly have seven kinds of data types which are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;null&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;string&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;symbol&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;number&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;object&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript is also a dynamically typed language i.e. a variable in JavaScript can hold any type of value at any point of time within a program. We use the &lt;code&gt;var&lt;/code&gt; keyword to declare variables in JavaScript as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myName;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;myName&lt;/code&gt; is a variable, which can store any data type from the above list. Also, &lt;em&gt;don't forget to end a JavaScript statement with a semicolon(&lt;code&gt;;&lt;/code&gt;).&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Variables in JavaScript can contain numbers, letters, &lt;code&gt;$&lt;/code&gt;, &lt;code&gt;_&lt;/code&gt; but can't contain spaces and start with numbers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can  as well assign a value to the variable using assignment &lt;code&gt;=&lt;/code&gt; operator as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myName = "Prashant";
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also initialize a variable at the time of its declaration as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myNum = 7;
var anotherNum = myNum;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Quirks with uninitialized variables
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When you don't initialize a variable, rather just only declare it, they hold the value &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Performing any mathematical operation with &lt;code&gt;undefined&lt;/code&gt; will result in &lt;code&gt;NaN&lt;/code&gt; which means &lt;em&gt;Not a Number&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Performing concatenation with &lt;code&gt;undefined&lt;/code&gt; by adding a string literal to it will result in a string &lt;code&gt;"undefined"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript is a case-sensitive language. It means variable &lt;code&gt;myvar&lt;/code&gt; and &lt;code&gt;myVar&lt;/code&gt; are different due to case insensitivity of letter &lt;code&gt;v&lt;/code&gt;.  In JavaScript, the best practice to define variables is to define them in &lt;em&gt;camelCase&lt;/em&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Mathematical Operations in JavaScript
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;Number&lt;/code&gt; data type is used to represent numeric data. There are various operators in JavaScript.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can add two numbers in JavaScript using &lt;code&gt;+&lt;/code&gt; operator as
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 10 + 10; // assigned 20
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We can subtract two numbers in JavaScript using &lt;code&gt;-&lt;/code&gt; operator as
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var b = 15 - 5; // assigned 10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We can multiply two numbers using &lt;code&gt;*&lt;/code&gt; operator as
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var c = 5 * 10; // assigned 50
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We can divide two numbers using &lt;code&gt;/&lt;/code&gt; operator as
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var d = 80 / 10; // assigned 8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Until now, we have just used some basic mathematical operation which we've seen already in mathematics. However, there are some other helpful mathematical operators like&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increment a number by 1 using &lt;code&gt;++&lt;/code&gt; operator. Yes, I hear, you say this can easily be done as
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var = var + 1;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, JavaScript provides a shorthand to achieve it without the need of an assignment operator. The above statement is equivalent to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var++;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Decrement a number by 1 using &lt;code&gt;--&lt;/code&gt; operator. Similarly, as the increment operator, we can decrement a numerical value stored in a variable in a concise way as
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var--;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Some other operations are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can create Decimal numbers by just assigning the variable to the floating value. For e.g.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var pi = 3.14;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;is a valid decimal number. All other arithmetic operations described above can also be performed with decimal numbers. :)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have remainder operator i.e. &lt;code&gt;%&lt;/code&gt; in JavaScript to find the remainder of a division. For e.g.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var rem = 7 % 3;  // assigned 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This operation is widely used for finding out even and odd numbers as a number properly divisible by 2 would be called as even, and odd otherwise.&lt;/p&gt;

&lt;p&gt;JavaScript provides some shorthand for compound assignments for various mathematical operations like, &lt;code&gt;+=&lt;/code&gt;, &lt;code&gt;-=&lt;/code&gt;, &lt;code&gt;*=&lt;/code&gt;, &lt;code&gt;/=&lt;/code&gt; etc.  You can use them as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 1;
a += 5;  // a = a + 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Other shorthand operators work in the same fashion.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In this post, we've got familiar with concepts like comments, variables and various mathematical operations in JavaScript.&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/"&gt;Introduction to JavaScript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=ZCZ2u14xsS4&amp;amp;list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&amp;amp;index=12&amp;amp;t=0s"&gt;Conquering freeCodeCamp - Basic JavaScript (Part 1) - Live Stream #11&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's meet in the next post, covering some other JavaScript fundamentals. Till then, be curious and keep learning! :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>freecodecamp</category>
      <category>webdev</category>
      <category>dailylearning</category>
    </item>
  </channel>
</rss>
