<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Within Bracket - Level Up Your Programming]]></title><description><![CDATA[Within Bracket is a programming blog which cover the topics regarding programming and Web technologies.]]></description><link>https://sarveshprajapati.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 18 Nov 2026 13:19:11 GMT</lastBuildDate><atom:link href="https://sarveshprajapati.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Function in JavaScript - The Basic]]></title><description><![CDATA[Bread and Butter of JavaScript Programming.]]></description><link>https://sarveshprajapati.hashnode.dev/function-in-javascript-the-basic</link><guid isPermaLink="true">https://sarveshprajapati.hashnode.dev/function-in-javascript-the-basic</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Sarvesh Prajapati]]></dc:creator><pubDate>Tue, 01 Mar 2022 16:20:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1646150479814/kRT2arpfL.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>If you been in programming for a while, you probably would know that functions are the important concept of any typical programming language.</p>
<p>And if are familiar with functional programming languages, like JavaScript, 
then it is hard to argue with that statement that <em>functions are the bread and butter</em> of 
functional programming languages.</p>
<p>In case if you are new to this programming worlds, then bare with throughout this article and in the end you might would get to know something interesting about functions and why it is important.</p>
<p>The concept of functions is fairly simple:</p>
<p>Breaking down large problems in pieces and wrapping them in a value to reduce repetition.</p>
<p>Sounds Bookish ?</p>
<p>Let me simplify it.</p>
<p>Say, you have a problem to find square of some numbers.</p>
<p>The typical approach would be:</p>
<pre><code class="lang-js">    <span class="hljs-keyword">let</span> num;
    num = <span class="hljs-number">10</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
    num = <span class="hljs-number">11</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
    num = <span class="hljs-number">12</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
    num = <span class="hljs-number">13</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
    num = <span class="hljs-number">14</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
    num = <span class="hljs-number">15</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
    num = <span class="hljs-number">16</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
    num = <span class="hljs-number">17</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
</code></pre>
<p>Doesn't it seems weird ? Repetitive.</p>
<p>Let's see how functions would reduce this repetition.</p>
<pre><code class="lang-js">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findSquare</span>(<span class="hljs-params">num</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
    }
    findSquare(<span class="hljs-number">10</span>); <span class="hljs-comment">//Square of 10 is 100</span>
    findSquare(<span class="hljs-number">11</span>); <span class="hljs-comment">//Square of 11 is 121</span>
    findSquare(<span class="hljs-number">12</span>); <span class="hljs-comment">//Square of 12 is 144</span>
    findSquare(<span class="hljs-number">13</span>); <span class="hljs-comment">//Square of 13 is 169</span>
    findSquare(<span class="hljs-number">14</span>); <span class="hljs-comment">//Square of 14 is 196</span>
    findSquare(<span class="hljs-number">15</span>); <span class="hljs-comment">//Square of 15 is 225</span>
    findSquare(<span class="hljs-number">16</span>); <span class="hljs-comment">//Square of 16 is 256</span>
    findSquare(<span class="hljs-number">17</span>); <span class="hljs-comment">//Square of 17 is 286</span>
    findSquare(<span class="hljs-number">18</span>); <span class="hljs-comment">//Square of 18 is 324</span>
</code></pre>
<p>How about that !</p>
<p>This is just a simple example to show you the power of functions, but this not enough. 
In fact these days we have <strong>functional programming approach</strong> to solve a problem. 
We'll discuss more about functional programming in future articles.</p>
<p>For now let's play with some basics, to help you to learn fundamental concepts.</p>
<h2 id="heading-structure-of-a-function">Structure of a function:</h2>
<p>To use a function:</p>
<ul>
<li>first you need to define a function</li>
<li>then, call the function</li>
</ul>
<p><strong>In the above example:</strong></p>
<p>Define a function</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findSquare</span>(<span class="hljs-params">num</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Square of "</span>+num+<span class="hljs-string">" is "</span>+num*num);
}
</code></pre>
<p>Call the function</p>
<pre><code class="lang-js">findSquare(<span class="hljs-number">10</span>);
</code></pre>
<h2 id="heading-components-of-a-function">Components of a function:</h2>
<p>Typically a function has three components:</p>
<ol>
<li>function name</li>
<li>parameters</li>
<li>return values</li>
</ol>
<p>function name is mandatory, whereas some function do have parameters and some functions don't. Same goes for return values, some function return values some functions don't. </p>
<pre><code class="lang-js">    <span class="hljs-comment">// Without parameter, Without return value</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params"></span>)</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"I said hello"</span>);
    }
    sayHello(); <span class="hljs-comment">// I said hello</span>

    <span class="hljs-comment">//Without parameter, With return value</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greeting</span>(<span class="hljs-params"></span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello"</span>;
    }
    <span class="hljs-built_in">console</span>.log(greeting()+ <span class="hljs-string">" World!"</span>); <span class="hljs-comment">// Hello world</span>

    <span class="hljs-comment">//With parameter, Without return</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">printDouble</span>(<span class="hljs-params">num</span>)</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Double of "</span>+num+<span class="hljs-string">" is "</span>+num*<span class="hljs-number">2</span>);
    }
    printDouble(<span class="hljs-number">2</span>); <span class="hljs-comment">// Double of 2 is 4</span>

    <span class="hljs-comment">//with parameter, with return</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">giveSquare</span>(<span class="hljs-params">num</span>) </span>{
        <span class="hljs-keyword">return</span> num*num;
    }
    <span class="hljs-built_in">console</span>.log(giveSquare(<span class="hljs-number">5</span>)); <span class="hljs-comment">// 25</span>
</code></pre>
<p>Each variable created inside a function is only valid only inside that function. 
It is called <strong>local scope</strong>. </p>
<p>Let me show you:</p>
<pre><code class="lang-js">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">printFive</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">const</span> num = <span class="hljs-number">5</span>;
        <span class="hljs-built_in">console</span>.log(num); <span class="hljs-comment">//5</span>
    }
    <span class="hljs-built_in">console</span>.log(num); <span class="hljs-comment">//ReferenceError: num is not defined</span>
</code></pre>
<p>I have written a detailed article on Scope in JavaScript. Check out here 👇</p>
<blockquote>
<p><a target="_blank" href="https://withinbracket.com/posts/variables-in-javascript-scope-and-hoisting/">Variables in JavaScript, Scope and Hoisting</a></p>
</blockquote>
<p>Before I go any further let me draw your attention to <strong>parameter</strong> and <strong>argument</strong>.</p>
<p>Sometimes people being confused between parameter and argument, 
and treat them as same. But there is a thin line between them which you need to know, 
to be a better developer.</p>
<p>Check out below image:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646151176802/O0SuIOQZP.png" alt="brunch.png" /></p>
<p>When we pass the value while calling the function, that value is refer as <strong>argument</strong>, 
whereas when we accept the value in the function definition, that value is refer as <strong>parameter</strong>.</p>
<p>This is all the basics you need to know about function. </p>
<p>But when is comes to JavaScript, you can manipulate function in a different way than any other 
functional programming language.</p>
<p>Let me show you:</p>
<p>All functional programming language can do this:</p>
<pre><code class="lang-js">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">makeTwice</span>(<span class="hljs-params">n</span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>*n;
    }
    <span class="hljs-built_in">console</span>.log(makeTwice(<span class="hljs-number">5</span>));
</code></pre>
<p>But not all functional programming language can do this:</p>
<pre><code class="lang-js">    <span class="hljs-keyword">const</span> makeTwice = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">n</span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>*n;
    }
    <span class="hljs-keyword">let</span> giveMeTwice = makeTwice; <span class="hljs-comment">//passing makeTwice to giveMeTwice</span>
    <span class="hljs-built_in">console</span>.log(giveMeTwice(<span class="hljs-number">5</span>));
</code></pre>
<p>In JavaScript we can even pass the function to variables like a typical value. 
This feature has a several benefits in functional programming with JavaScript 
which we'll discuss in future article.</p>
<p>Before I wrap things up, there are some topics which need to discuss.</p>
<h2 id="heading-optional-argument">Optional Argument:</h2>
<p>Let me first show you an example:</p>
<pre><code class="lang-js">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">makeSquare</span>(<span class="hljs-params">n</span>) </span>{
        <span class="hljs-keyword">return</span> n* n; 
    }
    <span class="hljs-built_in">console</span>.log(makeSquare(<span class="hljs-number">5</span>, <span class="hljs-string">"hello"</span>, <span class="hljs-literal">false</span>)); <span class="hljs-comment">//25</span>
</code></pre>
<p>We might pass 3 arguments <em>(5, "hello", false)</em>, but out function <strong>makeSquare</strong> only able to accept 1 parameter.</p>
<p>So you guessed it right, both <em>"hello"</em> and <em>false</em> are optional arguments.</p>
<p>The optional arguments looks like useless, but they are not at all.</p>
<p>Let me show you:</p>
<p><em>Say you have a problem where you pass 2 arguments. If function accept two arguments them it give the result of their difference, but if the function accepts only 1 parameter then it result the -ve of that single number.
</em></p>
<pre><code class="lang-js">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">minus</span>(<span class="hljs-params">x, y</span>) </span>{
      <span class="hljs-keyword">if</span> (y === <span class="hljs-literal">undefined</span>)
        <span class="hljs-keyword">return</span> -x;
      <span class="hljs-keyword">else</span> 
        <span class="hljs-keyword">return</span> x - y;
    }
    <span class="hljs-built_in">console</span>.log(minus(<span class="hljs-number">10</span>)); <span class="hljs-comment">// → -10</span>
    <span class="hljs-built_in">console</span>.log(minus(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>)); <span class="hljs-comment">// → 5</span>
</code></pre>
<p>You can even set the default value of optional parameter.</p>
<pre><code class="lang-js">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiply</span>(<span class="hljs-params">x, y=<span class="hljs-number">1</span></span>) </span>{
        <span class="hljs-keyword">return</span> x*y; <span class="hljs-comment">//here y is 1 by default</span>
    }
    <span class="hljs-built_in">console</span>.log(multiply(<span class="hljs-number">10</span>)); <span class="hljs-comment">// → 10</span>
    <span class="hljs-built_in">console</span>.log(multiply(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>)); <span class="hljs-comment">// 50</span>
</code></pre>
<h2 id="heading-the-arguments-array">The Arguments Array</h2>
<p>Like I told you earlier, functions in JavaScript are way advanced than any other. 
One of it's cool feature is <strong><em>arguments</em></strong> array.</p>
<p>The arguments array is the default property of a function in JavaScript which 
contains all the arguments passed to that function in the form of an <strong>array</strong>.</p>
<p>Let me show you:</p>
<pre><code class="lang-js">    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">demo</span>(<span class="hljs-params">a,b,c</span>) </span>{
        <span class="hljs-built_in">console</span>.log(a); <span class="hljs-comment">// 10</span>
        <span class="hljs-built_in">console</span>.log(b); <span class="hljs-comment">// 20</span>
        <span class="hljs-built_in">console</span>.log(c); <span class="hljs-comment">// 30</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>.length); <span class="hljs-comment">// 3 </span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>]); <span class="hljs-comment">// 10</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>]); <span class="hljs-comment">// 20</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>[<span class="hljs-number">2</span>]); <span class="hljs-comment">// 30</span>
    }
    demo(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>,<span class="hljs-number">30</span>);
</code></pre>
<p>The arguments array still works, even if you don't pass anything in the function parameter.</p>
<p>Sounds weird ?</p>
<p>Check this out:</p>
<pre><code class="lang-js">    <span class="hljs-comment">// removing a,b,c from parameter</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">demo</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>.length); <span class="hljs-comment">// 3 </span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>]); <span class="hljs-comment">// 10</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>]); <span class="hljs-comment">// 20</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>[<span class="hljs-number">2</span>]); <span class="hljs-comment">// 30</span>
    }
    demo(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>,<span class="hljs-number">30</span>);
</code></pre>
<h2 id="heading-conclusion">Conclusion:</h2>
<p>With that being said, up till here we have covered all the basics you need to know about 
functions in JavaScript. Now you are good to go and play with them on your own.</p>
<p>Cause this the only way you can learn even better.</p>
<p>But this is not at all. We still have lot to discuss about functions and some even cool features of them.</p>
<p>More on later articles. Till then keep learning.</p>
]]></content:encoded></item><item><title><![CDATA[Type Conversion in JavaScript - The Magic]]></title><description><![CDATA[More than just data types.]]></description><link>https://sarveshprajapati.hashnode.dev/type-conversion-in-javascript-the-magic</link><guid isPermaLink="true">https://sarveshprajapati.hashnode.dev/type-conversion-in-javascript-the-magic</guid><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Sarvesh Prajapati]]></dc:creator><pubDate>Sat, 26 Feb 2022 10:57:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1645872948109/ZL4MkObg-.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you are a JavaScript developer then you must know JavaScript has three primitive types: <em>Numbers, String and Boolean</em></p>
<p>But do you know, these types are being converted with each other to perform a certain program. This phenomenon is known as <strong>Type Conversion</strong>.</p>
<h2 id="heading-types-of-conversion">Types of Conversion</h2>
<ol>
<li>Implicitly</li>
<li>Explicitly</li>
</ol>
<p>Let’s explore each of this;</p>
<h2 id="heading-1-implicit-type-conversion">1. Implicit Type Conversion</h2>
<p>JavaScript’s objects and function automatically convert the value to a right type for the particular operation. For example:</p>
<p>When you perform a <code>console.log()</code>, the expression automatically convert into String:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> num = <span class="hljs-number">1001</span>;  <span class="hljs-comment">// num is a number</span>
<span class="hljs-built_in">console</span>.log(num) <span class="hljs-comment">//&gt; 1001</span>
<span class="hljs-comment">//? This num has become a string to print.</span>

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"20"</span>+<span class="hljs-string">"10"</span>) <span class="hljs-comment">//&gt; 2010</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"20"</span>-<span class="hljs-string">"10"</span>) <span class="hljs-comment">//&gt; 10  (same for * and /)</span>
<span class="hljs-comment">//? Here + operator simple concatenate the "20" and "10" as string and give 2010,</span>
<span class="hljs-comment">//whereas - operator converts the values into number to give output 10.</span>
</code></pre>
<p>Same for <code>alert()</code>. This also convert the value in String.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> isAlive = <span class="hljs-literal">true</span>:
alert(isAlive)  <span class="hljs-comment">//&gt; true</span>
<span class="hljs-comment">//? This isAlive has become string</span>
</code></pre>
<p>This <strong>Implicit</strong> type conversion also happen when you perform a condition operation using <code>if..else</code> or <code>switch</code>.</p>
<p>The expression within the <code>if</code> block is converted in <code>boolean</code> to perform the operation.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> point = <span class="hljs-number">100</span>;
<span class="hljs-keyword">if</span>(point) {  
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`You have <span class="hljs-subst">${point}</span> points`</span>)
} <span class="hljs-keyword">else</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Collect some point first`</span>)
}
<span class="hljs-comment">//? though point in number but it converts in boolean(true)</span>
<span class="hljs-comment">//! Since we know all the positive number is true except 0</span>
<span class="hljs-comment">// the output will be console.log(`You have ${point} points`)</span>
</code></pre>
<p>By the way if you don’t know about this <strong>``</strong> and <code>${}</code>, they are called <strong>template literals</strong>. This is the new feature in <strong>ES6</strong>. This is a very handy tool to logging a statement. Do check it out.</p>
<p>Some more function and operator also do this implicit type conversion, but this is not out concern. We don’t need to worry about all of the function but if you wanna know about them, you get them by doing some research.</p>
<p>Let’s explore the other one:</p>
<h2 id="heading-2-explicit-type-conversion">2. Explicit Type Conversion</h2>
<p>In this, we manually convert the value in different types by using some operator and constructor functions.</p>
<h3 id="heading-number-constructor">Number() constructor</h3>
<p>This convert the expression into <em>number</em>.</p>
<p>Conversion Rule</p>
<pre><code class="lang-js"><span class="hljs-literal">undefined</span> converts into <span class="hljs-literal">NaN</span>
<span class="hljs-literal">null</span> converts into <span class="hljs-number">0</span>
<span class="hljs-string">""</span> converts into <span class="hljs-number">0</span>
<span class="hljs-literal">true</span> converts into <span class="hljs-number">1</span>
<span class="hljs-literal">false</span> converts into <span class="hljs-number">0</span>
string converts into <span class="hljs-literal">NaN</span>
</code></pre>
<p>Example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> input = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">const</span> output = <span class="hljs-built_in">Number</span>(input);
<span class="hljs-built_in">console</span>.log(input);  <span class="hljs-comment">//&gt;true</span>
<span class="hljs-built_in">console</span>.log(output); <span class="hljs-comment">//&gt;1</span>
<span class="hljs-comment">//? The Number constructor converts the boolean value true of input into numeric value 1</span>
</code></pre>
<h3 id="heading-some-more-examples">Some more examples:</h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> value;
<span class="hljs-built_in">console</span>.log(value);  <span class="hljs-comment">//&gt;undefined</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Number</span>(value));   <span class="hljs-comment">//&gt;NaN (Not a Number)</span>

value = <span class="hljs-literal">null</span>;
<span class="hljs-built_in">console</span>.log(value);  <span class="hljs-comment">//&gt;null</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Number</span>(value));   <span class="hljs-comment">//&gt;0</span>

value = <span class="hljs-string">""</span>;
<span class="hljs-built_in">console</span>.log(value);  <span class="hljs-comment">//&gt;""</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Number</span>(value));   <span class="hljs-comment">//&gt;0</span>

value = <span class="hljs-string">"1to4"</span>;
<span class="hljs-built_in">console</span>.log(value);  <span class="hljs-comment">//&gt;"1to4"</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Number</span>(value));   <span class="hljs-comment">//&gt;NaN [ to is string ]</span>
</code></pre>
<h3 id="heading-string-constructor">String() Constructor</h3>
<p>This convert the expression into <em>string</em>.</p>
<p>Example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> isAlive = <span class="hljs-literal">true</span>;
<span class="hljs-built_in">console</span>.log(isAlive);  <span class="hljs-comment">//&gt;true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> isAlive);  <span class="hljs-comment">//&gt; boolean</span>

isAlive = <span class="hljs-built_in">String</span>(isAlive);
<span class="hljs-built_in">console</span>.log(isAlive);  <span class="hljs-comment">//&gt;true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> isAlive);  <span class="hljs-comment">//&gt; string</span>

<span class="hljs-comment">//&gt; Thought typeof(isAlive), second time, is true, but this true is not boolean, it's a string due to String()</span>
</code></pre>
<p>Any thing that pass into the <code>String()</code>, is become string <em>regardless</em> what it been before.</p>
<h3 id="heading-boolean-constructor">Boolean() Constructor</h3>
<p>This convert the expression into <em>boolean</em>.</p>
<p>Conversion Rule:</p>
<pre><code class="lang-js"><span class="hljs-number">0</span>, <span class="hljs-string">""</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">NaN</span> covert into <span class="hljs-number">0</span>
others than above convert into <span class="hljs-number">1</span>
</code></pre>
<p>Example:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>(<span class="hljs-number">0</span>)); <span class="hljs-comment">//&gt; false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>(<span class="hljs-number">123</span>)); <span class="hljs-comment">//&gt; true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>(<span class="hljs-string">'wooh'</span>)); <span class="hljs-comment">//&gt; true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>(<span class="hljs-string">""</span>)); <span class="hljs-comment">//&gt; false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>([])); <span class="hljs-comment">//&gt; true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>(<span class="hljs-string">"0"</span>)); <span class="hljs-comment">//&gt; true</span>
<span class="hljs-comment">//? Last one : Here the quote is not empty, it has a value "0".</span>
<span class="hljs-comment">// That's why it returns true rather than false</span>
</code></pre>
<p>Beside the type constructor like <em>Number()</em>, <em>String()</em>, <em>Boolean()</em>, there are some also operator the convert the type of a value.</p>
<p>Like <strong>'+'</strong> operator.</p>
<p>Have a look:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">2</span> + <span class="hljs-string">"2"</span>);  <span class="hljs-comment">//&gt; 22</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">2</span> + +<span class="hljs-string">"2"</span>);  <span class="hljs-comment">//&gt;4</span>
</code></pre>
<p>But why does that happen?</p>
<p>In the expression <code>2+ +”2"</code>, the extra + operator before “2”, force “2” to become a number. Hence the result is 4 rather than 22.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In JavaScript there are three types of types that occur: Number, String and Boolean.</p>
<p>For implicit type conversion the JavaScript compiler handle this on its own without bother us. Where are for explicit type conversion we use type constructor named Number(), String() and Boolean().</p>
<p>So yeah that’s pretty much it about the type conversion in JavaScript.</p>
<h2 id="heading-references">References</h2>
<ul>
<li><a target="_blank" href="https://javascript.info/type-conversions">Type Conversions on JavaScript Info</a></li>
<li><a target="_blank" href="https://en.wikibooks.org/wiki/Computer_Programming/Type_conversion">Computer Programming/Type conversion</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Glossary/Type_Conversion">Mozilla Docs</a></li>
<li><a target="_blank" href="https://www.digitalocean.com/community/tutorials/how-to-convert-data-types-in-javascript">How To Convert Data Types in JavaScript on Digital Ocean</a></li>
</ul>
<blockquote>
<p><strong>This article is officially published on <a target="_blank" href="https://withinbracket.com/posts/type-conversion-in-javascript-the-magic/">Within Bracket</a></strong></p>
</blockquote>
]]></content:encoded></item><item><title><![CDATA[Switch statement in JavaScript]]></title><description><![CDATA[When is comes to controlling the flow of a particular program with making decision, there are typically two options we have: In our previous article we had…]]></description><link>https://sarveshprajapati.hashnode.dev/switch-statement-in-javascript</link><guid isPermaLink="true">https://sarveshprajapati.hashnode.dev/switch-statement-in-javascript</guid><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Sarvesh Prajapati]]></dc:creator><pubDate>Fri, 25 Feb 2022 07:50:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1645775361423/0cioupk93.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When is comes to <strong>controlling</strong> the <em>flow</em> of a particular program with making decision, there are typically two options we have:</p>
<ul>
<li>if…else</li>
<li>switch case</li>
</ul>
<p>In our previous article we had covered everything you need to know about the if..else in JavaScript. Here is the link:</p>
<blockquote>
<p><strong><a target="_blank" href="https://withinbracket.com/posts/if-else-in-javascript-conditional-statement/">if else in JavaScript - Conditional Statements</a></strong></p>
</blockquote>
<p>Now let's explore the switch case statement.</p>
<h2 id="heading-check-out-this-simple-example">Check out this simple example:</h2>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> fruit = <span class="hljs-string">"apple"</span>;
<span class="hljs-keyword">switch</span> (fruit){
 <span class="hljs-keyword">case</span> <span class="hljs-string">"apple"</span>:
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is an apple"</span>);
   <span class="hljs-keyword">break</span>;
 <span class="hljs-keyword">case</span> <span class="hljs-string">"mango"</span>:
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is a mango"</span>);
   <span class="hljs-keyword">break</span>;
 <span class="hljs-keyword">default</span>:
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is a fruit"</span>);
}
<span class="hljs-comment">// Output:</span>
This is an apple
</code></pre>
<p>In case if you are not familiar with <code>switch</code> before, then it's a fairly simple procedure to control a flow of a program.</p>
<h2 id="heading-here-is-a-typical-syntax-of-switch">Here is a typical syntax of <code>switch</code>:</h2>
<pre><code class="lang-js"><span class="hljs-keyword">switch</span>(expression_to_be_checked){
  <span class="hljs-keyword">case</span> first_value:
     <span class="hljs-comment">//do something</span>
     <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> second_value:
     <span class="hljs-comment">//do something else</span>
     <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> third_value:
     <span class="hljs-comment">//do something else</span>
     <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">default</span>:
     <span class="hljs-comment">// do what not defined in above cases</span>
}
</code></pre>
<h2 id="heading-lets-breakdown-the-syntax">Let's breakdown the syntax:</h2>
<ul>
<li><p>every <code>switch</code> statement start with a <code>switch</code> keyword which hold an expression to be checked either as a <code>value</code> or as a <code>variable</code></p>
</li>
<li><p>then we have several <code>cases</code> to check the expression, and do a particular task according to the <code>case</code></p>
</li>
<li><p>in every <code>case</code> block we have a <code>break</code> keyword. It is used to <strong>break</strong> the further flow of the program and limit us to execute task of a particular <em>case</em> block</p>
</li>
<li><p>at the end we have a <code>default</code> case. It executes the <em>default</em> task if no case condition is satisfies.</p>
</li>
</ul>
<p>There you have it the switch case statement in JavaScript.</p>
<blockquote>
<p><strong>But</strong>, <em>it's not gonna end here, there are few important points you need to aware of before using it.</em></p>
</blockquote>
<h2 id="heading-you-can-put-the-default-keyword-wherever-you-want-not-necessary-to-put-at-the-end">You can put the <code>default</code> keyword wherever you want; not necessary to put at the end</h2>
<p>Everywhere you'll find that the <code>default</code> keyword has been put at the end of every required <code>case</code> block, but it's not necessary to put <code>default</code> at the end.</p>
<p>In-fact I prefer putting the default at the <strong>beginning</strong>, because it make me feel comfortable to not worry about any further if I use lot of <code>case</code> block.</p>
<p>Either way it's a subjective matter, so you put it where you want, it's not a big deal.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> animal = <span class="hljs-string">"mouse"</span>;
<span class="hljs-keyword">switch</span> (animal){
  <span class="hljs-attr">default</span>:
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is an animal"</span>);
  <span class="hljs-keyword">case</span> <span class="hljs-string">"rabbit"</span>:
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is an rabbit"</span>);
     <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> <span class="hljs-string">"lion"</span>:
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is a lion"</span>);
}
<span class="hljs-comment">// Output:</span>
This is an animal
</code></pre>
<blockquote>
<p>In-fact I don't need to put <code>break</code> at the last case block, because it makes sense, it's the last case and no other <code>case</code> is going to be checked after the last case block</p>
</blockquote>
<h2 id="heading-not-every-switch-statement-require-a-default-case">Not every switch statement require a default case.</h2>
<p>Yeah you read it right. It's not <strong>mandatory</strong> to use default in every <code>switch</code> statement. You may have condition where you wouldn't need to use <code>default</code> case.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> animal = <span class="hljs-string">"mouse"</span>;
<span class="hljs-keyword">switch</span> (animal){
  <span class="hljs-keyword">case</span> <span class="hljs-string">"rabbit"</span>:
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is an rabbit"</span>);
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> <span class="hljs-string">"lion"</span>:
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is a lion"</span>);
}
<span class="hljs-comment">// Output:</span>
you might not able to see anything <span class="hljs-keyword">as</span> output
</code></pre>
<h2 id="heading-what-if-you-forgot-to-use-break-keyword">What if you forgot to use <code>break</code> keyword</h2>
<p>It's fairly simple answer - <em>All the case block would be executed whether the condition meets or not</em> - Simple.</p>
<p>Let me comment out the <code>break</code> keyword.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> fruit = <span class="hljs-string">"guava"</span>;
<span class="hljs-keyword">switch</span> (fruit){
  <span class="hljs-keyword">case</span> <span class="hljs-string">"apple"</span>:
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is an apple"</span>);
     <span class="hljs-comment">//break;</span>
  <span class="hljs-keyword">case</span> <span class="hljs-string">"mango"</span>:
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is a mango"</span>);
     <span class="hljs-comment">//break;</span>
  <span class="hljs-keyword">default</span>:
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is a fruit"</span>);
}
<span class="hljs-comment">// Output:</span>
This is an apple
This is a mango
</code></pre>
<blockquote>
<p>Not using <code>break</code> keyword can leads to a new experience as well. It's called <strong>multi-case</strong> operation.</p>
</blockquote>
<p>Check this out:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> fruit = <span class="hljs-string">"guava"</span>;
<span class="hljs-keyword">switch</span> (fruit){
  <span class="hljs-keyword">case</span> <span class="hljs-string">"apple"</span>:
  <span class="hljs-keyword">case</span> <span class="hljs-string">"mango"</span>:
  <span class="hljs-keyword">case</span> <span class="hljs-string">"pineapple"</span>:
  <span class="hljs-keyword">case</span> <span class="hljs-string">"guava"</span>:
  <span class="hljs-keyword">case</span> <span class="hljs-string">"watermelon"</span>:
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is a fruit"</span>);
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">default</span>:
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"This is a thing"</span>);
}
<span class="hljs-comment">//Output</span>
This is a fruit.
</code></pre>
<blockquote>
<p>It's also known as <strong>fall-through</strong>.</p>
</blockquote>
<p>Here we use the four case statement perform the same task.</p>
<h2 id="heading-conclusion">Conclusion:</h2>
<p>So this is all you need to know to work with <code>switch</code> case. Sometimes switch statement is used as a alternative of <code>if…else</code>. But they have their own use cases.</p>
<p>It's also seen that <code>switch</code> statement work faster than <code>if…else</code> in most cases if there are too many <strong>conditions</strong> to be checked.</p>
<p>I've illustrated several example and scenarios to explain switch statement, but I would highly recommend to tweak with your own example to have a better understanding.</p>
<p>Cause as they say practice makes perfection.</p>
<p>Thanks for sticking around. Keep Learning.</p>
<blockquote>
<p><strong>This article is officially published on <a target="_blank" href="https://withinbracket.com/posts/switch-statement-in-javascript/">Within Bracket</a></strong></p>
</blockquote>
]]></content:encoded></item><item><title><![CDATA[if else in JavaScript - Conditional Statements]]></title><description><![CDATA[This is what programming is all about.]]></description><link>https://sarveshprajapati.hashnode.dev/if-else-in-javascript-conditional-statements</link><guid isPermaLink="true">https://sarveshprajapati.hashnode.dev/if-else-in-javascript-conditional-statements</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Sarvesh Prajapati]]></dc:creator><pubDate>Fri, 18 Feb 2022 05:15:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1645161229781/rrKVlC0ip.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Bill Gates's once said, "Computer programming is calculation of math and making decision with if..else".</p>
<p>And it is hard to argue with that statement, and why not. Because deep down this is what programming all about.</p>
<p>If you are new to this programming world, consider going through this article for a better reference.: 
📌<a target="_blank" href="https://withinbracket.com/posts/3-things-to-learn-while-programming/">3 Things to learn while Programming</a> </p>
<p>Anyway, if you been in computer programming for a while, you probably know that programs are build upon <code>logics</code>. 
And a logic need to go though certain condition to satisfy a particular problem.</p>
<p>This is where <code>conditional statements</code> comes into the picture. They help us to make decisions to ace a problem.</p>
<p>Generally the <code>conditional statements</code> are referred as <code>if...else</code>. And they are pretty straight forward: </p>
<pre><code class="lang-js"><span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span> happen){
  <span class="hljs-keyword">do</span> <span class="hljs-built_in">this</span> particular task;
}<span class="hljs-keyword">else</span>{
  <span class="hljs-keyword">do</span> <span class="hljs-built_in">this</span>.
}
</code></pre>
<p>Which means, if <code>certain</code> condition met, do a <strong>particular</strong> task, and if the condition is not met, then
do <strong>something</strong> else.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> num = <span class="hljs-number">1</span>;
<span class="hljs-keyword">if</span>(num &gt; <span class="hljs-number">0</span>){ 
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The number is poditive"</span>);
} <span class="hljs-keyword">else</span> {
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The number is negative"</span>);
}
</code></pre>
<h2 id="heading-types-of-conditional-statement">Types of Conditional Statement</h2>
<p>Typically there are two types of conditional statement exists, in any programming language:</p>
<ul>
<li>if...else</li>
<li>else if</li>
</ul>
<p>Let's check out them individually:</p>
<h2 id="heading-ifelse">if...else</h2>
<p>Syntax:</p>
<pre><code class="lang-js"><span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span> condition satisfies){
   <span class="hljs-keyword">do</span> a particular task
} <span class="hljs-keyword">else</span> {
   <span class="hljs-keyword">do</span> something <span class="hljs-keyword">else</span>.
}
</code></pre>
<p>This syntax is similar to what we discuss earlier. If a condition <code>satisfies</code>, do a <strong>particular</strong> task,
otherwise do something else.</p>
<p>Example:</p>
<pre><code class="lang-js"><span class="hljs-comment">// Check if a number is odd or even</span>
<span class="hljs-keyword">let</span> num = <span class="hljs-number">7</span>;
<span class="hljs-keyword">if</span>(num % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>){
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The number is even"</span>);
} <span class="hljs-keyword">else</span> {
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"the number is odd"</span>);
}
</code></pre>
<p>In the above example, we have checked that while dividing a number by 2, if we get <code>0</code> as <strong>reminder</strong> then 
the number is <code>even</code>, and if we get <code>1</code> as reminder then the number is <code>odd</code>.</p>
<p>Note: '%' is the <strong>modulo operator</strong> which gives the <code>reminder</code> while divide a number, 
whereas '/' gives the <code>quotient</code>.{#quote .info__quote}</p>
<h2 id="heading-else-if">else if</h2>
<p>Apart from <code>if...else</code>, we have <code>else if</code>, which is use to add another <strong>condition</strong>.</p>
<p>Syntax:</p>
<pre><code class="lang-js"><span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span> condition satisfies){
   <span class="hljs-keyword">do</span> a particular task
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(check <span class="hljs-built_in">this</span> condition){
   <span class="hljs-keyword">do</span> <span class="hljs-built_in">this</span> task
} <span class="hljs-keyword">else</span> {
   <span class="hljs-keyword">do</span> something <span class="hljs-keyword">else</span>.
}
</code></pre>
<p>In this syntax, you can easily see that here is an another <code>condition</code> to be check.</p>
<p>We can add as many conditions as we want to be checked, with the help of <code>else if</code>.</p>
<p>Example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> num = <span class="hljs-number">5</span>;
<span class="hljs-keyword">if</span>(num &gt; <span class="hljs-number">0</span>){
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The number is positive"</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(num == <span class="hljs-number">0</span>){
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The number is ZERO"</span>);
} <span class="hljs-keyword">else</span> {
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The number is negetive"</span>);
}
</code></pre>
<p>Quite obvious; In the above program we also check that whether or not is the number is <code>0</code>, 
and print <code>log</code> statement associated with that <code>condition</code>, using <code>else..if</code>.</p>
<h2 id="heading-the-nested-condition">The Nested Condition</h2>
<p>So far now, we have checked out, what is a conditional statement and its type. But we have a another version 
of conditional statement with <strong>nesting</strong>.</p>
<p>Nesting means checking a condition <strong>inside</strong> a particlular condition.</p>
<p>❕ Make Sence ? Check this example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> num1 = <span class="hljs-number">5</span>;
<span class="hljs-keyword">let</span> num2 = <span class="hljs-number">8</span>;
<span class="hljs-keyword">if</span>(num1 == num2){
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Both number are equal"</span>);
} <span class="hljs-keyword">else</span> {
   <span class="hljs-keyword">if</span>(num1 &gt; num2){
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"num1 is greater than num2"</span>);
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"num1 is lesser than num2"</span>);
    }
}
</code></pre>
<p>In the above example we have a <code>if..else</code> statement. But inside the <code>else</code> block we have
another <code>if..else</code> statement. This is called Nested Conditional Statement.</p>
<h2 id="heading-things-to-remember">Things to Remember:</h2>
<p>So here we have learned about the conditional statement, but there are some facts you should
consider while using them.</p>
<ul>
<li><code>else</code> always should be followed by a <code>if</code> statement</li>
<li>use <code>else if</code> only if you need to check for another condition</li>
<li>If <code>else</code> done your job, <code>else if</code> might not require to add</li>
</ul>
<h2 id="heading-conclusion">Conclusion;</h2>
<p>So let's wrap this up. In any programming language we have two kind of conditional statement:</p>
<ul>
<li>if..else</li>
<li>else if</li>
</ul>
<p>We also have a nested conditional statement, to check a condition inside a condition.</p>
<p>And this is all about conditional statement in JavaScript.</p>
]]></content:encoded></item><item><title><![CDATA[Data Types in JavaScript — The Weird Parts]]></title><description><![CDATA[The core concept of programming that you need to know.]]></description><link>https://sarveshprajapati.hashnode.dev/data-types-in-javascript-the-weird-parts</link><guid isPermaLink="true">https://sarveshprajapati.hashnode.dev/data-types-in-javascript-the-weird-parts</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Sarvesh Prajapati]]></dc:creator><pubDate>Sat, 12 Feb 2022 09:32:52 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644658255715/K8erCGb323.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you have been into programming for a while, you know what are data types and why they are important in typically every programming language.</p>
<p>But if you don’t know about data types, well it’s just the type of data (value) you stored in a variable — as simple as that.</p>
<h2 id="heading-lets-see-an-example">Let’s see an example</h2>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> name = <span class="hljs-string">'someone'</span>;    <span class="hljs-comment">// data type is string</span>
<span class="hljs-keyword">const</span> num = <span class="hljs-number">101</span>;           <span class="hljs-comment">// data type is number</span>
<span class="hljs-keyword">const</span> isAlive = <span class="hljs-literal">true</span>;      <span class="hljs-comment">// data type is boolean</span>
</code></pre>
<p>So let’s deep dive into data types and explore them.</p>
<p>JavaScript has two types of data types: <code>Primitive</code> and <code>Non-Primitive</code>.</p>
<h2 id="heading-lets-look-at-primitive-data-types">Let’s look at Primitive Data Types</h2>
<blockquote>
<p>Primitive data types are a type which contain only one <strong>particular</strong> value, it can be a string, number or boolean like the example above</p>
</blockquote>
<p>Here is an example:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-number">10</span>);     <span class="hljs-comment">//number</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">true</span>);   <span class="hljs-comment">//boolean</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-string">"sdf"</span>);  <span class="hljs-comment">//string</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-number">10.5</span>);   <span class="hljs-comment">//number</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">false</span>);  <span class="hljs-comment">//boolean</span>
</code></pre>
<h2 id="heading-now-lets-look-at-non-primitive-data-types">Now, let’s look at Non-Primitive Data Types</h2>
<blockquote>
<p>Non-Primitive data types is a type which contains a <strong>collection of data</strong> and this data can be of multiple types: primitive or non-primitive</p>
</blockquote>
<p>In <strong>JavaScript</strong>, the <code>object</code> is the most important non-primitive data-type. As we all know objects are the <strong>barebones</strong> of JavaScript, so we’ll deal with them in another article.</p>
<p>For now, let’s check them out for the sake of understanding the non-primitive data types.</p>
<p>Let’s see an example of this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs-string">"apple"</span>, <span class="hljs-attr">b</span>: <span class="hljs-string">"ball"</span> };
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> obj);  <span class="hljs-comment">//object</span>
</code></pre>
<h2 id="heading-some-more-data-types">Some more data types</h2>
<p>Along with primitive and non-primitive data types, JavaScript has <strong>three</strong> more data types.</p>
<h3 id="heading-1-function">1. function()</h3>
<p>In any programming language, as well as in JavaScript, the thing we use most often is the function.</p>
<p>The function has its own data type called <strong>function</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> whoAmI = <span class="hljs-function">(<span class="hljs-params">who</span>) =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`I am <span class="hljs-subst">${who}</span>`</span>);   <span class="hljs-comment">// I am No one</span>
};
whoAmI(<span class="hljs-string">'No one'</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> whoAmI);   <span class="hljs-comment">// function</span>
</code></pre>
<h3 id="heading-2-undefined">2. undefined</h3>
<p>It simply represents that the value is <strong>not assigned</strong> to a variable.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> name;
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> name);   <span class="hljs-comment">//undefined</span>
</code></pre>
<h3 id="heading-3-null">3. null</h3>
<p>The data type null represents that there is no value — <em>nothing</em>— empty.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> name = <span class="hljs-literal">null</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> name);   <span class="hljs-comment">//null</span>
</code></pre>
<p>You might be confused between <code>undefined</code> and <code>null</code>. But there is one simple explanation</p>
<p>The undefined is <strong>implicit</strong>, meaning we don’t have to set a value (or error value), <code>JavaScript</code> automatically gets it. Whereas in the case of null, it is <strong>explicit</strong>, meaning we have to set it on our own like in the example above.</p>
<h2 id="heading-okay-but-what-about-the-weird-part">Okay! But what about the weird part?</h2>
<p>In the context of data types, I might say that JavaScript is weird in some aspects. So far, I have seen some weirdness with JavaScript like :</p>
<h3 id="heading-1-weirdness-with-in-built-constructor-functions">1. Weirdness with in-built constructor functions</h3>
<p>In JavaScript, we have some in-built construction functions to define the data types of a variable (<em>which you should not use</em>) like String, Object, Date, etc.</p>
<p>Look at the code below:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-built_in">String</span>);  <span class="hljs-comment">//function</span>

<span class="hljs-keyword">const</span> place = <span class="hljs-built_in">String</span>(<span class="hljs-string">"somewhere"</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> place);   <span class="hljs-comment">//string</span>

<span class="hljs-keyword">const</span> fruit = <span class="hljs-keyword">new</span> <span class="hljs-built_in">String</span>(<span class="hljs-string">'fruit'</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> fruit);   <span class="hljs-comment">//object</span>

<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-built_in">Date</span>);   <span class="hljs-comment">//function</span>

<span class="hljs-keyword">const</span> now = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> now);     <span class="hljs-comment">//object</span>

<span class="hljs-keyword">const</span> date = <span class="hljs-built_in">Date</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> date);    <span class="hljs-comment">//function</span>
</code></pre>
<h3 id="heading-2-weirdness-with-null">2. Weirdness with null</h3>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">null</span>);  <span class="hljs-comment">//object</span>

<span class="hljs-keyword">const</span> name = <span class="hljs-literal">null</span>;
<span class="hljs-built_in">console</span>.log(name);   <span class="hljs-comment">//null</span>

<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> name);   <span class="hljs-comment">//object</span>
</code></pre>
<h3 id="heading-3-weirdness-with-objects">3. Weirdness with objects</h3>
<pre><code class="lang-js"> <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-built_in">Object</span>);   <span class="hljs-comment">//function</span>

<span class="hljs-keyword">const</span> item = [<span class="hljs-string">'a'</span>, <span class="hljs-string">'d'</span>];
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> item);   <span class="hljs-comment">//object</span>

<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs-string">"apple"</span>, <span class="hljs-attr">b</span>: <span class="hljs-string">"ball"</span> };
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> obj);   <span class="hljs-comment">//object</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>So, that’s all about Javascript data types and their weirdness. There are also some use-cases that I haven’t mentioned yet. So, if you want to know about them, write the code and explore on your own.</p>
<p>Finally, JavaScript is weird, but that’s the reason I love JavaScript. In future content, we will explore more about JavaScript and its weirdness.</p>
<h3 id="heading-references">References</h3>
<ul>
<li><a target="_blank" href="https://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript">Stackoverflow Forum</a></li>
<li><a target="_blank" href="https://flaviocopes.com/javascript-difference-null-undefined/">Flavio Copes Blog</a></li>
<li><a target="_blank" href="https://appdividend.com/2019/04/08/javascript-null-undefined-and-nan-tutorial-with-example/">App Dividend Article</a></li>
</ul>
<h3 id="heading-more-on-javascript">More on JavaScript</h3>
<ul>
<li><a target="_blank" href="https://withinbracket.com/posts/variables-in-javascript-scope-and-hoisting/">Variables in JavaScript, Scope and Hoisting</a></li>
<li><a target="_blank" href="https://withinbracket.com/posts/type-conversion-in-javascript-the-magic/">Type Conversion in JavaScript - The Magic</a></li>
<li><a target="_blank" href="https://withinbracket.com/posts/loops-in-javascript/">An Introduction to Loops in JavaScript</a></li>
<li><a target="_blank" href="https://withinbracket.com/posts/javascript-equality-double-equals-vs-triple-equals/">JavaScript Equality - Double Equals vs Triple Equals</a></li>
</ul>
<p>🎉Thanks for sticking around. Keep Learning.</p>
<blockquote>
<p><strong>📢This article is officially published on <a target="_blank" href="https://withinbracket.com/posts/data-types-in-javascript-the-weird-parts/">Within Bracket</a></strong></p>
</blockquote>
<p>📌 Find More Articles <a target="_blank" href="https://withinbracket.com/archives">Here</a></p>
]]></content:encoded></item><item><title><![CDATA[How things happen over the Web !]]></title><description><![CDATA[Everything that happen over the web is not a magic. There is certain process through which one can access INTERNET.]]></description><link>https://sarveshprajapati.hashnode.dev/how-things-happen-over-the-web</link><guid isPermaLink="true">https://sarveshprajapati.hashnode.dev/how-things-happen-over-the-web</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[webdev]]></category><category><![CDATA[internet]]></category><dc:creator><![CDATA[Sarvesh Prajapati]]></dc:creator><pubDate>Fri, 11 Feb 2022 11:26:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644578455400/L4wtTg2nG.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey there ! So as you know how you can start your web dev journey.</p>
<p>If you haven't, checkout the post below 👇</p>
<p><a target="_blank" href="https://withinbracket.com/posts/starting-web-development-in-2021/">Starting web development</a></p>
<p>So, where we were !</p>
<p>Yeah, There are couple of things you need to know about web ! 🌞</p>
<p>It's been seen that the way people accessing the web is changing
over the time.</p>
<p>As we all know to access the web, we need a web browser.
But how a browser work ❔</p>
<p>Thought there are a lot of browser, like</p>
<ul>
<li>Chrome</li>
<li>Firefox</li>
<li>Safari</li>
<li>Opera</li>
<li>Microsoft Edge</li>
</ul>
<p>But for the sake of simplicity we take Chrome for the example : 😏</p>
<p>So, if you ever open a browser, you can see a <code>address bar</code> which often referred as Search Bar.</p>
<p>In the address bar, you search something you want to surf.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/kinp5octxm2n2i2e5do0.png" alt="Alt Text" /></p>
<p>So you type in the search bar, like here to type <code>dev community</code> , and once you hit 🥊 the enter ,  there something happen behind the scene in the matter of second, well sometimes millisecond.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/homjbf5u4k1qfcn5iyov.gif" alt="Alt Text" /></p>
<p>After you press the <code>enter</code>, the browser fetch the IP address of whatever you type in the address bar, from the <code>DNS or Domain Name Server</code> </p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/jx4ld5seud6xfeuqidcx.png" alt="Alt Text" /></p>
<p>In DNS every domain store in the form of a unique IP address, the browser <code>request</code> to the IP address and the server give the <code>response</code> according to the request over a <code>standard protocol</code>.</p>
<blockquote>
<p><strong><em>Sounds silly ! 🙄. Let's dive deeper</em></strong> </p>
</blockquote>
<p>Once the <strong>client</strong> make a request to the <strong>server</strong>, at first there is a connection built between the client and the server, called <strong>TCP/IP</strong> <code>Transmission Control Protocol</code></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/4hi1zggsk5mri0ty4rbh.png" alt="Alt Text" /></p>
<p>Now, once the connection build, you can make requests to the server.</p>
<p>The server software are written in different languages like:  </p>
<ul>
<li>java</li>
<li>python</li>
<li>node js</li>
<li>asp.net</li>
</ul>
<p>But they all try to achieve the same goal. They fetch the data according to your request from the server and give you the data.</p>
<p>The data is might be in <strong>HTML</strong> , <strong>XML</strong> or <strong>JSON</strong>. But for the sake of simplicity we assume the data is in <code>HTML</code>.
<img src="https://dev-to-uploads.s3.amazonaws.com/i/nbg26nyssmc9p96ks056.png" alt="Alt Text" /></p>
<p>But you didn't make a request for an <code>HTML</code> page, Right. 🦉</p>
<p>This is where the <strong>Browser</strong> comes in. 🦸‍♂️</p>
<p>If the server gives you the visual rendered page, this might takes a lot of time.</p>
<p>So, it gives you the data in the format of <code>HTML</code> <code>CSS</code> and <code>JS</code> and the browser convert this data into the <strong>Visual rendered page</strong>, in just a flash of second ⌚.
<img src="https://dev-to-uploads.s3.amazonaws.com/i/v5ni2l9p5grmgl4skene.png" alt="Alt Text" />
And your page to ready for your enjoyment. 🎉</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/axqf0b0xzowa7u8gg2c8.gif" alt="Alt Text" /></p>
<h1 id="heading-wrap-things-up">Wrap things up:</h1>
<p>A browser is not only just help you to surf the web but also simplify your work by tracking your activity, restrict errors, keeping secret info, speed up the way you browse with using cache,  and much more.</p>
<p>So, thanks for reading the post. Feel free to comment down your opinion.</p>
<p>📢This article is officially been published on <a target="_blank" href="https://withinbracket.com/posts/how-things-happen-over-web/">Within Bracket</a>.</p>
<p>📌Find more articles <a target="_blank" href="https://withinbracket.com/">Here</a></p>
]]></content:encoded></item><item><title><![CDATA[Variables in JavaScript, Scope and Hoisting]]></title><description><![CDATA[The core concept of programming, one should not skip.]]></description><link>https://sarveshprajapati.hashnode.dev/variables-in-javascript-scope-and-hoisting</link><guid isPermaLink="true">https://sarveshprajapati.hashnode.dev/variables-in-javascript-scope-and-hoisting</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Sarvesh Prajapati]]></dc:creator><pubDate>Thu, 10 Feb 2022 09:33:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644485048989/ZhpVcgI4G.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>Variables are the basic and a most important part of any programming language. They are used to store values that are used in the further execution of a program.</p>
<blockquote>
<p>You can think variables like a box in which we can store something i.e., values.</p>
</blockquote>
<p>In JavaScript variables can store any type of values. It can be a number, string, boolean, array, object, and what else. I don’t want to mess around with data types in this article, we discuss them in another one. Let’s keep our eye only one variable.</p>
<h2 id="heading-lets-take-a-look-how-we-use-a-variable-in-javascript">Let’s take a look how we use a variable in JavaScript:</h2>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> x;            <span class="hljs-comment">//Declare a variable</span>
x=<span class="hljs-number">10</span>;             <span class="hljs-comment">//Assign a value in it</span>
<span class="hljs-built_in">console</span>.log(x);   <span class="hljs-comment">//Use it</span>
</code></pre>
<p>Before <strong>ES6 </strong>use <code>var</code> as variable keyword, but after ES6 there are two new keyword featured for assigning variable <code>let</code> and <code>const</code>.</p>
<p>But why <code>let</code> and <code>const</code>?</p>
<p>To understand the importance of <code>let</code> and <code>const</code>, first we need to know two JavaScript features: <strong>Scoping </strong>and <strong>Hoisting</strong>.</p>
<h2 id="heading-lets-first-discuss-about-scoping">Let’s first discuss about Scoping:</h2>
<blockquote>
<p>Scoping simply means that at what <strong>range </strong>a variable can be accessed.</p>
</blockquote>
<p>In JavaScript there are two type of scope: <code>Global</code> Scope and <code>Local</code> Scope</p>
<p>Didn’t get it ? Okay, don’t worry. Let’s do it practically. Consider following code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> <span class="hljs-built_in">global</span> = <span class="hljs-string">'i am a global variable'</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">doSomething</span>(<span class="hljs-params"></span>) </span>{                
   <span class="hljs-keyword">var</span> local = <span class="hljs-string">'i am a local variable'</span>;  
   <span class="hljs-built_in">console</span>.log(local);                   
}                                       
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">global</span>);
<span class="hljs-built_in">console</span>.log(local);
</code></pre>
<p>Output:</p>
<pre><code class="lang-js">i am a <span class="hljs-built_in">global</span> variable
<span class="hljs-attr">ReferenceError</span>: local is not defined
</code></pre>
<h3 id="heading-whats-is-the-referenceerror">What’s is the ReferenceError ?</h3>
<p>If you see in the above program, I declare two variables <code>global</code>and <code>local</code>.</p>
<p>The <code>local</code> variable is in the <code>doSomething</code> function, so you can’t access it outside the function. It means that the scope of variable local is within the function i.e, Local Scope.</p>
<p>But the variable <code>global</code> is declared outside of the function, so you can access it from anywhere. Thus variable global is in the Global Scope.</p>
<p>After ES6 the local scope is further divided into two part:</p>
<ul>
<li>Functional Scope (function) for <code>var</code></li>
<li>Block Scope (condition or loop) for <code>let</code> and <code>const</code></li>
</ul>
<p>Take a look in below code:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">doSomething</span>(<span class="hljs-params"></span>) </span>{
   <span class="hljs-keyword">if</span> (<span class="hljs-number">1</span>&lt;<span class="hljs-number">2</span>) {
     <span class="hljs-keyword">var</span> cow = <span class="hljs-string">'cow'</span>;
     <span class="hljs-keyword">let</span> dog = <span class="hljs-string">'dog'</span>;
     <span class="hljs-keyword">const</span> cat = <span class="hljs-string">'cat'</span>;
     <span class="hljs-built_in">console</span>.log(cow);   <span class="hljs-comment">//cow</span>
     <span class="hljs-built_in">console</span>.log(dog);   <span class="hljs-comment">//dog</span>
     <span class="hljs-built_in">console</span>.log(cat);   <span class="hljs-comment">//cat</span>
  }
 <span class="hljs-built_in">console</span>.log(cow);     <span class="hljs-comment">//cow</span>
 <span class="hljs-built_in">console</span>.log(dog);     <span class="hljs-comment">//ReferenceError: dog is not defined</span>
 <span class="hljs-built_in">console</span>.log(cat);     <span class="hljs-comment">//ReferenceError: cat is not defined</span>
}
doSomething();
</code></pre>
<p>As you can see if we try to access <code>let</code> and <code>const</code> variable outside of the if(block scope), it give a <code>ReferenceError</code>. However the var variable do its job perfectly within the <strong>function scope.</strong></p>
<p>So that’s being said, the scope of <code>var</code> is functional scope where the scope of <code>let</code> and <code>const</code> is <strong>block scope</strong>.</p>
<h2 id="heading-lets-first-discuss-about-hoisting">Let’s first discuss about Hoisting:</h2>
<blockquote>
<p>When a variable or a function is declared, its declaration is moved to <strong>top </strong>of their <strong>scope</strong>.</p>
</blockquote>
<p>Have a look at below condition;</p>
<ul>
<li>Trying to access a variable before it has been <code>declared</code> and <code>initialized</code></li>
</ul>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(name);  <span class="hljs-comment">//access name before it defined or initialized</span>
<span class="hljs-keyword">var</span> name=<span class="hljs-string">'person'</span>;  <span class="hljs-comment">//define and initialize after it been accessed</span>
<span class="hljs-comment">/* Output */</span>
<span class="hljs-literal">undefined</span>
</code></pre>
<ul>
<li>Trying to access a variable before it <code>initialized</code> without <code>declaring</code> it</li>
</ul>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(name);  <span class="hljs-comment">//access name before it defined or initialized</span>
name=<span class="hljs-string">'person'</span>;      <span class="hljs-comment">//initialize name without it defined</span>
<span class="hljs-comment">/* Output */</span>
<span class="hljs-built_in">ReferenceError</span>: name is not defined
</code></pre>
<p>As we can see, if we access a variable <code>before it has been declared and initialized</code>, it return undefined. However if we access a variable <code>before it initialized without declaring</code> it, it returns a <code>ReferenceError</code>.</p>
<p>It’s seems ok in <strong>second condition</strong> that we not declared <code>name</code>before accessed it, so it gives a <code>ReferenceError</code>, but what happened in <strong>first condition</strong> is that JavaScript automatically declared the variable <code>name</code> before accessed it because we put a <code>var</code> keyword before the variable.</p>
<pre><code class="lang-js"><span class="hljs-comment">//How we write it</span>
<span class="hljs-built_in">console</span>.log(name);  <span class="hljs-comment">//ReferenceError: name is not defined</span>
<span class="hljs-keyword">var</span> name=<span class="hljs-string">'person'</span>; 

<span class="hljs-comment">//How JavaScirpt Manipulate it</span>
<span class="hljs-keyword">var</span> name;
<span class="hljs-built_in">console</span>.log(name);  <span class="hljs-comment">//undefined</span>
name=<span class="hljs-string">'person'</span>;
</code></pre>
<h2 id="heading-lets-see-a-big-picture-regarding-hoisting">Let’s see a big picture regarding hoisting:</h2>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> statement = <span class="hljs-literal">true</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkHoisting</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">//var statement;  /* Javascript automatically declared it here */</span>
  <span class="hljs-keyword">if</span>(<span class="hljs-number">1</span>&gt;<span class="hljs-number">2</span>){
    <span class="hljs-keyword">var</span> statement = <span class="hljs-literal">false</span>;
  }
  <span class="hljs-built_in">console</span>.log(statement); <span class="hljs-comment">// undefined</span>
}
checkHoisting();
</code></pre>
<p>By seeing the example one can easily predict that the output should be <code>true</code>. But due to <strong>hoisting</strong> property JavaScript declared a new statement variable top on top of the <code>checkHoisting</code> function, which is not initialized, thus the output is <code>undefined</code>.</p>
<p>This type of output might cause a weird <code>error</code>.</p>
<p>But that simply not happen it case of <code>let</code>or <code>const</code>. Let’s see.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> statement = <span class="hljs-literal">true</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkHoisting</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span>(<span class="hljs-number">1</span>&gt;<span class="hljs-number">2</span>){
     <span class="hljs-keyword">let</span> statement = <span class="hljs-literal">false</span>;
  }
  <span class="hljs-built_in">console</span>.log(statement);   <span class="hljs-comment">//the global statement variable</span>
}
checkHoisting(); <span class="hljs-comment">//Output: true</span>
</code></pre>
<p><code>let</code> and <code>const</code> are not participate in hoist behavior, cause they are block-scoped variable.</p>
<p>Let’s have a look on another scenario:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> statement = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">var</span> statement = <span class="hljs-literal">false</span>;
<span class="hljs-built_in">console</span>.log(statement); <span class="hljs-comment">// Output:false</span>

<span class="hljs-keyword">let</span> done = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">let</span> done = <span class="hljs-literal">false</span>;
<span class="hljs-built_in">console</span>.log(done);      
<span class="hljs-comment">//Output: SyntaxError: Identifier 'done' has already been declared</span>
</code></pre>
<p><strong>What happened here ? Can you guess why ?</strong></p>
<p>Let me simplify it.</p>
<p>When we declare a variable again with with a different value with <code>var</code>, then due to <strong>hoist</strong> behavior, the value of the variable updated with the latest value, thus the output is <code>false</code>.</p>
<p>But in case of <code>let</code>and <code>const</code>, as they not follow hoist property, it throw a <code>SyntaxError</code> that Identifier <code>done</code>has already been declared.</p>
<p>This duplication of variable can also cause an error.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Due to scope and hoisting, the <code>var</code> keyword might cause an unpredictable result with w don’t want to occur. So as per the <strong>ES6</strong> feature, it good to use <code>let</code> and <code>const</code> instead of <code>var</code> to keep our code less confusing and error free.</p>
<p>That’s being said, that all for this article. I hope this article might help you to understand the variable in JavaScript with scope and hoist property.</p>
<h2 id="heading-references">References</h2>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting">Mozilla Docs</a></li>
<li><a target="_blank" href="https://www.digitalocean.com/community/tutorials/understanding-variables-scope-hoisting-in-javascript">Digital Ocean Community</a></li>
</ul>
<h2 id="heading-more-on-javascript">More on JavaScript</h2>
<ul>
<li><a target="_blank" href="https://withinbracket.com/posts/data-types-in-javascript-the-weird-parts/">Data Types in JavaScript — The Weird Parts</a></li>
<li><a target="_blank" href="https://withinbracket.com/posts/type-conversion-in-javascript-the-magic/">Type Conversion in JavaScript - The Magic</a></li>
<li><a target="_blank" href="https://withinbracket.com/posts/loops-in-javascript/">An Introduction to Loops in JavaScript</a></li>
<li><a target="_blank" href="https://withinbracket.com/posts/javascript-equality-double-equals-vs-triple-equals/">JavaScript Equality - Double Equals vs Triple Equals</a></li>
</ul>
<p>Thanks for sticking around. Keep Learning.</p>
<p>📢<strong>This article is officially been published on <a target="_blank" href="https://withinbracket.com">Within Bracket</a>.</strong></p>
<p>📌Find more articles <a target="_blank" href="https://withinbracket.com/archives">Here</a></p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Equality : Double Equals v/s Triple Equals]]></title><description><![CDATA[Learn about javascript equality. A simple explanation about hoe to use double equal and triple equal in javascript by Sarvesh Prajapati]]></description><link>https://sarveshprajapati.hashnode.dev/javascript-equality-double-equals-vs-triple-equals</link><guid isPermaLink="true">https://sarveshprajapati.hashnode.dev/javascript-equality-double-equals-vs-triple-equals</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Sarvesh Prajapati]]></dc:creator><pubDate>Tue, 03 Aug 2021 08:46:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1627740918278/R5yC105pb.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>One thing we often do as a programmer in any programming language is <strong>Equality Checking</strong>. Whether this value is equals to that value or not.</p>
<p>Whether you know or not in <strong>JavaScript </strong>there are two equality checking operators: 
double equal(==) and triple equal(===). And they often leads to programmer to be confused while using them.</p>
<p>We'll they are not too complicated to understand.</p>
<p>In this article we'll go over some <strong>differences and use cases</strong> like where and how to use them <strong>efficiently </strong>and guess what they are also <em>funnier </em>to learn.</p>
<p>So let's go 🚀.</p>
<p>But hold on before deep dive in this, there is one thing I want you to take below points in your consideration.</p>
<blockquote>
</blockquote>
<ul>
<li><p>All the comparison operator returns Boolean Values after execution. 
Either <strong>true </strong>or <strong>false</strong>.</p>
</li>
<li><p>And as we all know in programming there is only two values <strong>1 </strong>and <strong>0</strong>.
So if we go even further, <strong>true become 1 </strong>and <strong>false become 0</strong>.</p>
</li>
</ul>
<p>Okay then, with that keep in mind let's get into this.</p>
<p>In JavaScript the <strong>comparison </strong>happen in two ways.</p>
<ul>
<li>Comparison with type</li>
<li>Comparison with values</li>
</ul>
<p>So what's the difference between == and === 🤔 ?</p>
<h3 id="one-simple-answer">One simple answer 👇</h3>
<p>The <strong>Double Equals</strong> first <em>convert </em>the type of the operand and then compare them with values. While the <strong>Triple Equals</strong> compare with the value <em>without </em>changing the type of the operand.</p>
<p><em>So that's it ? 😒</em></p>
<p>Hell not. There is more yet to come.</p>
<p>🔔 <strong>But wait, if you don't know about type conversion in JavaScript. Do check the article below 👇. It would make much more sense then:</strong></p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://javascript.plainenglish.io/type-conversion-in-javascript-the-magic-c93cf96ba4da">https://javascript.plainenglish.io/type-conversion-in-javascript-the-magic-c93cf96ba4da</a></div>
<h3 id="now-lets-look-upon-some-scenarios">Now, Let's look upon some scenarios :</h3>
<p>To check whether a value is true or false, we can use <strong>Boolean</strong> object constructor.
Here's how 👇</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>(<span class="hljs-string">'hey'</span>))  <span class="hljs-comment">//true</span>
<span class="hljs-comment">//-- any valid string is true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>(<span class="hljs-number">0</span>))       <span class="hljs-comment">//false</span>
<span class="hljs-comment">//-- as I said earlier 1 is true and 0 is false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>(<span class="hljs-string">'0'</span>))     <span class="hljs-comment">//true</span>
<span class="hljs-comment">//-- why 0 is true here ? Thanks to quotation, which makes it a String</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>(<span class="hljs-string">' '</span>))      <span class="hljs-comment">//false</span>
<span class="hljs-comment">//-- the quotation has no character: not a valid string; false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Boolean</span>([ ]))     <span class="hljs-comment">//true</span>
<span class="hljs-comment">//-- empty array</span>
</code></pre>
<h3 id="more-examples">More examples:</h3>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-literal">false</span> == <span class="hljs-number">0</span>)  <span class="hljs-comment">//true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">0</span> == <span class="hljs-string">''</span>)  <span class="hljs-comment">//true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">''</span> == <span class="hljs-literal">false</span>)  <span class="hljs-comment">//true</span>
</code></pre>
<p><strong>Double Equal</strong> converts <em>false </em>and<em> ' '</em> into 0, that's why they are equal to 0.</p>
<p><strong>But !</strong>, this not gonna happen in case of Triple Equal. Why ? Cause === not converts the type of operand.</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-literal">false</span> === <span class="hljs-number">0</span>)  <span class="hljs-comment">//false</span>
<span class="hljs-comment">//-- false is Boolean while 0 is Number, so they not equal for ===</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">0</span> === <span class="hljs-string">''</span>)  <span class="hljs-comment">//false</span>
<span class="hljs-comment">//-- 0 is Number while '' is string</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">''</span> === <span class="hljs-literal">false</span>)  <span class="hljs-comment">//false</span>
<span class="hljs-comment">//-- '' is String while false is Boolean</span>
</code></pre>
<p>✨ Data type is one of the crucial topic in any programming language; and so as in the JavaScript. I have also written an article on <strong>Data Type in JavaScript</strong>. You can check it here 👇</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://javascript.plainenglish.io/data-types-in-javascript-the-weird-part-151e8fc3f1f3">https://javascript.plainenglish.io/data-types-in-javascript-the-weird-part-151e8fc3f1f3</a></div>
<p><strong>With that said, let's move further.</strong>
 In JavaScript we have: null, undefined and NaN</p>
<ul>
<li>null is a type of object, which mean noting; blank</li>
<li>undefined is a data type itself</li>
<li>NaN is type of Number, which mean not a number</li>
</ul>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">null</span>) <span class="hljs-comment">// object</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">undefined</span> ) <span class="hljs-comment">// undefined </span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">NaN</span>) <span class="hljs-comment">// Number</span>
</code></pre>
<p>So first, let's compare null and undefined with == v/s ===;</p>
<pre><code class="lang-js"> <span class="hljs-built_in">console</span>.log(<span class="hljs-literal">null</span> == <span class="hljs-literal">undefined</span>) <span class="hljs-comment">// true</span>
<span class="hljs-comment">//-- double equal convert null into 0 and undefined as well</span>

 <span class="hljs-built_in">console</span>.log(<span class="hljs-literal">null</span> === <span class="hljs-literal">undefined</span>) <span class="hljs-comment">// false</span>
<span class="hljs-comment">//-- for triple equal null is an object while undefined is undefined</span>
</code></pre>
<blockquote>
<p><strong>null </strong>and <strong>undefined </strong>are only equal to each other and themselves while comparing with Double Equal( == ). If you compare them with any other value you'll get nothing but a <strong>false</strong>.</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-literal">null</span> == <span class="hljs-literal">null</span>) <span class="hljs-comment">//true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">null</span> == <span class="hljs-string">' '</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">null</span> == <span class="hljs-literal">false</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">null</span> == <span class="hljs-number">000</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">null</span> == <span class="hljs-number">123</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">null</span> == []) <span class="hljs-comment">//false</span>

<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">undefined</span>  == <span class="hljs-literal">undefined</span> ) <span class="hljs-comment">//true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">undefined</span> == <span class="hljs-string">' '</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">undefined</span> == <span class="hljs-literal">false</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">undefined</span> == <span class="hljs-number">0</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">undefined</span> == <span class="hljs-number">1</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">undefined</span> == []) <span class="hljs-comment">//false</span>
</code></pre>
<h3 id="now-is-the-time-for-nan">Now is the time for <em>NaN</em></h3>
<p><strong>NaN </strong>is a <em>insane </em>player in the <strong>JavaScript </strong>world. Why? Cause it never equal to any value and guess what ! It not <strong>even </strong>equal to <em>itself</em>.</p>
<p>Are you kidding me ? 😵
No buddy, have a look 👇</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-literal">NaN</span> == <span class="hljs-literal">null</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">NaN</span> == <span class="hljs-number">0</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">NaN</span> == <span class="hljs-number">135</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">NaN</span> == <span class="hljs-string">'NaN'</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">NaN</span> == <span class="hljs-string">'hellow'</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">NaN</span> == <span class="hljs-number">0</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">NaN</span> == <span class="hljs-literal">undefined</span>) <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">NaN</span> == <span class="hljs-literal">NaN</span>) <span class="hljs-comment">//false</span>
</code></pre>
<h1 id="wrapping-up">Wrapping Up:</h1>
<p>So as you saw, one can easily get confused while choosing whether he/she use == or ===.</p>
<p>Let me clarify this. Whenever you need to compare two values always go with ===, cause it gives expected results. But do play with both == and ===, cause programming is fun right ?</p>
<p>So that's it for now.</p>
<h2 id="checkout-my-other-article-here">Checkout my other article here 👇</h2>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://thesarveshprajapati.medium.com/">https://thesarveshprajapati.medium.com/</a></div>
<h3 id="connect-with-me-on-twitter">Connect with me on twitter :</h3>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/iamthesarvesh">https://twitter.com/iamthesarvesh</a></div>
<h3 id="ill-see-you-is-next-one">I'll see you is next one. 😎</h3>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://media.giphy.com/media/l1J3CbFgn5o7DGRuE/giphy.gif">https://media.giphy.com/media/l1J3CbFgn5o7DGRuE/giphy.gif</a></div>
]]></content:encoded></item></channel></rss>