<?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 Archives | Mithle.sh</title>
	<atom:link href="https://mithle.sh/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://mithle.sh/tag/javascript/</link>
	<description>The Diary of a Full Stack Developer</description>
	<lastBuildDate>Sun, 19 Nov 2023 15:23:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>

<image>
	<url>https://i0.wp.com/mithle.sh/wp-content/uploads/2023/03/cropped-favicon.png?fit=32%2C32&#038;ssl=1</url>
	<title>javascript Archives | Mithle.sh</title>
	<link>https://mithle.sh/tag/javascript/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">219607879</site>	<item>
		<title>Nullish Coalescing Operator (??) vs Logical OR (&#124;&#124;)   in JavaScript</title>
		<link>https://mithle.sh/null-coalescing-vs-logical-or-operator-javascript/</link>
					<comments>https://mithle.sh/null-coalescing-vs-logical-or-operator-javascript/#respond</comments>
		
		<dc:creator><![CDATA[Mithlesh]]></dc:creator>
		<pubDate>Thu, 01 Jun 2023 13:43:46 +0000</pubDate>
				<category><![CDATA[Typescript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://mithle.sh/?p=1360</guid>

					<description><![CDATA[<p>Nullish Coalescing Operator (??) and Logical OR (&#124;&#124;) are two operators in JavaScript/TypeScript that are used to handle situations where a variable may be null...</p>
<p>The post <a href="https://mithle.sh/null-coalescing-vs-logical-or-operator-javascript/">Nullish Coalescing Operator (??) vs Logical OR (||)   in JavaScript</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p><strong>Nullish Coalescing Operator (??)</strong> and <strong>Logical OR (||) </strong>are two operators in JavaScript/TypeScript that are used to handle situations where a variable may be<strong> null</strong> or <strong>undefined</strong>. While they may appear to be similar, there are some subtle differences between them that are important to understand.</p>



<h2 class="wp-block-heading"><strong>Nullish Coalescing Operator (??)</strong> </h2>



<p>The Nullish Coalescing Operator (??) is a <strong>JavaScript/Typescript</strong> operator that provides a concise way to handle <strong>nullish (null or undefined)</strong> values in expressions. It allows you to specify a default value that will be returned if the value on the <strong>left side</strong> of the operator is <strong>nullish</strong>.</p>



<pre class="wp-block-code language-javascript"><code>const result = value1 ?? value2;</code></pre>



<p>If <strong>value1</strong> is <strong>nullish</strong> (null or undefined), it returns <strong>value2</strong>. Otherwise, it returns <strong>value1</strong>.</p>



<p>Here&#8217;s an example to illustrate its usage:</p>



<pre class="wp-block-code language-javascript"><code>const url = null;
const defaultUrl = "https://mithle.sh";
const redirectTo = url ?? defaultUrl

console.log(redirectTo); // Output: "https://mithle.sh"</code></pre>



<p>The nullish coalescing operator <strong><code>??</code></strong> checks if the value on the left side (<strong>url</strong>) is <strong><code>null</code></strong> or <strong><code>undefined</code></strong>. If it is, it uses the value on the right side (<strong>defaultUrl</strong>). In other words, if <strong><code>url</code></strong> is not provided or has no value, <strong><code>redirectTo</code></strong> will be set to the value of <strong><code>defaultUrl</code></strong>. However, if <strong><code>url</code></strong> has a value (not <strong>null</strong> or <strong>undefined</strong>), then<code> <strong>redirectTo</strong></code> will be set to the value of <strong><code>url</code></strong>.</p>



<p>Since <strong><code>url</code></strong> is <strong><code>null</code></strong> in this case, <strong><code>redirectTo</code></strong> will be set to <strong><code><a href="https://mithle.sh">https://mithle.sh</a></code></strong>, and that&#8217;s what will be displayed in the console.</p>



<h2 class="wp-block-heading">Logical OR Operator (||)</h2>



<p>In Javascript/Typescript, the Logical OR Operator <strong>(||)</strong> is used to check if the left-hand side operand is <strong>truthy</strong>. If it is, then the left-hand side operand is returned. Otherwise, the right-hand side operand is returned.</p>



<p>Here&#8217;s an example:</p>



<pre class="wp-block-code language-javascript"><code>const name = null; 
const fullName = name || "John Doe";
console.log(fullName); // Output: "John Doe"
</code></pre>



<p>In this case, if <code>name</code> is truthy (not <code>null</code>, <code>undefined</code>, <code>false</code>, <code>0</code>, <code>empty string</code>, or <code>NaN</code>), the variable <code>name</code> will be assigned the value of <code>fullName</code>. Otherwise, it will be assigned the string <code>'John Doe'</code>.</p>



<p>Remember that the logical OR operator returns the actual value of the operand, not just <code>true</code> or <code>false</code>.</p>



<h2 class="wp-block-heading">What&#8217;s the difference?</h2>



<p>Here are the main differences between the <strong>Nullish Coalescing Operator (??)</strong> and <strong>Logical OR Operator (||)</strong> in JavaScript:</p>



<ol>
<li><strong>Nullish check:</strong> The <strong>Nullish Coalescing Operator</strong> in Javascript only checks for <code>null</code> or <code>undefined</code>, while the <strong>Logical OR Operator</strong> in Javascript checks for any falsy value (such as <code>null</code>, <code>undefined</code>, <code>0</code>, <code>false</code>, <code>empty string</code>, or <code>NaN</code>).</li>



<li><strong>Return value:</strong> The <strong>Nullish Coalescing Operator</strong> returns the right-hand side operand only if the left-hand side operand is <code>null</code> or <code>undefined</code>. <strong>The Logical OR Operator</strong> returns the right-hand side operand if the left-hand side operand is <strong>falsy</strong>.</li>



<li><strong>Precedence:</strong> The <strong>Nullish Coalescing Operator</strong> has <strong>higher precedence</strong> than the <strong>Logical OR Operator</strong>. This means that if both operators are used in the same statement, the Nullish Coalescing Operator will be evaluated first.</li>



<li><strong>Use case:</strong> The <strong>Nullish Coalescing Operator</strong> is typically used to provide default values for variables that could be <code>null</code> or <code>undefined</code>. The <strong>Logical OR Operator</strong> is often used for boolean operations or to provide default values for variables that could be any falsy value.</li>
</ol>



<p>Here&#8217;s an example to illustrate the difference between the two operators:</p>



<pre class="wp-block-code language-javascript"><code>const age = 0;
const defaultAge = 18;

const result1 = age ?? defaultAge;
const result2 = age || defaultAge;

console.log(result1); // Output: 0
console.log(result2); // Output: 18</code></pre>



<p>In this example, <code>age</code> is <code>0</code> and <code>defaultAge</code> is <code>18</code>. When we use the <strong>Nullish Coalescing Operator</strong>, <code>result1</code> is assigned the value of <code>age</code> because <code>age</code> has a defined value (<code>0</code>). When we use the <strong>Logical OR Operator</strong>, <code>result2</code> is assigned the value of <code>defaultAge</code> because <code>age</code> is <strong>falsy</strong> (i.e., <code>0</code>).</p>
<p>The post <a href="https://mithle.sh/null-coalescing-vs-logical-or-operator-javascript/">Nullish Coalescing Operator (??) vs Logical OR (||)   in JavaScript</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mithle.sh/null-coalescing-vs-logical-or-operator-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1360</post-id>	</item>
		<item>
		<title>How to use Async Await with For Loop in Javascript &#038; Typescript</title>
		<link>https://mithle.sh/javascript-for-loop-async-await/</link>
					<comments>https://mithle.sh/javascript-for-loop-async-await/#respond</comments>
		
		<dc:creator><![CDATA[Mithlesh]]></dc:creator>
		<pubDate>Mon, 20 Mar 2023 23:27:56 +0000</pubDate>
				<category><![CDATA[Typescript]]></category>
		<category><![CDATA[frontend]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://mithle.sh/?p=1321</guid>

					<description><![CDATA[<p>In this quick tutorial we will learn how to use async await with for loops in Javascript and Typescript using simple examples. There are mainly...</p>
<p>The post <a href="https://mithle.sh/javascript-for-loop-async-await/">How to use Async Await with For Loop in Javascript &#038; Typescript</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this quick tutorial we will learn how to use async await with for loops in Javascript and Typescript using simple examples.</p>



<p>There are mainly three types of for loops in JavaScript:</p>



<ol>
<li><strong>for loop:</strong> A <code>for</code> loop allows you to execute a block of code repeatedly for a specified number of times.</li>



<li><strong>for…in loop:</strong> A <code>for/in</code> loop allows you to iterate over the properties of an object, executing the loop body once for each property.</li>



<li><strong>for…of loop:</strong> A <code>for/of</code> loop allows you to iterate over the values of an iterable object, executing the loop body once for each value. </li>
</ol>



<p>Consider the following object array. Suppose we want to iterate over the users array, run some time consuming task on each iteration before logging the user object to the console.</p>



<pre class="wp-block-code line-numbers language-javascript"><code>const users = &#91;
    {
        id: 1,
        name: 'Alan',
    },
    {
        id: 2,
        name: 'Joel',
    },
    {
        id: 3,
        name: 'Triss',
    }
];</code></pre>



<p>To use <code>await</code> in a for loop, you must mark the loop function as <code>async</code>, and then use <code>await</code> inside the loop to wait for each asynchronous operation to complete before moving on to the next iteration. </p>



<p>In the following example, the code is same for each type of the loops except the syntax of the loop method.</p>



<h2 class="wp-block-heading"><u>Async await with for loop</u></h2>



<pre class="wp-block-code line-numbers language-javascript"><code>async function randomWait() {
    await new Promise((resolve) =&gt; setTimeout(resolve, Math.floor(Math.random() * 100)));
    return;
}

async function printUser() {
    for(let i=0; i &lt; users.length; i++) {
        // Wait for some time consuming task to finish
        await randomWait();
        console.log(users&#91;i]);
    }
}

printUser();</code></pre>



<pre class="wp-block-code language-json"><code>OUTPUT: 
{ id: 1, name: 'Alan' }
{ id: 2, name: 'Joel' }
{ id: 3, name: 'Triss' }</code></pre>



<h2 class="wp-block-heading"><u>Async await with for…in loop</u></h2>



<pre class="wp-block-code line-numbers language-javascript"><code>async function printUser() {
    for(const index in users) {
        await randomWait();
        console.log(users&#91;index]);
    }
}</code></pre>



<pre class="wp-block-code language-json"><code>OUTPUT: 
{ id: 1, name: 'Alan' }
{ id: 2, name: 'Joel' }
{ id: 3, name: 'Triss' }</code></pre>



<h2 class="wp-block-heading"><u>Async await with for…of</u></h2>



<pre class="wp-block-code line-numbers language-javascript"><code>async function printUser() {
    for(const user of users) {
        await randomWait();
        console.log(user);
    }
}</code></pre>



<pre class="wp-block-code language-json"><code>OUTPUT: 
{ id: 1, name: 'Alan' }
{ id: 2, name: 'Joel' }
{ id: 3, name: 'Triss' }</code></pre>



<p>This code defines two async functions: <code>randomWait()</code> and <code>printUser()</code>.</p>



<p><code>randomWait()</code> uses the <code>await</code> keyword to pause execution for a random amount of time, chosen using <code>Math.random()</code> and <code>setTimeout()</code>. Once the timer expires, the <code>Promise</code> resolves and the function returns.</p>



<p><code>printUser()</code> is an async function that iterates over an array called <code>users</code>. For each element of the array, it first calls <code>randomWait()</code> to simulate some time-consuming operation, then prints the element to the console using <code>console.log()</code>.</p>



<p>In the result, you can see that the data is printed in the sequential order in each of the three for loops.</p>



<h2 class="wp-block-heading">Caveats</h2>



<p>Using <code>await</code> inside a loop can potentially slow down the loop significantly, especially if the operations being awaited are time-consuming. In some cases, it might be more efficient to use <code>Promise.all()</code> to run multiple promises in parallel rather than awaiting them one at a time in a loop.</p>



<p></p>
<p>The post <a href="https://mithle.sh/javascript-for-loop-async-await/">How to use Async Await with For Loop in Javascript &#038; Typescript</a> appeared first on <a href="https://mithle.sh">Mithle.sh</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mithle.sh/javascript-for-loop-async-await/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1321</post-id>	</item>
	</channel>
</rss>
