<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by InterviewPro on Medium]]></title>
        <description><![CDATA[Stories by InterviewPro on Medium]]></description>
        <link>https://medium.com/@interviewpro?source=rss-3e61ee01be40------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jyCvWchyZUShRV_H8loqYg.png</url>
            <title>Stories by InterviewPro on Medium</title>
            <link>https://medium.com/@interviewpro?source=rss-3e61ee01be40------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:12:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@interviewpro/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My One-Minute Morning Habit]]></title>
            <link>https://medium.com/@interviewpro/my-one-minute-morning-habit-ef860cf4dd36?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef860cf4dd36</guid>
            <category><![CDATA[stay-hydrated]]></category>
            <category><![CDATA[one-minute-habits]]></category>
            <category><![CDATA[wellness-tips]]></category>
            <category><![CDATA[healthy-habits]]></category>
            <category><![CDATA[morning-routines]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Thu, 03 Jul 2025 19:28:41 GMT</pubDate>
            <atom:updated>2025-07-03T19:28:41.286Z</atom:updated>
            <content:encoded><![CDATA[<p>One Minute. Big Shift.</p><p>Not my words — <em>research proves it.</em><br>But I’ve been doing this simple thing for a while now: <strong>drinking a glass of lukewarm water right after I wake up ☀️.</strong></p><p>It takes less than a minute, and honestly? It’s made a difference I can feel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/0*qoc8OXQscDXUSVpO.jpeg" /></figure><h3>💧 What I’ve Noticed:</h3><ul><li><strong>My digestion feels smoother</strong> — less bloating, less heaviness after meals.</li><li><strong>I feel more awake and clear-headed</strong> in the morning. It’s like my body switches on faster.</li><li><strong>My skin’s been clearer</strong> and looks more hydrated — maybe because I’m starting the day with hydration, not caffeine.</li><li><strong>It actually calms me down.</strong> The warmth is grounding. It’s weirdly comforting, especially on stressful days.</li><li>And yes, even my <strong>morning energy feels more steady.</strong></li></ul><p>Apparently, lukewarm water helps with metabolism, circulation, and even detox. But honestly, I didn’t start it for the science — I just wanted something simple and healthy to begin the day. Now it’s non-negotiable.</p><p>No complicated routine. No expensive products. Just a warm glass of water, and your body says thank you.</p><p>Try it for a week — you’ll feel the difference. 💛</p><p>Follow for more!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef860cf4dd36" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[*ngFor Directive in Angular]]></title>
            <link>https://medium.com/@interviewpro/ngfor-directive-in-angular-cb9934786820?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb9934786820</guid>
            <category><![CDATA[loop]]></category>
            <category><![CDATA[ngfor]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[frontend-development]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Sat, 14 Jun 2025 09:18:14 GMT</pubDate>
            <atom:updated>2025-06-14T09:26:45.821Z</atom:updated>
            <content:encoded><![CDATA[<p>When it comes to rendering lists in Angular, *ngFor is the directive developers reach for most often. It allows looping through arrays or collections in templates and renders elements dynamically.</p><p>Let’s take a closer look at what *ngFordoes, how it works under the hood, and how you can use it effectively with hands-on examples.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fNGpoNjueTMrHAS54VtfRQ.png" /></figure><p>If you are looking for a video explanation, feel free to watch the <a href="https://youtu.be/qqTyrppkCgg?si=Lvj1P3xeviUBMvNe">video </a>I posted on my channel <a href="https://www.youtube.com/c/InterviewPro">InterviewPro</a>.</p><h3>What is *ngFor?</h3><p>The *ngFor directive in Angular is used to repeat a section of the HTML template for each item in a list. It&#39;s similar to a for loop in JavaScript, but used directly in your HTML templates.</p><p><strong>Syntax:</strong></p><pre>*ngFor=&quot;let item of items&quot;</pre><h3><strong>Examples</strong>:</h3><p>Let’s say you have a component with the following array:</p><pre><br>export class PlaylistComponent {<br>  playlists: string[] = [&#39;Angular&#39;, &#39;React&#39;, &#39;JavaScript&#39;, &#39;Python&#39;];<br>}</pre><p>In your template, use ngFor to loop through each playlist and display it on the UI. For each item in the playlists array, Angular creates a new &lt;li&gt; element and assigns the current item to the playlist variable.</p><p>{{playlist}} interpolates the string and displays it inside the list item.</p><pre>&lt;!-- app.component.html --&gt;<br>&lt;ul&gt;<br>  &lt;li *ngFor=&quot;let playlist of playlists&quot;&gt;{{ playlist }}&lt;/li&gt;<br>&lt;/ul&gt;<br><br><br>&lt;!--Looks like this in the browser  --&gt;<br>&lt;ul&gt;<br>  &lt;li&gt;Angular&lt;/li&gt;<br>  &lt;li&gt;React&lt;/li&gt;<br>  &lt;li&gt;JavaScript&lt;/li&gt;<br>  &lt;li&gt;Python&lt;/li&gt;<br>&lt;/ul&gt;</pre><p><strong>Using </strong><strong>index:</strong></p><p>Sometimes, you want to display the position of each item in a list. Angular provides a built-in local variable index for this.</p><pre>&lt;ul&gt;<br>  &lt;li *ngFor=&quot;let playlist of playlists; let i = index&quot;&gt;<br>    {{ i + 1 }}. {{ playlist}}<br>  &lt;/li&gt;<br>&lt;/ul&gt;<br><br>&lt;!-- Output --&gt;<br>1. Angular<br>2. React<br>3. JavaScript<br>4. Python </pre><p>let i = index stores the index (starting from 0) of the current item. We add 1 to display it as a human-readable position (1, 2, 3 instead of 0, 1, 2)</p><p><strong>With </strong><strong>objects:</strong></p><p>You’re not limited to strings — *ngFor works with array of objects too.</p><pre>playlists = [<br>  { id: 1, name: &#39;Angular&#39; },<br>  { id: 2, name: &#39;React&#39; },<br>  { id: 3, name: &#39;JavaScript&#39; },<br>  { id: 4, name: &#39;Python&#39; }<br>];</pre><p>You can access the properties like:</p><pre>&lt;ul&gt;<br>  &lt;li *ngFor=&quot;let playlist of playlists&quot;&gt;<br>    {{ playlist.id }} - {{ playlist.name }}<br>  &lt;/li&gt;<br>&lt;/ul&gt;<br><br>&lt;!-- Output --&gt;<br>1 - Angular<br>2 - React<br>3 - JavaScript<br>4 - Python</pre><ul><li>Each object in the playlists array has an id and name property.</li><li>Inside the loop, you access them using dot notation: playlist.id, playlist.name.</li></ul><p><strong>Local Variables:</strong></p><p>Besides index, Angular provides several other local variables in *ngFor:</p><ul><li>index: the current loop index</li><li>first: true if the item is the first in the iteration</li><li>last: true if the item is the last</li><li>even: true if the index is even</li><li>odd: true if the index is odd</li></ul><pre>&lt;ul&gt;<br>  &lt;li *ngFor=&quot;let playlist of playlists; let isOdd = odd&quot;&gt;<br>    &lt;span [style.color]=&quot;isOdd ? &#39;blue&#39; : &#39;black&#39;&quot;&gt;<br>      {{ playlist.name }}<br>    &lt;/span&gt;<br>  &lt;/li&gt;<br>&lt;/ul&gt;</pre><p><strong>Track items:</strong></p><p>For performance, especially with large or dynamic lists, Angular allows you to use trackBy to identify items uniquely. This prevents Angular from re-rendering the entire list when only one item changes.</p><pre>&lt;li *ngFor=&quot;let playlist of playlists; trackBy: trackByUserId&quot;&gt;<br>  {{ playlist.name }}<br>&lt;/li&gt;</pre><p>In your component:</p><pre>trackByUserId(index: number, playlist: any): number {<br>  return playlist.id;<br>}</pre><ul><li>By default, Angular tracks items by object identity.</li><li>If an object reference changes (even if the data is the same), Angular re-renders it.</li><li>With trackBy, Angular uses a unique key (playlist.id) to keep track of items more efficiently.</li></ul><p>Use trackBy when,</p><ul><li>You’re loading large lists.</li><li>Items may be updated frequently.</li><li>You want to minimize unnecessary DOM manipulation.</li></ul><h3>Final Thoughts:</h3><p>The *ngFor directive is essential for any Angular developer. Whether you&#39;re rendering a simple list or working with complex objects and performance considerations, mastering *ngFor gives you powerful control over dynamic data in the UI.</p><h3>Key Takeaways:</h3><ul><li>Use *ngFor to iterate over arrays or lists.</li><li>Take advantage of local variables (index, first, last, even, odd).</li><li>Use trackBy for performance on large or frequently updated lists.</li></ul><p>If you found this article helpful, don’t forget to follow<strong> </strong><a href="https://medium.com/@interviewpro">InterviewPro </a>for more content on JavaScript tips and best practices!</p><p>Show your support on my <a href="https://www.youtube.com/@InterviewPro">YouTube channel</a> too 😁.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb9934786820" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[delete operator in JavaScript]]></title>
            <link>https://medium.com/@interviewpro/delete-operator-in-javascript-d1028f596b97?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1028f596b97</guid>
            <category><![CDATA[delete]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[interviewpro]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Mon, 24 Mar 2025 14:47:29 GMT</pubDate>
            <atom:updated>2025-03-24T14:47:29.466Z</atom:updated>
            <content:encoded><![CDATA[<p>In JavaScript, removing properties from objects and elements from arrays is a common task. The delete operator is a popular tool for this, but it&#39;s important to understand how it works and when to use it to avoid common mistakes and performance problems.</p><p>This article will explain what the delete operator does, how it works, when to use it, and some best practices for efficient and reliable code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*WoujaPijHjrTOCKEHjKYqQ.png" /></figure><h3>What is JavaScript delete operator?</h3><p>The delete operator is used to remove properties from objects and elements from arrays. When applied to an object, it removes the specified property, and when applied to an array, it deletes the element at the specified index.</p><p><strong>Syntax:</strong></p><pre>delete object.property;</pre><h3>How Does the delete Operator Work?</h3><h4>Deleting Object Properties</h4><p>The behavior of the delete operator varies slightly depending on whether you use it on an object or an array.</p><pre>let emp = { name: &quot;John&quot;, age: 30 };<br>// Removes the &#39;age&#39; property<br>delete emp.age;  <br>console.log(emp);  <br>// Output: { name: &quot;John&quot; }</pre><p>In this case, the age property is removed from the person object, and the object is left with only the name property.</p><h4>Deleting Array Elements</h4><p>When you use the delete operator on an array, the element at the specified index is removed. However, the array will not be re-indexed, and the deleted index will remain as undefined. This is important to note, as it can lead to unintended gaps in the array.</p><pre>let arr = [10, 20, 30, 40, 50 ];<br>delete arr[1];  // Deletes the element at index 1<br>console.log(arr);  // Output: [10, undefined, 30, 40, 50]</pre><p>While the element is technically deleted, the array still has the same length, and the index where the element was deleted remains undefined.</p><h3>Performance</h3><p>Although the delete operator is useful, it can have performance implications, especially when used repeatedly in loops or when working with large objects. Here&#39;s why:</p><ul><li>Deleting properties from objects can cause the internal structure of the object to change, which may lead to slower performance in some JavaScript engines.</li><li>Deleting elements from arrays can result in sparse arrays, which are less efficient in terms of iteration and indexing.</li></ul><p>For better performance, consider the following alternatives:</p><ul><li><strong>Avoid Deleting Array Elements</strong>: Instead of using delete on an array, use methods like splice() to remove elements, which maintains array indexing.</li></ul><pre>let arr = [10, 20, 30, 40, 50];<br>arr.splice(1, 1);  // Removes the element at index 1 and shifts the array<br>console.log(arr);  // Output: [10, 30, 40, 50]</pre><ul><li><strong>Set to </strong><strong>null or </strong><strong>undefined</strong>: Instead of deleting object properties, you can set them to null or undefined to clear their values without removing the property entirely.</li></ul><pre>let emp = { name: &quot;John&quot;, age: 25 };<br>emp.age = null;  // Clears the value of &#39;age&#39; but doesn&#39;t delete the property<br>console.log(emp);  // Output: { name: &quot;John&quot;, age: null }</pre><h3>Conclusion</h3><p>The JavaScript delete operator is a powerful tool for removing properties from objects and elements from arrays. Understanding how it works, its limitations, and the potential pitfalls can help you avoid common mistakes and write more efficient code. While the delete operator can be useful for memory management and cleaning up unnecessary data, always consider alternatives like splice() for arrays or setting properties to null when you don’t need to delete them completely.</p><p>By following best practices and considering performance, you can leverage the delete operator effectively in your JavaScript development projects.</p><p>If you found this article helpful, don’t forget to follow<strong> </strong><a href="https://medium.com/@interviewpro">InterviewPro </a>for more content on JavaScript tips and best practices!</p><p>Show your support on my <a href="https://www.youtube.com/@InterviewPro">YouTube channel</a> too 😁.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1028f596b97" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding *ngIf, *ngIf else, *ngIf then else, and ng-template in Angular]]></title>
            <link>https://medium.com/@interviewpro/understanding-ngif-ngif-else-ngif-then-else-and-ng-template-in-angular-8f4c2fe611e2?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f4c2fe611e2</guid>
            <category><![CDATA[structural-directive]]></category>
            <category><![CDATA[interviewpro]]></category>
            <category><![CDATA[angular-16]]></category>
            <category><![CDATA[ng-template]]></category>
            <category><![CDATA[ngif]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Sun, 16 Feb 2025 16:25:03 GMT</pubDate>
            <atom:updated>2025-02-16T16:30:40.513Z</atom:updated>
            <content:encoded><![CDATA[<p>In Angular, the *ngIf directive is an essential tool for managing the visibility of elements in the DOM based on specific conditions. As a structural directive, *ngIf allows you to add or remove elements from the DOM depending on whether a given condition is true or false. This article explains the functionality and usage of *ngIf, along with its variations such as *ngIf else, *ngIf then else, and ng-template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eUxPxQYg5jHF9XnE4g7F1g.png" /></figure><p>If you are looking for a video explanation, feel free to watch the <a href="https://youtu.be/0z50ctWWhA8?feature=shared">video</a> I posted on my channel <a href="https://www.youtube.com/c/InterviewPro">InterviewPro</a>.</p><h3>What is *ngIf?</h3><p>*ngIf is a structural directive that conditionally adds or removes an element from the DOM based on the evaluation of a boolean expression. If the expression evaluates to true, the associated DOM element is rendered. If it evaluates to false, the element is removed from the DOM entirely.</p><h4>Syntax:</h4><pre>&lt;div *ngIf=&quot;condition&quot;&gt;<br>  Content when the condition is evaluated to true.<br>&lt;/div&gt;</pre><h4>Example:</h4><pre>&lt;button *ngIf=&quot;isSubscribed&quot;&gt;Subscribed&lt;/button&gt;</pre><p>In this example:</p><ul><li>The button element will only appear if isSubscribed is true. If isSubscribed is false, the button will not be added to the DOM.</li></ul><h3>*ngIf else: Handling False Conditions</h3><p>Often, you need to handle both true and false conditions. To manage this, Angular provides the else keyword, which allows you to specify an alternate template when the condition evaluates to false.</p><h4>Syntax:</h4><pre>&lt;div *ngIf=&quot;condition; else elseContent&quot;&gt;<br>  Content when the condition is evaluated to true.<br>&lt;/div&gt;<br>&lt;ng-template #elseContent&gt;<br>  Content when the condition is evaluated to true.<br>&lt;/ng-template&gt;</pre><h4>Example:</h4><pre>&lt;div *ngIf=&quot;isSubscribed; else notSubscribed&quot;&gt;<br>  You are subscribed to InterviewPro!<br>&lt;/div&gt;<br><br>&lt;ng-template #notSubscribed&gt;<br>  &lt;div&gt;Hit the Subscribe button for more content.&lt;/div&gt;<br>&lt;/ng-template&gt;</pre><h4>Explanation:</h4><ul><li>In this example, if isSubscribed is true, the div with the message &quot;You are subscribed to InterviewPro!&quot; will be displayed.</li><li>If isSubscribed is false, the content inside the &lt;ng-template&gt; (i.e., &quot;Hit the Subscribe button for more content.&quot;) will be displayed instead.</li></ul><p>Now what is ng-template??</p><h3>What is ng-template?</h3><ul><li>ng-template is an Angular directive that allows you to define an HTML template that is not rendered immediately, but rather when the condition specified in the *ngIf directive is met.</li><li>Templates in Angular are not rendered until explicitly requested by directives like *ngIf, *ngFor, or other structural directives.</li></ul><p>In the example above, the #notSubscribed template is not part of the DOM unless the isSubscribed condition is false.</p><h3>*ngIf then else: Using then for True Condition and else for False</h3><p>Angular allows you to further refine the *ngIf directive by using both then and else keywords, making your code more readable and organized. The then block is used to define content for the true case, and the else block for the false case.</p><p><strong>Syntax:</strong></p><pre>&lt;div *ngIf=&quot;condition; then thenTemplate else elseTemplate&quot;&gt;&lt;/div&gt;<br>&lt;ng-template #thenTemplate&gt;<br>  Content when the condition is evaluated to true.<br>&lt;/ng-template&gt;<br>&lt;ng-template #elseTemplate&gt;<br>  Content when the condition is evaluated to true.<br>&lt;/ng-template&gt;</pre><p><strong>Example:</strong></p><pre>&lt;div *ngIf=&quot;isSubscribed; then subscribedTmp else notSubscribedTmp&quot;&gt;&lt;/div&gt;<br><br>&lt;ng-template #subscribedTmp&gt;<br>  &lt;div&gt;You are subscribed to InterviewPro!&lt;/div&gt;<br>&lt;/ng-template&gt;<br><br>&lt;ng-template #notSubscribedTmp&gt;<br>  &lt;div&gt;Hit the Subscribe button for more content.&lt;/div&gt;<br>&lt;/ng-template&gt;</pre><h4>Explanation:</h4><ul><li>The *ngIf directive evaluates isSubscribed.</li><li>If isSubscribed is true, the content inside the subscribedTmp will be displayed.</li><li>If isSubscribed is false, the content inside the notSubscribedTmp will be displayed.</li></ul><h3><strong>Points to remember:</strong></h3><p><strong>Avoid Complex Logic in the Template</strong>: It’s good practice to keep the conditions in *ngIf simple. If you have complex logic, consider using a method in your component that returns the boolean result.</p><pre>&lt;div *ngIf=&quot;isUserSubscribed()&quot;&gt;<br>  You are subscribed to InterviewPro!<br>&lt;/div&gt;</pre><p><strong>Performance</strong>: While *ngIf can be very helpful, frequent toggling of DOM elements might cause performance issues in certain scenarios, especially if the DOM structure is complex. In such cases, you may want to consider other solutions, such as lazy loading or using ngIf with ng-template for more fine-grained control over rendering.</p><h3>Conclusion</h3><p>The *ngIf directive, along with its variations (*ngIf else, *ngIf then else), is essential for creating dynamic UIs in Angular. It allows you to conditionally display or hide content based on boolean expressions. The addition of ng-template provides flexibility, as it allows you to define templates that are not rendered immediately, improving performance and readability.</p><p>By mastering *ngIf, you can easily manage complex DOM structures in response to the application state, making your Angular applications more dynamic and interactive.</p><p>I hope this article has provided you with a clearer understanding of the *ngIf structural directive and its variations in Angular. Happy coding!</p><p>Follow <a href="https://medium.com/@interviewpro">InterviewPro </a>for more updates. Show your support on my <a href="https://www.youtube.com/@InterviewPro">YouTube channel</a> too 😁.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f4c2fe611e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Unary Plus (+) Operator in JavaScript]]></title>
            <link>https://medium.com/@interviewpro/the-unary-plus-operator-in-javascript-59bd5d5b38e9?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/59bd5d5b38e9</guid>
            <category><![CDATA[unary-operators]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[interviewpro]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[frontend-development]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Wed, 29 Jan 2025 04:41:50 GMT</pubDate>
            <atom:updated>2025-01-29T04:41:50.678Z</atom:updated>
            <content:encoded><![CDATA[<p>The unary plus (+) operator is one of the simplest yet highly versatile operators in JavaScript. As a unary operator, it works with a single operand, with its primary function being to convert the operand into a number. While it might appear straightforward, the unary plus operator is fundamental to type conversion and numeric operations in JavaScript.</p><p>In this article, we’ll explore the unary plus operator in detail, including its behavior, use cases, and examples to help you understand how it works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*40D9IEC5JWM9vRAkv38yNQ.png" /></figure><h3>What Is the Unary Plus (+) Operator?</h3><p>The unary plus (+) operator is used to transform its operand into a numeric value. If the operand is already a number, it remains unchanged. However, for strings, booleans, or other data types, the operator attempts to convert them into a number.</p><h4>Syntax:</h4><pre>+operand</pre><p>Here, operand can be any value, such as a number, string, boolean, or even an object.</p><h3>How it works?</h3><p>The unary plus (+) operator adheres to the following rules when converting an operand:</p><ul><li>If the operand is already a number, it simply returns the number unchanged.</li><li>For string operands, it attempts to parse them into numbers. If the string contains any non-numeric characters (aside from leading or trailing whitespace), it returns NaN (Not-a-Number).</li><li>Boolean values are converted to numbers, with true becoming 1 and false becoming 0.</li><li>When the operand is null, it returns 0.</li><li>If the operand is undefined, it results in NaN.</li><li>For objects, the operator invokes the valueOf() or toString() method to obtain a primitive value, then tries to convert that value into a number.</li></ul><h3>Examples</h3><p>Let’s look at some practical examples to understand how the unary plus operator behaves in different scenarios.</p><p><strong>🟡Converting Strings to Numbers: </strong>The unary plus operator is commonly used to convert strings to numbers.</p><pre>let str1 = &quot;51&quot;;<br>let num1 = +str1; // Converts &quot;51&quot; to 42<br><br>console.log(num1); // Output: 51<br>console.log(typeof num1); // Output: &quot;number&quot;<br><br>let str2 = &quot;3.14&quot;;<br>let num2 = +str2; // Converts &quot;3.14&quot; to 3.14<br><br>console.log(num2); // Output: 3.14<br><br>let str3 = &quot;InterviewPro&quot;;<br>let num3 = +str3; // Cannot convert &quot;InterviewPro&quot; to a number<br><br>console.log(num3); // Output: NaN</pre><p><strong>🟡Converting Booleans to Numbers: </strong>Converts true to 1 and false to 0.</p><pre>console.log(+true); // Output: 1<br>console.log(+false); // Output: 0</pre><p><strong>🟡Converting </strong><strong>null and </strong><strong>undefined: </strong>converts null to 0 and undefined to NaN.</p><pre>console.log(+null); // Output: 0<br>console.log(+undefined); // Output: NaN</pre><p><strong>🟡Handling Whitespace in Strings: </strong>Ignores leading and trailing whitespace in strings and converts it to number.</p><pre>let str4 = &quot;   135   &quot;;<br>let num4 = +str4; // Converts &quot;   135   &quot; to 135<br><br>console.log(num4); // Output: 135<br><br>let str5 = &quot;   Hello   &quot;;<br>let num5 = +str5; // Cannot convert &quot;   Hello   &quot; to a number<br><br>console.log(num5); // Output: NaN</pre><p><strong>🟡Objects: </strong>When applied to an object, the unary plus operator calls the object’s valueOf() or toString() method to convert it to a primitive value, then attempts to convert that value to a number.</p><pre>let obj1 = { valueOf: () =&gt; 51};<br>console.log(+obj1); // Output: 51<br><br>let obj2 = { toString: () =&gt; &quot;3.14&quot; };<br>console.log(+obj2); // Output: 3.14<br><br>let obj3 = { name: &quot;InterviewPro&quot; };<br>console.log(+obj3); // Output: NaN (cannot convert to a number)</pre><h3>Points to remember</h3><ol><li>The unary plus operator (+) converts its operand to a number.</li><li>It works with strings, booleans, null, undefined, and objects.</li><li>If the operand cannot be converted to a number, it returns NaN.</li><li>It is a concise alternative to functions like Number() or parseFloat().</li></ol><h3>Conclusion</h3><p>The unary plus (+) operator is a straightforward yet effective tool in JavaScript for converting values into numbers. It offers a simple and efficient way to handle numerical conversions, whether you&#39;re processing user input, parsing data, or performing calculations.</p><p>Follow my articles at <a href="https://medium.com/@interviewpro">InterviewPro</a> to explore more insights and deepen your understanding of JavaScript.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59bd5d5b38e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Decoding (x++ + ++x) in JavaScript]]></title>
            <link>https://medium.com/@interviewpro/decoding-x-x-in-javascript-ba6618fd1494?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba6618fd1494</guid>
            <category><![CDATA[pre-increment]]></category>
            <category><![CDATA[js-interview-questions]]></category>
            <category><![CDATA[post-increment]]></category>
            <category><![CDATA[interviewpro]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Fri, 24 Jan 2025 16:22:10 GMT</pubDate>
            <atom:updated>2025-01-24T16:22:10.647Z</atom:updated>
            <content:encoded><![CDATA[<p>JavaScript can sometimes behave in unexpected ways, especially when it comes to increment operators (++). These operators are often tested in coding interviews because they can be tricky to understand.</p><p>A common question is about the difference between <strong>post-increment (</strong><strong>x++)</strong> and <strong>pre-increment (</strong><strong>++x)</strong>, and how they work when used together in the same expression. Let’s take a closer look at this concept with a simple example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*Q7YMiBd-V_4SL72XA160TQ.png" /></figure><pre>let x = 1;<br>let result = x++ + ++x;<br>console.log(result);</pre><p>This seemingly simple code snippet often leaves developers scratching their heads. Let’s break it down step-by-step.</p><h3>Solution</h3><p>Let’s start by understanding how the expression x++ + ++x works when x = 1.</p><p><strong>Step 1</strong> — <strong>Evaluate</strong> <strong>x++</strong>:</p><ul><li>x++ is a <strong>post-increment</strong>, meaning the current value of x (1) is used first, and then x is incremented.</li><li>After this step, the value of x++ is 1, but x is now incremented to 2.</li></ul><p><strong>Step 2 — Evaluate </strong><strong>++x:</strong></p><ul><li>++x is a <strong>pre-increment</strong>, meaning x is incremented first (from 2to 3), and then the updated value (3) is used.</li></ul><p><strong>Step 3 — Add Results:</strong></p><ul><li>The result of x++ is 1, and the result of ++x is 3. Adding these together gives: 4</li></ul><p><strong>Final Output:</strong><br>The value of result is 4, and the value of x after the entire expression is 3.</p><h3>Points to remember:</h3><p><strong>Post-Increment (</strong><strong>x++):</strong></p><ul><li>Returns the current value of x.</li><li>Increments x <strong>after</strong> returning the current value.</li></ul><p><strong>Pre-Increment (</strong><strong>++x):</strong></p><ul><li>Increments x <strong>before</strong> returning the updated value.</li></ul><p><strong>Order of Execution:</strong></p><p>The tricky part lies in understanding how JavaScript evaluates this expression:</p><ul><li>JavaScript evaluates expressions from <strong>left to right</strong>, but the values of variables are updated immediately when a pre- or post-increment operation is executed.</li></ul><h4>Timeline of Events for x++ + ++x:</h4><p>When x = 1:</p><ul><li><strong>x++:</strong> Uses 1(current value), then increments x to 2.</li><li><strong>++x:</strong> Increments x to 3(from 2), then uses the updated value 3.</li><li><strong>Final Addition:</strong> 1+3 = 4.</li></ul><h3>Conclusion</h3><p>Increment operators in JavaScript can be tricky, but once you understand the difference between pre-increment (++x) and post-increment (x++), it becomes much easier to solve these types of questions.</p><p>In an interview, tackle such problems step by step. Follow the changes in the variable’s value carefully and see how the operators affect it.</p><p>These questions not only test your understanding of JavaScript but also your ability to think logically and debug code.</p><h3>Practice Questions</h3><p>Here are some advanced questions to test your understanding of increment operators and expression evaluation in JavaScript:</p><pre>let x = 3;<br>let result = x++ + ++x + x++;<br>console.log(result, x);</pre><pre>let a = 1;<br>let b = ++a + a++ + ++a + a++;<br>console.log(b, a);</pre><pre>let x = 1;<br>let y = x++ &amp;&amp; ++x || x++;<br>console.log(y, x);</pre><p>Test your knowledge by solving the practice questions above. Share your answers and thoughts in the comments section — I’d love to hear how you approach these tricky problems!</p><p>If you enjoyed this article and want to explore more JavaScript concepts and interview tips, don’t forget to follow <a href="https://medium.com/@interviewpro">InterviewPro</a> for future content. 🙂</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba6618fd1494" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding !!{} in JavaScript: Why It Returns True]]></title>
            <link>https://medium.com/@interviewpro/understanding-in-javascript-why-it-returns-true-459d785baf4b?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/459d785baf4b</guid>
            <category><![CDATA[js]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[interview-preparation]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[interviewpro]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Sun, 19 Jan 2025 17:34:39 GMT</pubDate>
            <atom:updated>2025-01-19T17:34:39.358Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding !!{} in JavaScript: Why It Returns True</h3><p>JavaScript has many surprising behaviors that can confuse developers. One example is the expression console.log(!!{}), which prints true. Let’s go through it step by step to understand why this happens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*5_pBd6E1g21g_nvB1MrLgg.png" /></figure><h3>Understanding !!</h3><p>The !! is a double logical NOT operator. It is commonly used to convert a value into its boolean equivalent. Here’s how it works:</p><p>The first ! (logical NOT) converts the value into a boolean and negates it:</p><ul><li>For <strong>truthy</strong> values, it becomes false.</li><li>For <strong>falsy</strong> values, it becomes true.</li></ul><p>The second ! negates the result again, reverting it to its original truthy or falsy boolean representation.</p><p>This double negation effectively coerces any value into a boolean (true or false).</p><h3>Understanding {} (Empty Object)</h3><p>In JavaScript, an empty object {} is considered a <strong>truthy value</strong>. Truthy values are those that are interpreted as true in a boolean context. Some common examples of truthy values include:</p><ul><li>Non-zero numbers (e.g., 1, -5)</li><li>Non-empty strings (e.g., &quot;InterviewPro&quot;)</li><li>Arrays (e.g., [])</li><li>Objects (e.g., {})</li></ul><h3>Step-by-Step Execution</h3><p>To understand the output of console.log(!!{}), let’s analyze it step by step:</p><ol><li><strong>!{}: </strong>The first ! converts the truthy value {} into false.</li><li><strong>!!{}: </strong>The second ! negates false back into true.</li></ol><h3>Final Result</h3><p>Since the boolean equivalent of an empty object {} is true, the expression console.log(!!{}) outputs true.</p><h3>Why Is This Important?</h3><p>The !! pattern is widely used in JavaScript to ensure that a value is explicitly converted to a boolean. This can be particularly useful when dealing with conditions or ensuring that a value is either true or false without ambiguity. By using !!, developers can write clearer and more predictable code.</p><h3>Conclusion</h3><p>The behavior of console.log(!!{}) demonstrates JavaScript’s approach to truthy and falsy values. Understanding these nuances can help developers write more robust and readable code. Whether you’re checking if an object exists or simplifying conditions, the double logical NOT operator is a handy tool in your JavaScript toolkit.</p><p>Follow <a href="https://medium.com/@interviewpro">InterviewPro</a> for more JavaScript interview questions and in-depth explanations to help you prepare and learn!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=459d785baf4b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Directives in a Nutshell]]></title>
            <link>https://medium.com/@interviewpro/directives-in-angular-86276f3dbfae?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/86276f3dbfae</guid>
            <category><![CDATA[attribute-directive]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[component-directives]]></category>
            <category><![CDATA[structural-directive]]></category>
            <category><![CDATA[interviewpro]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Sat, 11 Jan 2025 05:12:00 GMT</pubDate>
            <atom:updated>2025-01-11T05:19:35.893Z</atom:updated>
            <content:encoded><![CDATA[<p>Angular directives are a fundamental feature of Angular that enable developers to create dynamic, reusable, and interactive components. They enhance the capabilities of HTML and facilitate the clean and efficient implementation of complex behaviors by interacting with the DOM.</p><p>This article will explain what Angular directives are and outline their different types.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bLUkkMTY5dWWp7NrpswXzw.png" /></figure><p>If you are looking for a video explanation, feel free to watch the <a href="https://youtu.be/NDraJFkG6wU?si=OUekJqIVRK0chwbd">video</a> I posted on my channel <a href="https://www.youtube.com/c/InterviewPro">InterviewPro</a>.</p><h3>What Are Angular Directives?</h3><p>In Angular, directives are special markers placed on elements, such as attributes, classes, or comments. They instruct Angular to apply a specific behavior to the DOM element or to transform it in a certain way. Directives are used to modify the appearance, behavior, or layout of DOM elements.</p><p>Angular provides three types of directives:</p><ol><li>Component Directives</li><li>Attribute Directives</li><li>Structural Directives</li></ol><h3><strong>Component Directives</strong></h3><p>A <strong>component directive</strong> in Angular is a type of directive that comes with a template. It defines a reusable piece of your application, like a button, form, or an entire page. Essentially, it’s what you commonly think of as a component in Angular.</p><p>It controls a part of the user interface (UI) by using the @Component decorator and includes HTML, CSS, and JavaScript/TypeScript logic.</p><pre>@Component({<br>  selector: &#39;app-root&#39;,<br>  template: &#39;&lt;h1&gt;Hello, World!&lt;/h1&gt;&#39;,<br>  styles: [&#39;h1 { color: blue; font-family: Arial, sans-serif; }&#39;]<br>})<br>export class AppComponent {}</pre><p>Another example using templateUrl and styleUrls:</p><pre>@Component({<br>  selector: &#39;app-root&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.css&#39;]<br>})<br>export class AppComponent {}</pre><p>Template file (app.component.html):</p><pre>&lt;h2&gt;Welcome to Angular Components!&lt;/h2&gt;</pre><p>Styles file (app.component.css):</p><pre>h2 {<br>  color: blue;<br>  text-align: center;<br>}</pre><h3>Attribute Directives</h3><p>An <strong>attribute directive</strong> in Angular is a type of directive that modifies the appearance or behavior of a DOM element or component by changing its attributes or styles. It acts like a decorator for elements, allowing you to dynamically update their properties or apply effects based on logic in your code.</p><p>Here is a list of some common <strong>built-in attribute directives</strong> in Angular, which can modify the behavior or appearance of DOM elements:</p><h4>Built-in Attribute Directives</h4><ol><li><a href="https://medium.com/@interviewpro/class-binding-ngclass-in-angular-431fa989207d"><strong>NgClass</strong></a><strong>: </strong>Dynamically adds or removes CSS classes on an element.</li><li><a href="https://medium.com/@interviewpro/style-binding-ngstyle-in-angular-8df0bfd169d7"><strong>NgStyle</strong></a><strong>: </strong>Dynamically applies inline styles to an element.</li><li><strong>NgModel: </strong>Two-way data binding for form inputs.</li></ol><p>We have previously discussed NgClass and NgStyle built-in attributes in our earlier article. Feel free to revisit them for more details.</p><h4>NgModel</h4><p>ngModel is a directive in Angular that facilitates two-way data binding. It allows you to bind an HTML element (like an input field) to a variable in your component, automatically updating the variable when the user changes the input and vice versa.</p><pre>&lt;input [(ngModel)]=&quot;username&quot;&gt;</pre><p>In this example, changes to the input field username automatically update the name property in the component.</p><h3><strong>Structural Directives</strong></h3><p><strong>Structural directives </strong>in Angular are used to modify the structure of the DOM (Document Object Model) by adding, removing, or manipulating elements. They change how the view appears based on certain conditions.</p><p>The most common structural directives are:</p><p><strong>*ngIf</strong>: Adds or removes an element from the DOM depending on whether a condition is true or false.</p><pre>&lt;button *ngIf=&quot;isVisible&quot;&gt;Display button if isVisible is true&lt;/button&gt;</pre><p><strong>*ngFor</strong>: Loops through a collection (like an array) and creates an element for each item.</p><pre>&lt;ul&gt;<br>  &lt;li *ngFor=&quot;let playlist of playlists&quot;&gt;{{ playlist }}&lt;/li&gt;<br>&lt;/ul&gt;</pre><p><strong>*ngSwitch</strong>: Conditionally displays one of many elements based on a given expression.</p><pre>&lt;div [ngSwitch]=&quot;selectedPlaylist&quot;&gt;<br>  &lt;div *ngSwitchCase=&quot;&#39;react&#39;&quot;&gt;React JS&lt;/div&gt;<br>  &lt;div *ngSwitchCase=&quot;&#39;angular&#39;&quot;&gt;Angular&lt;/div&gt;<br>  &lt;div *ngSwitchDefault&gt;All playlists&lt;/div&gt;<br>&lt;/div&gt;</pre><p>In simple terms, structural directives help you control what parts of your UI are displayed or hidden based on conditions, loops, or switches.</p><h3>Conclusion</h3><p>Angular directives are essential for creating dynamic and interactive applications. By comprehending the various types of directives and learning how to build custom ones, you can greatly improve the functionality and maintainability of your Angular projects.</p><p>I hope this article has helped you gain a better understanding of different types of directives in Angular. Happy coding!</p><p>Follow <a href="https://medium.com/@interviewpro">InterviewPro </a>for more updates. Show your support on my <a href="https://www.youtube.com/@InterviewPro">YouTube channel</a> too 😁.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86276f3dbfae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Style binding [ngStyle] in Angular]]></title>
            <link>https://medium.com/@interviewpro/style-binding-ngstyle-in-angular-8df0bfd169d7?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/8df0bfd169d7</guid>
            <category><![CDATA[interviewpro]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[angular-16]]></category>
            <category><![CDATA[style-binding]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Sat, 04 Jan 2025 05:38:46 GMT</pubDate>
            <atom:updated>2025-01-04T05:53:40.119Z</atom:updated>
            <content:encoded><![CDATA[<p>The Angular framework includes many features that enhance the interactivity and user-friendliness of your website. One of these features is <a href="https://medium.com/@interviewpro/class-binding-ngclass-in-angular-431fa989207d">class binding</a>, which I discussed in my previous article. <em>(I hope you had a chance to check it out! )</em></p><p>In this article, I will introduce you to another fantastic feature: <strong>style binding</strong>. This feature allows you to control inline styles directly in your templates.</p><p>While we previously explored class binding, which enables the dynamic application of CSS classes, today we will focus on style binding. This important feature offers great flexibility in customizing inline styles within your templates.</p><p>For a detailed video explanation of this concept, check out my video on style binding in Angular with practical examples.</p><p><a href="https://youtu.be/RH5LOyQl6Yc?si=qymU_9R5ZyXOa3LO">https://youtu.be/RH5LOyQl6Yc?si=qymU_9R5ZyXOa3LO</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LWUE1lZE7Tskxyuz32krxw.png" /></figure><h3>What is Style Binding?</h3><p>Style binding in Angular is a powerful tool that allows you to dynamically adjust the inline styles of HTML elements based on the data in your component. This feature enables you to apply one or more styles conditionally, making it easy to create visually dynamic and responsive designs.</p><p>There are <strong>3</strong> methods to implement style binding, similar to class binding:</p><ol><li><strong>style.&lt;style-name&gt;:</strong> This method binds an element’s style property directly using the style keyword.</li><li><strong>[style]</strong>: This method allows you to set multiple styles at once by binding an object that contains style properties and their corresponding values.</li><li><strong>ngStyle directive</strong>: The <strong>ngStyle</strong> directive enables binding of multiple inline styles in a flexible manner, similar to object binding, but with additional features provided by Angular.</li></ol><p>Let me revisit the example I mentioned in the article about class binding.</p><blockquote>We have a feedback form consisting of two input fields: one for the username and another for the feedback. There is also a submit button for the form. We want the button to appear grayed out when either of the input fields is empty. Once both fields contain data, the button should change to blue.</blockquote><h4>style.&lt;style-name&gt;</h4><pre>&lt;button [style.background-color]=&quot;(username &amp;&amp; feedback) ? &#39;blue&#39; : &#39;grey&#39;&quot;&gt;<br>  Submit<br>&lt;/button&gt;</pre><ul><li>The background-color style is conditionally set based on whether both username and feedback have values.</li><li>If both username and feedback are not empty, the button&#39;s background color will be <strong>blue</strong>.</li><li>If either username or feedback is empty, the background color will be <strong>grey</strong>.</li></ul><p>To apply multiple styles</p><pre>&lt;button <br>  [style.background-color]=&quot;(username &amp;&amp; feedback) ? &#39;blue&#39; : &#39;red&#39;&quot;<br>  [style.color]=&quot;(username &amp;&amp; feedback) ? &#39;white&#39; : &#39;black&#39;&quot;<br>  [style.padding]=&quot;&#39;10px&#39;&quot;<br>  [style.font-size]=&quot;&#39;16px&#39;&quot;&gt;<br>  Submit<br>&lt;/button&gt;</pre><h4>[style]</h4><pre>&lt;button [style]=&quot;{ &#39;background-color&#39;: (username &amp;&amp; feedback) ? &#39;blue&#39; : &#39;grey&#39; }&quot;&gt;<br>  Submit<br>&lt;/button&gt;</pre><ul><li>The [style] directive binds to a style object, which contains the background-color property.</li><li>The background color is set conditionally based on the values of username and feedback.</li><li>When both username and feedback have values, the background color will be <strong>blue</strong>. Otherwise, it will be <strong>grey</strong>.</li></ul><p>Multiple styles:</p><pre>&lt;button [style]=&quot;{<br>  &#39;background-color&#39;: (username &amp;&amp; feedback) ? &#39;blue&#39; : &#39;red&#39;,<br>  &#39;color&#39;: (username &amp;&amp; feedback) ? &#39;white&#39; : &#39;black&#39;,<br>  &#39;padding&#39;: &#39;10px&#39;,<br>  &#39;font-size&#39;: &#39;16px&#39;<br>}&quot;&gt;<br>  Submit<br>&lt;/button&gt;</pre><h4>ngStyle Directive</h4><pre>&lt;button [ngStyle]=&quot;{ &#39;background-color&#39;: (username &amp;&amp; feedback) ? &#39;blue&#39; : &#39;grey&#39; }&quot;&gt;<br>  Submit<br>&lt;/button&gt;</pre><ul><li>ngStyle allows for binding a style object just like the [style] directive, but it&#39;s more powerful because it can be used to apply multiple styles at once.</li><li>The background-color is set based on whether username and feedback are both non-empty.</li><li>If both variables are filled, the background color will be <strong>blue</strong>. If either is empty, the button will have a <strong>grey</strong> background.</li></ul><p>Multiple styles:</p><pre>&lt;button [ngStyle]=&quot;{<br>  &#39;background-color&#39;: (username &amp;&amp; feedback) ? &#39;blue&#39; : &#39;red&#39;,<br>  &#39;color&#39;: (username &amp;&amp; feedback) ? &#39;white&#39; : &#39;black&#39;,<br>  &#39;padding&#39;: &#39;10px&#39;,<br>  &#39;font-size&#39;: &#39;16px&#39;<br>}&quot;&gt;<br>  Submit<br>&lt;/button&gt;</pre><h4>[style] and [ngStyle] may seem similar, but what’s the difference between them?</h4><p>Both [ngStyle] and [style] with object binding can achieve the same result when applying dynamic styles, but they differ in usage, performance, and flexibility. Here’s why [ngStyle] is generally the better choice when applying multiple styles.</p><p><strong>[ngStyle]</strong>:</p><ul><li>It is built to manage multiple dynamic styles and is the preferred method when applying several styles conditionally with complex logic.</li><li>Angular is better optimized for style updates with [ngStyle], especially when updating multiple styles simultaneously. It efficiently tracks and updates the styles in a single change detection cycle, ensuring better performance when managing a group of dynamic styles.</li></ul><p><strong>[style]</strong>:</p><ul><li>When you use [style] with an object, Angular isn&#39;t as optimized as with [ngStyle] for updating multiple styles. Although it works, it needs to evaluate each style individually, which can lead to additional overhead if the styles change often.</li></ul><h3>Conclusion</h3><p>Style binding in Angular provides a powerful way to dynamically apply styles to elements based on conditions or data changes. There are several methods for style binding:</p><ul><li><strong>[style.property]</strong> — individual style properties</li><li><strong>[ngStyle]</strong> directive — multiple styles at once with complex conditions or updates.</li><li><strong>[style] with object binding</strong> — group styles in a single binding. Not as optimized as [ngStyle].</li></ul><p>To choose the right method, consider the complexity of your styles and performance needs.</p><p>If you found this helpful, make sure to follow <a href="https://medium.com/@interviewpro">InterviewPro</a> for more tips and check out my <a href="https://www.youtube.com/@InterviewPro">YouTube channel</a> for in-depth tutorials and examples. 🙏</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8df0bfd169d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Class Binding [ngClass] in Angular]]></title>
            <link>https://medium.com/@interviewpro/class-binding-ngclass-in-angular-431fa989207d?source=rss-3e61ee01be40------2</link>
            <guid isPermaLink="false">https://medium.com/p/431fa989207d</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[interviewpro]]></category>
            <category><![CDATA[class-binding]]></category>
            <dc:creator><![CDATA[InterviewPro]]></dc:creator>
            <pubDate>Sat, 28 Dec 2024 14:45:37 GMT</pubDate>
            <atom:updated>2024-12-28T14:47:19.500Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Ever wondered how websites change styles instantly based on user actions?</strong> For example, a button turns green when clicked, or a menu item is highlighted when you select it. In Angular, you can do this easily with <strong>class binding</strong>!</p><p>Class binding lets you <strong>add or remove CSS classes dynamically</strong> based on what’s happening in your app. This is super useful for making your app look more interactive and responsive to user actions or data changes.</p><p>In this article, we’ll look at <strong>what class binding is</strong>, why it’s useful, and how to use it in your Angular projects with <strong>simple, real-world examples</strong>.</p><p>For a detailed video explanation of this concept, check out my video on class binding in Angular with practical examples by clicking the link below.</p><p><a href="https://youtu.be/wYd0nvfNAvY?si=CfPYxs6KYG2XEYOI">https://youtu.be/wYd0nvfNAvY?si=CfPYxs6KYG2XEYOI</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xhxRoXxHiMuf7qHFIBdgpQ.png" /></figure><h3>What is Class Binding?</h3><p>Class binding allows you to connect an element’s class attribute to a property or expression in your component. This means the element can automatically add or remove CSS classes based on the logic you set in your code.</p><p>There are 3 ways to implement class binding in Angular:</p><ul><li><strong>class.&lt;class-name&gt;</strong></li><li><strong>[class]</strong></li><li><strong>ngClass directive</strong></li></ul><p>Imagine a feedback form with two input fields: one for the usernameand another for feedback. There’s also a submitbutton for the form. We want the button to appear greyed outwhen either of the input fields is empty. Once both fields have data, the button should turn blue. Let’s explore how to achieve this using class binding.</p><h4>class.&lt;class-name&gt;</h4><ul><li>This approach binds <strong>specific classes</strong> (e.g., primary or secondary) directly to a condition.</li><li>The syntax [class.&lt;class-name&gt;] ensures that the specified class (&lt;class-name&gt;) is added only when the condition evaluates to true.</li></ul><pre>&lt;button <br>  [class.primary]=&quot;username.length &gt; 0 &amp;&amp; feedback.length &gt; 0&quot; <br>  [class.secondary]=&quot;username.length ==0 || feedback.length == 0&quot;<br>&gt;<br>  Submit<br>&lt;/button&gt;</pre><ul><li><strong>[class.primary]</strong>: Adds the primary class if both username and feedback have data.</li><li><strong>[class.secondary]</strong>: Adds the secondary class if either of the fields is empty.</li><li>Since only one condition can be true at a time, either primary or secondary is applied, but not both.</li></ul><h4>[class]</h4><ul><li>Binds the entire class attribute to an <strong>expression</strong>.</li><li>The expression determines which <strong>single class</strong> is applied at any given time.</li></ul><p>Let’s say the logic to validate the form is moved to a method isFormValid() in the TypeScript code.</p><pre>&lt;button [class]=&quot;isFormValid() ? &#39;primary&#39; : &#39;secondary&#39;&quot;&gt;<br>  Submit<br>&lt;/button&gt;</pre><ul><li>The expression isFormValid() ? &#39;primary&#39; : &#39;secondary&#39; dynamically evaluates to &#39;primary&#39; when the form is valid, and &#39;secondary&#39; when it is not.</li><li>The result is directly assigned to the button’s class attribute.</li></ul><h4>ngClass Directive</h4><ul><li>The ngClass directive allows you to dynamically apply <strong>multiple classes</strong> at once.</li></ul><p>You can pass:</p><ul><li>An <strong>object</strong> with keys as class names and values as conditions.</li><li>An <strong>array</strong> of class names.</li></ul><pre>&lt;button [ngClass]=&quot;{&#39;primary&#39;:username.length &gt; 0 &amp;&amp; feedback.length &gt; 0,<br>                    &#39;secondary&#39;:username.length == 0 || feedback.length == 0}&quot;&gt;<br>  Submit<br>&lt;/button&gt;</pre><h3>Conclusion:</h3><p>We covered the three primary methods of class binding:</p><ol><li><strong>class.&lt;class-name&gt;</strong> for toggling individual classes,</li><li><strong>[class]</strong> for directly assigning a single class,</li><li><strong>ngClass</strong> for managing multiple classes and more complex conditions.</li></ol><p>By leveraging these methods, you can build interactive and responsive Angular applications that react to user input and application state.</p><p>I hope this article has helped you understand how to use class binding effectively in your Angular projects. Happy coding!</p><p>Follow <a href="https://medium.com/@interviewpro">InterviewPro </a>for more updates. Show your support on my <a href="https://www.youtube.com/@InterviewPro">YouTube channel</a> too 😁.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=431fa989207d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>