<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
>

<channel>
	<title>JavaScript - Bipon's Diary</title>
	<atom:link href="https://biponnotes.iglyphic.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://biponnotes.iglyphic.com</link>
	<description>Do good for others. It will come back in unexpected ways.</description>
	<lastBuildDate>Thu, 06 Feb 2020 07:21:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.9.10</generator>
<site xmlns="com-wordpress:feed-additions:1">168324471</site>	<item>
		<title>The concept of hoisting in JavaScript</title>
		<link>https://biponnotes.iglyphic.com/the-concept-of-hoisting-in-javascript/</link>
					<comments>https://biponnotes.iglyphic.com/the-concept-of-hoisting-in-javascript/#respond</comments>
		
		<dc:creator><![CDATA[bipon68]]></dc:creator>
		<pubDate>Thu, 06 Feb 2020 07:20:20 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://bipon.me/?p=503</guid>

					<description><![CDATA[<p>Behind the Scenes, you will see that JavaScript always takes all the declarations first. That&#8217;s basically the hosting. Since this is the default behavior in JavaScript. In this article, I will discuss only the hosting specific. Since hosting is the default behavior, all functions in JavaScript are hosted in the Creation phase. That&#8217;s why we can actually call&#8230;<a href="https://biponnotes.iglyphic.com/the-concept-of-hoisting-in-javascript/" class="more-link">Continue reading <span class="screen-reader-text">The concept of hoisting in JavaScript</span></a></p>
<p>The post <a href="https://biponnotes.iglyphic.com/the-concept-of-hoisting-in-javascript/">The concept of hoisting in JavaScript</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Behind the Scenes, you will see that JavaScript always takes all the declarations first. That&#8217;s basically the hosting. Since this is the default behavior in JavaScript.  In this article, I will discuss only the hosting specific. </p>



<p>Since hosting is the default behavior, all functions in JavaScript are hosted in the Creation phase. That&#8217;s why we can actually call a function before declaring it. </p>



<pre class="wp-block-code"><code>customFunc();

 function customFunc(){
     let a = 77;
     let b = 4;
     let sum = a + b;
     console.log('Sum : ' + sum) 
 }</code></pre>



<figure class="wp-block-image"><img width="424" height="199" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/02/5-1.png?resize=424%2C199&#038;ssl=1" alt="" class="wp-image-504" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/5-1.png?w=424&amp;ssl=1 424w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/5-1.png?resize=300%2C141&amp;ssl=1 300w" sizes="(max-width: 424px) 100vw, 424px" data-recalc-dims="1" /></figure>



<p> The reason for working is for JavaScript default behavior in the Creation phase.  Here are some points for convenience: </p>



<ul><li>The arguments list of the arguments that are passed to the function is created</li><li>The code scans all the functions and each function is stored in a variable object that usually points to the function.</li><li>Then it looks for the declarations of the code variables. And <code>undefined</code> sets the property for each declared variable .</li><li></li></ul>



<p>Then the code goes to the execution phase and the line-by-line code is executed according to the execution context.</p>



<p>And that is why since function declaration is earlier in the creation phase and the function call is executed in execution phase, we can use it even before declaring function.</p>



<p>But function expressions will not work this way. Because in function expressions we store our functions in a variable. And that variable <code>undefined</code>is set as the creation phase . The original function is executed later, so calling the function before declare in the function expression will show the error as it is not a function. Because it&#8217;s not really a function right now, just a variable.</p>



<pre class="wp-block-code"><code> miew();

 var miewVar = function(){
     console.log('Miew is calling')
 }</code></pre>



<figure class="wp-block-image"><img loading="lazy" width="506" height="95" src="https://i0.wp.com/bipon.me/wp-content/uploads/2020/02/3-1.png?resize=506%2C95&#038;ssl=1" alt="" class="wp-image-505" srcset="https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/3-1.png?w=506&amp;ssl=1 506w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/3-1.png?resize=300%2C56&amp;ssl=1 300w" sizes="(max-width: 506px) 100vw, 506px" data-recalc-dims="1" /></figure>



<p> Similarly, we can use a variable before declaring it. If you use a variable before declaring it <code>undefined</code> because of the Creation phase, it will not show any error, but it will show that variable <code>undefined</code> as it is set in Creation Phase . </p>



<pre class="wp-block-code"><code>console.log('Value of Miew: ' + miew);
var miew = 10</code></pre>



<figure class="wp-block-image"><img loading="lazy" width="427" height="117" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/02/6.png?resize=427%2C117&#038;ssl=1" alt="" class="wp-image-506" srcset="https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/6.png?w=427&amp;ssl=1 427w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/6.png?resize=300%2C82&amp;ssl=1 300w" sizes="(max-width: 427px) 100vw, 427px" data-recalc-dims="1" /></figure>



<p>But if you have never declared that variable later, you will want to use it: </p>



<pre class="wp-block-code"><code>console.log('Value of C: ' + miewC);</code></pre>



<figure class="wp-block-image"><img loading="lazy" width="434" height="115" src="https://i1.wp.com/bipon.me/wp-content/uploads/2020/02/7-1.png?resize=434%2C115&#038;ssl=1" alt="" class="wp-image-507" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/7-1.png?w=434&amp;ssl=1 434w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/7-1.png?resize=300%2C79&amp;ssl=1 300w" sizes="(max-width: 434px) 100vw, 434px" data-recalc-dims="1" /></figure>



<p>So hopefully this time, there is a clear idea about hosting. However, for good practice it is not advisable to use a function before declaring it. But there&#8217;s definitely a lot of work to be done to understand how JavaScript works within the scene. In some situations, it may be helpful to have such a technique. </p><p>The post <a href="https://biponnotes.iglyphic.com/the-concept-of-hoisting-in-javascript/">The concept of hoisting in JavaScript</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://biponnotes.iglyphic.com/the-concept-of-hoisting-in-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">503</post-id>	</item>
		<item>
		<title>Behind the Scene in JavaScript</title>
		<link>https://biponnotes.iglyphic.com/behind-the-scene-in-javascript/</link>
					<comments>https://biponnotes.iglyphic.com/behind-the-scene-in-javascript/#respond</comments>
		
		<dc:creator><![CDATA[bipon68]]></dc:creator>
		<pubDate>Wed, 05 Feb 2020 17:34:50 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://bipon.me/?p=484</guid>

					<description><![CDATA[<p>If you understand how Code Behind works in the scene, you can write code at your convenience anyway.&#160;In this episode of mine today, I will discuss as much detail as possible about how JavaScript works within the scene. JavaScript is usually run in an environment.&#160;Often it may be our most widely used browser, or another&#8230;<a href="https://biponnotes.iglyphic.com/behind-the-scene-in-javascript/" class="more-link">Continue reading <span class="screen-reader-text">Behind the Scene in JavaScript</span></a></p>
<p>The post <a href="https://biponnotes.iglyphic.com/behind-the-scene-in-javascript/">Behind the Scene in JavaScript</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></description>
										<content:encoded><![CDATA[<p> If you understand how Code Behind works in the scene, you can write code at your convenience anyway.&nbsp;In this episode of mine today, I will discuss as much detail as possible about how JavaScript works within the scene. </p>



<p> JavaScript is usually run in an environment.&nbsp;Often it may be our most widely used browser, or another application program such as&nbsp;<a rel="noreferrer noopener" aria-label=" (opens in a new tab)" target="_blank" href="https://nodejs.org/en/">Node JS</a>&nbsp;. </p>



<p> Now from where this JavaScript is run, there is also a JavaScript engine (Google V8 Engine, Spider Monkey, JavaScript Core etc…) whose function is to execute this JavaScript code.&nbsp;Browsers use JavaScript to make JavaScript run.&nbsp;For example, Google Chrome uses Google V8 engine, Firefox uses Spider-Manki / Rihano, Internet Explorer and Edge servers use the engine, and Safari uses JavaScript-core / Nitro (JavaScript has many engines,&nbsp;<a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/JavaScript_engine#JavaScript_engines" target="_blank">see here for</a>&nbsp;full reference&nbsp;).</p>



<p> Now this JavaScript code executes in several steps and runs. </p>



<pre class="wp-block-code"><code>Code > Parser > Convert to machine code > code runs</code></pre>



<p>Here, the code passes through the parser first.&nbsp;Here the syntax error is usually checked.&nbsp;If a syntax error is found, then the error is thrown from here.&nbsp;And if all the syntax is done, then a data structure called Abstract Syntax Tree is created.</p>



<p>The next step is to convert this code into machine code, which our computer can actually understand.&nbsp;And finally our code is run.</p>



<p>Now why do you have to cross such steps before running a code?&nbsp;Yes, our computer does not understand anything but the machine code.&nbsp;Whatever language we write in code, it must be converted to machine code, or our computer may not understand it.</p>



<h2 id="9040">Execution Context and Execution Stack</h2>



<p>It is very important to have clear ideas about Execution Context and Execution Stack.&nbsp;There are many trick issues in JavaScript that are difficult to understand unless you can understand exactly the Execution Context and the Execution Stack.</p>



<p><strong>Execution Context:</strong></p>



<p>By default Javascript executes global context.&nbsp;Unlike some other languages, no space functions are executed.&nbsp;And JavaScript&#8217;s Global Execution Context is a browser&nbsp;<code>window</code>object.&nbsp;Declaring a global variable or assigning it to an&nbsp;<code>window</code>object is the same</p>



<pre class="wp-block-code"><code>let bGlobal = 77;</code></pre>



<p>Now I can see if it is <code>window </code>inside:<code>bGlobal </code></p>



<p>window.<code>bGlobal </code> </p>



<p> Then you see that this seam&nbsp;<code>bGlobal</code>shows the value of&nbsp;your declared </p>



<figure class="wp-block-image"><img loading="lazy" width="428" height="140" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/02/1.png?resize=428%2C140&#038;ssl=1" alt="" class="wp-image-485" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/1.png?w=428&amp;ssl=1 428w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/1.png?resize=300%2C98&amp;ssl=1 300w" sizes="(max-width: 428px) 100vw, 428px" data-recalc-dims="1" /></figure>



<pre class="wp-block-code"><code>bGlobal === window.bGlobal //true</code></pre>



<figure class="wp-block-image"><img loading="lazy" width="422" height="174" src="https://i0.wp.com/bipon.me/wp-content/uploads/2020/02/2-1.png?resize=422%2C174&#038;ssl=1" alt="" class="wp-image-490" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/2-1.png?w=422&amp;ssl=1 422w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/2-1.png?resize=300%2C124&amp;ssl=1 300w" sizes="(max-width: 422px) 100vw, 422px" data-recalc-dims="1" /></figure>



<p>That means two things are the same.&nbsp;So&nbsp;<code>window</code>it is the same thing to&nbsp;declare a global variable or assign it to an&nbsp;object.</p>



<p>However, if your environment is not a browser, then this Global Execution Context may be different depending on the environment.&nbsp;For example, Node JS&#8217;s Global Execution Context is an&nbsp;<code>global</code>object.&nbsp;Depending on the environment it may change.</p>



<p>Execution context is discussed in more detail later.</p>



<p><strong>Execution Stack:</strong></p>



<p>We&#8217;ll use an example to get a clear idea about Execution Stack in JavaScript and see how those codes are executed.</p>



<pre class="wp-block-code"><code>function A(){
    let a = ' call from a';
    B();
    console.log('Hey', name + a);
}
function B(){
    let b = ' call from b';
    C();
    console.log('Hey', name +  b)
}
function C(){
    let c = ' call from c';
    console.log('Hey', name + c)
}
A();</code></pre>



<p> If you run this code above, you will see how it runs: </p>



<figure class="wp-block-image"><img loading="lazy" width="429" height="129" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/02/3.png?resize=429%2C129&#038;ssl=1" alt="" class="wp-image-489" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/3.png?w=429&amp;ssl=1 429w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/3.png?resize=300%2C90&amp;ssl=1 300w" sizes="(max-width: 429px) 100vw, 429px" data-recalc-dims="1" /></figure>



<p>The last ones are coming before and the first ones are later.&nbsp;How does it look random?&nbsp;Yes here we need to understand how the execution stack works.&nbsp;So only then will we have a clear idea of ​​how this code ran.&nbsp;First notice this diagram below: </p>



<figure class="wp-block-image"><img loading="lazy" width="720" height="386" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/02/5.png?resize=720%2C386&#038;ssl=1" alt="" class="wp-image-491" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/5.png?w=720&amp;ssl=1 720w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/5.png?resize=300%2C161&amp;ssl=1 300w" sizes="(max-width: 720px) 100vw, 720px" data-recalc-dims="1" /></figure>



<p>Here the number codes will be executed according to the serial number.&nbsp;And the diagram on the right is the Basically Execution Stack.&nbsp;If you have no idea about the stack data structure, then the stack is a structure where the first in-out system is maintained.&nbsp;This means that there will be only one way to get data out, and whenever a data is extracted it can be extracted from the top.&nbsp;If you have several glass plates stacked on top of one, then if you want to take a plate, you have to take the top plate first, otherwise you will not be able to take it down or you will still have an accident.&nbsp;The plate that you place at the end of the stack should be taken if you want to take a plate on the data structure system, since this plate is on top of you.&nbsp;And the plate you put first will be the bottom,</p>



<p>Now when our code is number 1 it is&nbsp;assigned to the&nbsp;<code>name</code>variable&nbsp;<code>Bipon</code>.&nbsp;Then only the function definitions go one by one&nbsp;<code>A()</code>, then&nbsp;<code>B()</code>and finally into the&nbsp;<code>C()</code>execution stack on the global execution context.&nbsp;Nothing will be excluded from inside the function as we have not called any of the functions yet.</p>



<p>Then we&nbsp;<code>A()</code>call the function&nbsp;at number 5&nbsp;.&nbsp;It will now be positioned on the global execution context in the first execution stack and will now&nbsp;<code>A()</code>start executing the code from within.&nbsp;Now with a variable at number 1, some data is assigned there.&nbsp;But again another function&nbsp;<code>B()</code>is called&nbsp;at number 5&nbsp;.&nbsp;So now Execution Stack A&nbsp;<code>A()</code>will have another new execution context&nbsp;<code>B()</code>on it and now&nbsp;<code>B()</code>the code will start&nbsp;executing&nbsp;from within.&nbsp;Now here also some data has been assigned to a new variable in number 1.&nbsp;Again another function called number 7&nbsp;<code>C()</code>is called.&nbsp;So now another execution context&nbsp;<code>C()</code>will be found in the&nbsp;execution stack&nbsp;and now&nbsp;<code>C()</code>the code will start executing from within.&nbsp;Now <code>C()</code>Inside number 1 again, some new data is assigned with a new variable.&nbsp;And the console is logged in the next line which will now show on your console.&nbsp;So the log here is the first thing you see in your output.&nbsp;Now&nbsp;<code>C()</code>execute is over and it will move away.&nbsp;So now the execution context will go&nbsp;<code>B()</code>as it is now at the top of the execution stack.&nbsp;Now&nbsp;<code>B()</code>inside the console log of the number 12 will be printed on the output and so you will see this as the output in the second position.&nbsp;Now the same way&nbsp;<code>B()</code>, the last one and the execution stack will be gone.&nbsp;Now the execution stack will be at the top&nbsp;<code>A()</code>.&nbsp;So now&nbsp;<code>A()</code>it will be executed.<code>A()</code>Console logs located at number 5 will be printed at the output and this function will be finished and removed from the execution stack.&nbsp;And so you can see the output here.&nbsp;This is how the execution stack works in JavaScript.&nbsp;And so we see output like this.</p>



<p><strong>Details on Execution Context:</strong></p>



<p>It is very important to know the details of the execution context.&nbsp;You may have seen in JavaScript that you can call before you define a function and that code will work perfectly.</p>



<pre class="wp-block-code"><code>miew();
function miew() {
 console.log('Miew');
}</code></pre>



<p>This code will work fine if you run it all together.</p>



<p>Or you can use a variable before defining it (before defining it will show the value undefined, no error).</p>



<pre class="wp-block-code"><code>console.log(miew);
var miew = 'Hello World!';</code></pre>



<p>Output will come like this but no error will come </p>



<p><code>undefined</code></p>



<figure class="wp-block-image"><img loading="lazy" width="381" height="46" src="https://i1.wp.com/bipon.me/wp-content/uploads/2020/02/7.png?resize=381%2C46&#038;ssl=1" alt="" class="wp-image-492" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/7.png?w=381&amp;ssl=1 381w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/02/7.png?resize=300%2C36&amp;ssl=1 300w" sizes="(max-width: 381px) 100vw, 381px" data-recalc-dims="1" /></figure>



<p>What causes it?&nbsp;Yes, that&#8217;s why we need to have a more in-depth knowledge of execution context so you can understand why it works.</p>



<p>When a function is called, there are two stages:</p>



<p>2.&nbsp;Creation phase</p>



<p>2.&nbsp;Execution Phase</p>



<p><strong>2.&nbsp;Creation Phase: In</strong>&nbsp;Creation Phase, the variable object is created first.&nbsp;Then the scope chain is created.</p>



<p>What happens in the creation phase directly in Bengal:</p>



<ul><li>The arguments list of the arguments that are passed to the function is created</li><li>The code scans all the functions and each function is stored in a variable object that usually points to the function.</li><li>Then it looks for the declarations of the code variables.&nbsp;And&nbsp;<code>undefined</code>sets the property for&nbsp;each declared variable&nbsp;.</li></ul>



<p><strong>2.&nbsp;Execution Phase:</strong>&nbsp;Then the code is executed in this phase.&nbsp;In the case of code execution, the code execution is executed by executing the context.&nbsp;This is why calling a function before defining it works fine.&nbsp;If you use any other variable before declaring it&nbsp;<code>undefined</code> is set as a&nbsp;value&nbsp;and no error shows.</p>



<p>Reference Source : <a href="https://codeburst.io/js-essentials-the-javascript-engine-302ff38e8465">JS Essentials: The JavaScript Engine</a> , <a href="https://codeburst.io/@misaogura">Execution Context</a> , <a href="https://medium.com/better-programming/understanding-the-this-keyword-in-javascript-cb76d4c7c5e8">Understanding the “this” Keyword in JavaScript</a> , <a href="https://medium.com/%E0%A6%AA%E0%A7%8D%E0%A6%B0%E0%A7%8B%E0%A6%97%E0%A7%8D%E0%A6%B0%E0%A6%BE%E0%A6%AE%E0%A6%BF%E0%A6%82-%E0%A6%AA%E0%A6%BE%E0%A6%A4%E0%A6%BE/%E0%A6%9C%E0%A6%BE%E0%A6%AD%E0%A6%BE%E0%A6%B8%E0%A7%8D%E0%A6%95%E0%A7%8D%E0%A6%B0%E0%A6%BF%E0%A6%AA%E0%A7%8D%E0%A6%9F%E0%A6%83-%E0%A6%AC%E0%A6%BF%E0%A6%B9%E0%A7%8D%E0%A6%AF%E0%A6%BE%E0%A6%87%E0%A6%A8%E0%A7%8D%E0%A6%A1-%E0%A6%A6%E0%A7%8D%E0%A6%AF%E0%A6%BE-%E0%A6%B8%E0%A7%80%E0%A6%A8-4857f19f4059">Behind the Scene</a></p><p>The post <a href="https://biponnotes.iglyphic.com/behind-the-scene-in-javascript/">Behind the Scene in JavaScript</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://biponnotes.iglyphic.com/behind-the-scene-in-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">484</post-id>	</item>
		<item>
		<title>JavaScript: async and await</title>
		<link>https://biponnotes.iglyphic.com/javascript-async-and-await/</link>
					<comments>https://biponnotes.iglyphic.com/javascript-async-and-await/#respond</comments>
		
		<dc:creator><![CDATA[bipon68]]></dc:creator>
		<pubDate>Tue, 28 Jan 2020 10:02:27 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[await]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://bipon.me/?p=469</guid>

					<description><![CDATA[<p>If the arrow function is: This is how you can identify asynchronous code in your function. This time you have to identify the original asynchronous codes one by one and tell Javascript that this is my promise that it cannot go to the next instruction until it is solved. Before that we will need a promiss, right? Yes,&#8230;<a href="https://biponnotes.iglyphic.com/javascript-async-and-await/" class="more-link">Continue reading <span class="screen-reader-text">JavaScript: async and await</span></a></p>
<p>The post <a href="https://biponnotes.iglyphic.com/javascript-async-and-await/">JavaScript: async and await</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></description>
										<content:encoded><![CDATA[<pre class="wp-block-code"><code>async function customAsyncCode() {
   // Asynchronous Codes
}  </code></pre>



<p> If the arrow function is: </p>



<pre class="wp-block-code"><code> const customAsyncCode = async() => {      
     // Asynchronous Codes    
} </code></pre>



<p> This is how you can identify asynchronous code in your function. This time you have to identify the original asynchronous codes one by one and tell Javascript that this is my promise that it cannot go to the next instruction until it is solved. Before that we will need a promiss, right? Yes, let me start with a promise: </p>



<pre class="wp-block-code"><code>      const a1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          if(true){
            resolve('a 1 resolved');
          }else{
            reject('a 1 error');
          }
        }, 3000)
      })</code></pre>



<p> Now we will handle this promise inside an asynchronous function. That is why I will call our Promise talk inside with an asynchronous identifying function. Now this is where we <code>await</code>need the keyword. With this we will identify our Promise so that this Promise does not go to the next instruction until completed: </p>



<pre class="wp-block-code"><code>const promiseHandle = async() => {
        const data = await a1;
        console.log(data);
      }
      promiseHandle();</code></pre>



<p> Notice here that I just printed the data from our premise in the next line. Yes, here <code>async</code>is <code>await</code>the magic of it. It helps our asynchronous code behave asynchronously so that we no longer have to call the callback. We can retrieve data with a single line-by-line instruction. Now if you call this function you will see our desired data: </p>



<pre class="wp-block-code"><code>promiseHandle()</code></pre>



<p> Now you will see that your console is printed on the next line of console log. If you have trouble understanding it, then <code>setTimeout()</code>you can extend the time and test it yourself by working code synchronously: </p>



<figure class="wp-block-image"><img loading="lazy" width="486" height="182" src="https://i0.wp.com/bipon.me/wp-content/uploads/2020/01/1-5.png?resize=486%2C182&#038;ssl=1" alt="" class="wp-image-470" srcset="https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/1-5.png?w=486&amp;ssl=1 486w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/1-5.png?resize=300%2C112&amp;ssl=1 300w" sizes="(max-width: 486px) 100vw, 486px" data-recalc-dims="1" /></figure>



<p> Now if the promise is rejected? Yes then <code>try…catch</code>we can handle this rejection or error with our previous block. Before that, our promise to create another such thing: </p>



<pre class="wp-block-code"><code>      const a1Rejection = new Promise((resolve, reject) => {
        setTimeout(() => {
          if(true){
            reject('a 1 error');
          }else{
            resolve('a 1 resolved');
          }
        }, 3000)
      })</code></pre>



<p> Now we <code>async await</code>will create our function. But before we can take data from this promise, we have to <code>try…catch</code>put the whole thing inside the block. And with that <code>catch</code>block we can access our error message: </p>



<pre class="wp-block-code"><code>     const promiseErrorHandle = async() => {
        try{
          const data = await a1Rejection;
          console.log(data);
        }catch(err){
          console.log(err);
        }
      }
</code></pre>



<pre class="wp-block-code"><code>promiseErrorHandle();</code></pre>



<p><strong>An alternative method to handle more than one promise</strong> , now we want to promise more than they can handle  synchronously. Suppose we have two promise: </p>



<pre class="wp-block-code"><code>   const a1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          if(true){
            resolve('a 1 resolved');
          }else{
            reject('a 1 error');
          }
        }, 3000)
      })</code></pre>



<p>

Another one

</p>



<pre class="wp-block-code"><code>     const a2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            if(true) {
              resolve('a 2 Resolved');
            } else {
              reject('a 2 Error');
            }
        }, 3000)
      })</code></pre>



<p> Now here&#8217;s something to understand. If we <code>Promise.all</code>use it, let&#8217;s see if it returns: </p>



<pre class="wp-block-code"><code>console.log('Return all Promise :', Promise.all([a1, a2]))</code></pre>



<p> That means the promise also returns: </p>



<figure class="wp-block-image"><img loading="lazy" width="571" height="146" src="https://i0.wp.com/bipon.me/wp-content/uploads/2020/01/2-2.png?resize=571%2C146&#038;ssl=1" alt="" class="wp-image-471" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/2-2.png?w=571&amp;ssl=1 571w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/2-2.png?resize=300%2C77&amp;ssl=1 300w" sizes="(max-width: 571px) 100vw, 571px" data-recalc-dims="1" /></figure>



<p> So with that we can easily handle multiple promises like this: </p>



<pre class="wp-block-code"><code> const handleMultiplePromise = async() => {
        const data = await Promise.all([a1, a2]);
        console.log(data);
}</code></pre>



<p> Well then how come their data? Yes the data will come in the same array size as we have seen before. Now call the function: </p>



<pre class="wp-block-code"><code>handleMultiplePromise();
</code></pre>



<figure class="wp-block-image"><img loading="lazy" width="568" height="182" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/3-3.png?resize=568%2C182&#038;ssl=1" alt="" class="wp-image-472" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/3-3.png?w=568&amp;ssl=1 568w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/3-3.png?resize=300%2C96&amp;ssl=1 300w" sizes="(max-width: 568px) 100vw, 568px" data-recalc-dims="1" /></figure><p>The post <a href="https://biponnotes.iglyphic.com/javascript-async-and-await/">JavaScript: async and await</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://biponnotes.iglyphic.com/javascript-async-and-await/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">469</post-id>	</item>
		<item>
		<title>Promise in JavaScript</title>
		<link>https://biponnotes.iglyphic.com/promise-in-javascript/</link>
					<comments>https://biponnotes.iglyphic.com/promise-in-javascript/#respond</comments>
		
		<dc:creator><![CDATA[bipon68]]></dc:creator>
		<pubDate>Mon, 27 Jan 2020 18:10:29 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[front-end]]></category>
		<category><![CDATA[front-end-development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[promise]]></category>
		<guid isPermaLink="false">https://bipon.me/?p=464</guid>

					<description><![CDATA[<p>Creating a Promise: The task of the Promise is to handle such asynchronous operations. Now we are running the operation on a remote server, but until data arrives we cannot say that operation will not succeed or fail. And basically, the promise works to handle these. In most cases, we do not have to create our own promises. We&#8230;<a href="https://biponnotes.iglyphic.com/promise-in-javascript/" class="more-link">Continue reading <span class="screen-reader-text">Promise in JavaScript</span></a></p>
<p>The post <a href="https://biponnotes.iglyphic.com/promise-in-javascript/">Promise in JavaScript</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></description>
										<content:encoded><![CDATA[<p><strong>Creating a <code>Promise</code>:</strong></p>



<p> The task of the <code>Promise </code>is to handle such <code>asynchronous operations</code>. Now we are running the operation on a remote server, but until data arrives we cannot say that operation will not succeed or fail. And basically, the promise works to handle these. In most cases, we do not have to create our own promises. We handle Just Promise. The basic structure of the promise is that the library we work with will be implemented from the system. We have to handle just Promise. But I&#8217;ll also look at how we can create our own promise here. </p>



<pre class="wp-block-code"><code>    let bb = new Promise((resolve, reject) => {
      let a = 1 + 1;
      if(a == 2){
        resolve('Success man!')
      }else{
        reject('Faild man!')
      }
    })

    bb.then((message) => {
      console.log('Get', message)
    }).catch((message) =>{
      console.log('Get', message)
    })</code></pre>



<p> Promise takes two arguments, <code>resolve</code>and <code>reject</code>. </p>



<p><strong>Handle Promise:</strong></p>



<p> Now we will handle whether the Promise has succeeded or failed. In that case, if the Promise is successful then we can chain our <code>bb</code> function and <code>.then()</code>give a callback function here, which <code>resolve</code>will run if the Promise is successful or otherwise: </p>



<pre class="wp-block-code"><code>    bb.then((message) => {
      console.log('Get', message)
    }).catch((message) =>{
      console.log('Get', message)
    })</code></pre>



<p><a href="https://bipon.me/callback-function/">Callback</a></p>



<p> After that <code>true</code>argument <code>resolve</code>, our callback will be triggered by the promise : </p>



<pre class="wp-block-code"><code>  bb.then((message) => {
      console.log('Get', message)
    })</code></pre>



<p> And if the promise is rejected, then we need <code>.catch()</code>to handle it in another chain operation with a callback function. Now we do not know whether the operation will succeed or fail. In that case, we <code>.then()</code>have to <code>.catch()</code>keep both. <code>.catch()</code>If for any reason we will run <code>reject</code>: </p>



<pre class="wp-block-code"><code>.catch((message) =>{
      console.log('Get', message)
    })</code></pre>



<p> The arguments <code>false</code>given here <code>reject</code>will be from the promise . And so  <code>catch</code>the callback will run inside the block after the operation is finished :</p>



<pre class="wp-block-code"><code>  let bb = new Promise((resolve, reject) => {
      let a = 1 + 2;
      if(a == 2){
        resolve('Success man!')
      }else{
        reject('Faild man!')
      }
    })

    bb.then((message) => {
      console.log('Get', message)
    }).catch((message) =>{
      console.log('Get', message)
    })</code></pre>



<p>The <code>bb</code>function that we first returned the Promise here, the creation of the Promise, in most cases, does not require us to write. Rather, the system we use to exchange data, or use the library, is coded as a part of how to do <code>resolve</code>or not do <code>reject</code>it. In most cases, the promise of our return <code>.then()</code>, and <code>.catch()</code>with the handle. The callbacks that are used inside, in most cases, the data we want from our remote server comes as arguments. We can access those arguments from within a callback. </p>



<p><strong>Handling</strong> Multiple Promises: At times, we may have to handle multiple promos. For example, we have two premises: </p>



<pre class="wp-block-code"><code>      const a1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          if(true){
            resolve('a 1 resolved');
          }else{
            reject('a 1 error');
          }
        }, 3000)
      })</code></pre>



<p> We can also directly create promises like this. Below I made another Promise: </p>



<pre class="wp-block-code"><code>      const a2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            if(true) {
              resolve('a 2 Resolved');
            } else {
              reject('a 2 Error');
            }
        }, 3000)
      })</code></pre>



<p>

Then&nbsp;<code>.then()</code>I can make a callback call which will run soon after these two promises are completed.&nbsp;And the data from these promos will be arrayed in this callback function:

</p>



<pre class="wp-block-code"><code>Promise.all([a1, a2]).then((promiseArr) => {console.log(promiseArr)}) </code></pre><p>The post <a href="https://biponnotes.iglyphic.com/promise-in-javascript/">Promise in JavaScript</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://biponnotes.iglyphic.com/promise-in-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">464</post-id>	</item>
		<item>
		<title>Callback Function in JavaScript</title>
		<link>https://biponnotes.iglyphic.com/callback-function/</link>
					<comments>https://biponnotes.iglyphic.com/callback-function/#comments</comments>
		
		<dc:creator><![CDATA[bipon68]]></dc:creator>
		<pubDate>Fri, 24 Jan 2020 17:01:07 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[synchronous]]></category>
		<guid isPermaLink="false">https://bipon.me/?p=419</guid>

					<description><![CDATA[<p>Callback function means that it is a function that executes after another function is executed. And that&#8217;s why it&#8217;s called callback function. #synchronous&#160;function #Asynchronous&#160;function #Infinity Loop Now what&#8217;s the callback job? We know about the asynchronous behavior of JavaScript. If JavaScript takes time to do a task, do not wait and move on to the&#8230;<a href="https://biponnotes.iglyphic.com/callback-function/" class="more-link">Continue reading <span class="screen-reader-text">Callback Function in JavaScript</span></a></p>
<p>The post <a href="https://biponnotes.iglyphic.com/callback-function/">Callback Function in JavaScript</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Callback function means that it is a function that executes after another function is executed. And that&#8217;s why it&#8217;s called callback function.</p>



<p> #synchronous&nbsp;function </p>



<pre class="wp-block-code"><code>    function synch(func){
      func("synchronous")
    }
    synch(alert)
    alert('Test')</code></pre>



<figure class="wp-block-image"><img loading="lazy" width="472" height="331" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/1-4.png?resize=472%2C331&#038;ssl=1" alt="" class="wp-image-420" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/1-4.png?w=472&amp;ssl=1 472w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/1-4.png?resize=300%2C210&amp;ssl=1 300w" sizes="(max-width: 472px) 100vw, 472px" data-recalc-dims="1" /></figure>



<p> #Asynchronous&nbsp;function </p>



<pre class="wp-block-code"><code>setTimeout(alert,1000, "Asynchronous");
    alert('Test')</code></pre>



<p>#<strong>Infinity Loop</strong></p>



<pre class="wp-block-code"><code>function _1(){
      console.log('First function')
      _2();
    }
    function _2(){
      console.log('Second function');
      _1();
    }
    _1();</code></pre>



<p>Now what&#8217;s the <code>callback </code>job? We know about the <code>asynchronous </code>behavior of JavaScript. If JavaScript takes time to do a task, do not wait and move on to the next code:</p>



<pre class="wp-block-code"><code>    const getCustomFunction = () => {
      setTimeout(function(){
        console.log('A function that takes some time');
      }, 3000)
    }
    const printAnotherFunction = () => {
      console.log('Another Function');
    }
    getCustomFunction();
    printAnotherFunction();</code></pre>



<p>Running this code will show the next as before, and the next one for <code>JavaScript asynchronous behavior</code>:</p>



<figure class="wp-block-image"><img loading="lazy" width="543" height="241" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/3-2.png?resize=543%2C241&#038;ssl=1" alt="" class="wp-image-427" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/3-2.png?w=543&amp;ssl=1 543w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/3-2.png?resize=300%2C133&amp;ssl=1 300w" sizes="(max-width: 543px) 100vw, 543px" data-recalc-dims="1" /></figure>



<p>From the definition of a <code>callback </code>function, we know that it is executed after another function is executed. And so we can use this technique here by writing two functions individually, but we can do it by the <code>callback </code>at the exact time the function is called:</p>



<pre class="wp-block-code"><code>    const getCustomFunction = (callback) => {
      setTimeout(function(){
        console.log('A function that takes some time');
        callback();
      }, 3000)
    }
    const printAnotherFunction = () => {
      console.log('Another Function');
    }
    getCustomFunction(printAnotherFunction);</code></pre>



<p>We passed our function here as <code>an argument inside</code> our Main function call. And then I called it exactly where I needed it. This  <code>printAnotherFunction()</code> function here is the callback function. It will give us results like mind. Means one after another. It will wait exactly 3 seconds then give the result. But serial maintenance does. Promise first, the data will come from it, then run the callback function:</p>



<figure class="wp-block-image"><img loading="lazy" width="670" height="211" src="https://i0.wp.com/bipon.me/wp-content/uploads/2020/01/4-2.png?resize=670%2C211&#038;ssl=1" alt="" class="wp-image-428" srcset="https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/4-2.png?w=670&amp;ssl=1 670w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/4-2.png?resize=300%2C94&amp;ssl=1 300w" sizes="(max-width: 670px) 100vw, 670px" data-recalc-dims="1" /></figure><p>The post <a href="https://biponnotes.iglyphic.com/callback-function/">Callback Function in JavaScript</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://biponnotes.iglyphic.com/callback-function/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">419</post-id>	</item>
		<item>
		<title>Filter</title>
		<link>https://biponnotes.iglyphic.com/filter/</link>
					<comments>https://biponnotes.iglyphic.com/filter/#respond</comments>
		
		<dc:creator><![CDATA[bipon68]]></dc:creator>
		<pubDate>Tue, 14 Jan 2020 08:28:12 +0000</pubDate>
				<category><![CDATA[ES6]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://bipon.me/?p=364</guid>

					<description><![CDATA[<p>The filter is the same as the map, but if the function applied here returns true, then it will move to the new array, or if the function returns false then that item will not be in the array. Suppose I want to extract only the odd numbers from the following array and place them&#8230;<a href="https://biponnotes.iglyphic.com/filter/" class="more-link">Continue reading <span class="screen-reader-text">Filter</span></a></p>
<p>The post <a href="https://biponnotes.iglyphic.com/filter/">Filter</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>The filter is the same as the <code>map</code>, but if the function applied here returns <code>true</code>, then it will move to the new array, or if the function returns <code>false </code>then that item will not be in the array.<br> Suppose I want to extract only the odd numbers from the following array and place them in the new array:</p>



<p> <code>var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] </code></p>



<p>Now, by applying a filter method, I can simplify it</p>



<pre class="wp-block-code"><code>var newArr = arr.filter(function(item) {
   return item % 2 == 0
})</code></pre>



<p>Functions are assigned to each <code>item</code>. And the one that returns <code>true </code>here is the new <code>array</code>. And the one that returns <code>false </code>is not in the new array.<br> You can also call here with an anonymous function or an external function, just like a map. View the Result:</p>



<p><code> newArr </code></p>



<figure class="wp-block-image"><img loading="lazy" width="200" height="56" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/13.png?resize=200%2C56&#038;ssl=1" alt="" class="wp-image-365" data-recalc-dims="1"/></figure>



<p>The filter also accentuates <code>three arguments</code> on the map:</p>



<figure class="wp-block-image"><img loading="lazy" width="540" height="240" src="https://i1.wp.com/bipon.me/wp-content/uploads/2020/01/14.png?resize=540%2C240&#038;ssl=1" alt="" class="wp-image-366" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/14.png?w=540&amp;ssl=1 540w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/14.png?resize=300%2C133&amp;ssl=1 300w" sizes="(max-width: 540px) 100vw, 540px" data-recalc-dims="1" /></figure>



<p>Using ES6 syntax</p>



<p><code> const newArr = arr.filter(item => item % 2 === 0); </code></p><p>The post <a href="https://biponnotes.iglyphic.com/filter/">Filter</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://biponnotes.iglyphic.com/filter/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">364</post-id>	</item>
		<item>
		<title>Map</title>
		<link>https://biponnotes.iglyphic.com/map/</link>
					<comments>https://biponnotes.iglyphic.com/map/#respond</comments>
		
		<dc:creator><![CDATA[bipon68]]></dc:creator>
		<pubDate>Tue, 14 Jan 2020 07:32:46 +0000</pubDate>
				<category><![CDATA[ES6]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://bipon.me/?p=359</guid>

					<description><![CDATA[<p>The map() method creates a new array with the results of calling a provided function on every element in the calling array Suppose you have an array: var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; Now you want to find the square of each item in this array. So what you&#8230;<a href="https://biponnotes.iglyphic.com/map/" class="more-link">Continue reading <span class="screen-reader-text">Map</span></a></p>
<p>The post <a href="https://biponnotes.iglyphic.com/map/">Map</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></description>
										<content:encoded><![CDATA[<p> The <strong>map()</strong> method creates a new array with the results of calling a provided function on every element in the calling array </p>



<p>Suppose you have an array:</p>



<p><code> var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; </code></p>



<p>Now you want to find the square of each item in this array. So what you can do:</p>



<pre class="wp-block-code"><code>var anotherArr = []
for(var i = 0; i &lt; arr.length; i++) {
   anotherArr.push(arr[i] * arr[i])
}</code></pre>



<p>Now you will get your desired result in <strong><code>anotherArr</code></strong>. But even if that is true, here we have a new array to look at. Again we have to use another method <code>push</code>. And there are a few other uses inside that are actually Meaningless. No matter the only thing you want to do in the middle of the map. The function you want to apply to each of your items first. Square of each item you want:</p>



<pre class="wp-block-code"><code>function getSquare(item) {
   return item * item
}</code></pre>



<p>Now you want to apply this function to every item in your array. This is where the map () comes in.</p>



<p><code>arr.map(getSquare)</code></p>



<p>Now we know that this also returns an array. Now where to store that array:</p>



<p><code> var newArr = arr.map(getSquare); </code></p>



<figure class="wp-block-image"><img loading="lazy" width="323" height="320" src="https://i1.wp.com/bipon.me/wp-content/uploads/2020/01/11.png?resize=323%2C320&#038;ssl=1" alt="" class="wp-image-360" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/11.png?w=323&amp;ssl=1 323w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/11.png?resize=150%2C150&amp;ssl=1 150w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/11.png?resize=300%2C297&amp;ssl=1 300w" sizes="(max-width: 323px) 100vw, 323px" data-recalc-dims="1" /></figure>



<p>Now the whole thing can be written like this</p>



<pre class="wp-block-code"><code>var newArr = arr.map(function(item) {
   return item * item;
})</code></pre>



<p>This will give the same result as before. Now here we see that we have used an <code>item </code>as an argument. But here, <code>map ()</code> accepts three arguments. The first argument is that it selects every item in the array, the second argument is the index number of the item in the array, and the third number will always return the whole array.</p>



<pre class="wp-block-code"><code>var newArr = arr.map(function(item, index, fullArr) {
    console.log('Item: ' + item + ' and index: ' + index + '. Full Array: ' + fullArr)
})</code></pre>



<p><strong>Result:</strong></p>



<figure class="wp-block-image"><img loading="lazy" width="452" height="183" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/12.png?resize=452%2C183&#038;ssl=1" alt="" class="wp-image-361" srcset="https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/12.png?w=452&amp;ssl=1 452w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/12.png?resize=300%2C121&amp;ssl=1 300w" sizes="(max-width: 452px) 100vw, 452px" data-recalc-dims="1" /></figure>



<p>And if you use ES6 syntax then see how it is:</p>



<p> <code>const newArr = arr.map(item => item * item); </code></p><p>The post <a href="https://biponnotes.iglyphic.com/map/">Map</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://biponnotes.iglyphic.com/map/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">359</post-id>	</item>
		<item>
		<title>Javascript ES6 &#8211; Map</title>
		<link>https://biponnotes.iglyphic.com/javascript-es6-map/</link>
					<comments>https://biponnotes.iglyphic.com/javascript-es6-map/#respond</comments>
		
		<dc:creator><![CDATA[bipon68]]></dc:creator>
		<pubDate>Fri, 10 Jan 2020 07:51:17 +0000</pubDate>
				<category><![CDATA[ES6]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://bipon.me/?p=328</guid>

					<description><![CDATA[<p>Javascript&#8217;s ES6 has nothing quite new in our eyes without syntax differences, or syntactic sugar.&#160;But this time we will discuss something completely new.&#160;Yes this thing has been added to ES6 completely new.&#160;It never existed in any previous version of JavaScript.&#160;That&#8217;s the map&#160;Map. Do not mistake&#160;this map&#160;Mapas a map&#160;map.&#160;The reason&#160;mapis a method.&#160;But the map we&#160;Mapwill talk&#8230;<a href="https://biponnotes.iglyphic.com/javascript-es6-map/" class="more-link">Continue reading <span class="screen-reader-text">Javascript ES6 &#8211; Map</span></a></p>
<p>The post <a href="https://biponnotes.iglyphic.com/javascript-es6-map/">Javascript ES6 – Map</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Javascript&#8217;s ES6 has nothing quite new in our eyes without syntax differences, or syntactic sugar.&nbsp;But this time we will discuss something completely new.&nbsp;Yes this thing has been added to ES6 completely new.&nbsp;It never existed in any previous version of JavaScript.&nbsp;That&#8217;s the map&nbsp;<code>Map</code>.</p>



<p>Do not mistake&nbsp;this map&nbsp;<code>Map</code>as a map&nbsp;<code>map</code>.&nbsp;The reason&nbsp;<code>map</code>is a method.&nbsp;But the map we&nbsp;<code>Map</code>will talk about&nbsp;here today&nbsp;is a data structure.&nbsp;The new introduction has been introduced to developers in the ES6 version of JavaScript.&nbsp;But this is a bit different from the object type data structure.&nbsp;It has its own method, its own properties.&nbsp;The map can also access all kinds of functions, booleans, integers, characters, strings.&nbsp;So we will discuss this in detail today.</p>



<h3 id="f564">Map (&nbsp;<code>Map)</code>create:</h3>



<p><strong>Taking a new map: If</strong>&nbsp;we want to take a new map data structure, we can take it like this:</p>



<p>  <code>const&nbsp;bName&nbsp;=&nbsp;new&nbsp;Map() </code></p>



<p> Diameter!&nbsp;There&nbsp;<code>bName</code>a map called.&nbsp;Now inside the map there are keys and values.&nbsp;If we want to put any new value inside this map: </p>



<p> <code>bName.set('fullName',&nbsp;'Bipon&nbsp;Biswas'); </code></p>



<p> Now if you want to access any data from the map, there is another method.&nbsp;Want to know&nbsp;<code>bName</code>&nbsp;<code>fullName</code> </p>



<p><code> bName.get('fullName') </code></p>



<figure class="wp-block-image"><img loading="lazy" width="624" height="138" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/1-2.png?resize=624%2C138&#038;ssl=1" alt="" class="wp-image-332" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/1-2.png?w=624&amp;ssl=1 624w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/1-2.png?resize=300%2C66&amp;ssl=1 300w" sizes="(max-width: 624px) 100vw, 624px" data-recalc-dims="1" /></figure>



<p> Again we can also check if there is a specific value in our map: </p>



<p><code> bName.has('fullName'); </code></p>



<p> Since our map&nbsp;<code>fullName</code>is what it is, it will be true: </p>



<figure class="wp-block-image"><img loading="lazy" width="630" height="142" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/2-1.png?resize=630%2C142&#038;ssl=1" alt="" class="wp-image-335" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/2-1.png?w=630&amp;ssl=1 630w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/2-1.png?resize=300%2C68&amp;ssl=1 300w" sizes="(max-width: 630px) 100vw, 630px" data-recalc-dims="1" /></figure>



<p> Now if that is not the case then it will be false: </p>



<p> <code>bName.has('something'); </code></p>



<figure class="wp-block-image"><img loading="lazy" width="674" height="180" src="https://i1.wp.com/bipon.me/wp-content/uploads/2020/01/3-1.png?resize=674%2C180&#038;ssl=1" alt="" class="wp-image-336" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/3-1.png?w=674&amp;ssl=1 674w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/3-1.png?resize=300%2C80&amp;ssl=1 300w" sizes="(max-width: 674px) 100vw, 674px" data-recalc-dims="1" /></figure>



<p> Now we can delete any such pair from the map if we want.&nbsp;First, we have not added another new pair: </p>



<p><code> bName.set('sajib',&nbsp;'biswas') </code></p>



<p> Now if you want to double check whether the new pair has been added to the map: </p>



<p> <code>bName.has('sajib'); </code></p>



<p> Now we want&nbsp;<code>sajib</code>delete&nbsp;this&nbsp;removal: </p>



<p> <code>bName.delete('sajib'); </code></p>



<p> Diameter!&nbsp;Deleted.&nbsp;Now if I check this is&nbsp;<code>sajib</code>there: </p>



<p> <code>bName.has('sajib'); </code></p>



<figure class="wp-block-image"><img loading="lazy" width="589" height="138" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/4-1.png?resize=589%2C138&#038;ssl=1" alt="" class="wp-image-337" srcset="https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/4-1.png?w=589&amp;ssl=1 589w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/4-1.png?resize=300%2C70&amp;ssl=1 300w" sizes="(max-width: 589px) 100vw, 589px" data-recalc-dims="1" /></figure>



<p> That would be a lie, because we&#8217;ve already deleted it from our map </p>



<figure class="wp-block-image"><img loading="lazy" width="753" height="208" src="https://i1.wp.com/bipon.me/wp-content/uploads/2020/01/5-1.png?resize=753%2C208&#038;ssl=1" alt="" class="wp-image-338" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/5-1.png?w=753&amp;ssl=1 753w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/5-1.png?resize=300%2C83&amp;ssl=1 300w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/5-1.png?resize=750%2C207&amp;ssl=1 750w" sizes="(max-width: 753px) 100vw, 753px" data-recalc-dims="1" /></figure>



<p> Now we&nbsp;<code>bName</code>can clear the&nbsp;entire&nbsp;map&nbsp;if we want&nbsp;: </p>



<p><code> bName.clear(); </code></p>



<p> Now if you check&nbsp;<code>bName</code>, nothing can be found inside: </p>



<p> <code>console.log(bName); </code></p>



<figure class="wp-block-image"><img loading="lazy" width="627" height="223" src="https://i0.wp.com/bipon.me/wp-content/uploads/2020/01/6-1.png?resize=627%2C223&#038;ssl=1" alt="" class="wp-image-339" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/6-1.png?w=627&amp;ssl=1 627w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/6-1.png?resize=300%2C107&amp;ssl=1 300w" sizes="(max-width: 627px) 100vw, 627px" data-recalc-dims="1" /></figure>



<p> Suppose we have a map like the following with a few elements: </p>



<p><code> const&nbsp;boxItem&nbsp;=&nbsp;new&nbsp;Map(); </code></p>



<p> Now put some elements inside it: </p>



<pre class="wp-block-code"><code>boxItem.set('name', 'Bipon Biswas');
boxItem.set('job', 'student');
boxItem.set('color', 'blue')
boxItem.set('os', 'windows')
boxItem.set('mobile', 'iOS')
boxItem.set('city', 'dhaka')</code></pre>



<p> Now you will find&nbsp;<code>boxItem</code> all the data inside&nbsp;this&nbsp;map: </p>



<p><code> console.log(boxItem); </code></p>



<figure class="wp-block-image"><img loading="lazy" width="1163" height="307" src="https://i1.wp.com/bipon.me/wp-content/uploads/2020/01/7.png?fit=1024%2C270&amp;ssl=1" alt="" class="wp-image-341" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/7.png?w=1163&amp;ssl=1 1163w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/7.png?resize=300%2C79&amp;ssl=1 300w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/7.png?resize=768%2C203&amp;ssl=1 768w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/7.png?resize=1024%2C270&amp;ssl=1 1024w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/7.png?resize=750%2C198&amp;ssl=1 750w" sizes="(max-width: 1040px) 100vw, 1040px" /></figure>



<p> </p>



<p> If we want to know how many elements inside the map we can find the properties of the map&nbsp;<code>size</code>: Now we can also loop in the map if we want: </p>



<p> <code>console.log(boxItem.size); </code></p>



<figure class="wp-block-image"><img loading="lazy" width="756" height="160" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/8.png?resize=756%2C160&#038;ssl=1" alt="" class="wp-image-342" srcset="https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/8.png?w=756&amp;ssl=1 756w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/8.png?resize=300%2C63&amp;ssl=1 300w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/8.png?resize=750%2C159&amp;ssl=1 750w" sizes="(max-width: 756px) 100vw, 756px" data-recalc-dims="1" /></figure>



<pre class="wp-block-code"><code>boxItem.forEach((value, key) => console.log(`Key is: ${key} and the value is: ${value}`));</code></pre>



<p><code>forEach</code>&nbsp;In the second argument in the method, we usually get the index but here in the case of the map we get the key of the map: </p>



<figure class="wp-block-image"><img loading="lazy" width="761" height="231" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/9.png?resize=761%2C231&#038;ssl=1" alt="" class="wp-image-343" srcset="https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/9.png?w=761&amp;ssl=1 761w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/9.png?resize=300%2C91&amp;ssl=1 300w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/9.png?resize=750%2C228&amp;ssl=1 750w" sizes="(max-width: 761px) 100vw, 761px" data-recalc-dims="1" /></figure>



<p> Likewise, we can&nbsp;<code>for…of</code>also run a loop: </p>



<pre class="wp-block-code"><code>for(let [key, value] of favorite.entries()) {
   console.log(`Key is: ${key} and the value is: ${value}`);
}</code></pre>



<figure class="wp-block-image"><img loading="lazy" width="762" height="222" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/10.png?resize=762%2C222&#038;ssl=1" alt="" class="wp-image-344" srcset="https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/10.png?w=762&amp;ssl=1 762w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/10.png?resize=300%2C87&amp;ssl=1 300w, https://i1.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/10.png?resize=750%2C219&amp;ssl=1 750w" sizes="(max-width: 762px) 100vw, 762px" data-recalc-dims="1" /></figure>



<p> <strong>Note</strong>: One of the important differences is also that you’re able to use anything for the keys. You’re not just limited to primitive values like symbols, numbers, or strings, but you can even use functions, objects and dates – too. Keys won’t be casted to strings like with regular objects, either.</p>



<pre class="wp-block-code"><code>var map = new Map()
map.set(new Date(), function today () {})
map.set(() => 'key', { pony: 'foo' })
map.set(Symbol('items'), [1, 2])</code></pre>



<p>This is how we can use the newly introduced map in JavaScript ES6. Now, is there more to it than other data structures? Yes this is</p>



<p>In the Map Data Structure:</p>



<p>1.&nbsp;We can easily run the loop.</p>



<p>2.&nbsp;We can find out the size of the map</p>



<p>3. Finally, it is much easier to insert or delete data from the map.</p>



<p><strong><a href="https://ponyfoo.com/articles/es6-maps-in-depth">Reference Source</a></strong></p><p>The post <a href="https://biponnotes.iglyphic.com/javascript-es6-map/">Javascript ES6 – Map</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://biponnotes.iglyphic.com/javascript-es6-map/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">328</post-id>	</item>
	</channel>
</rss>
