<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kiran Kumar</title>
    <description>The latest articles on Forem by Kiran Kumar (@kiran_kumar).</description>
    <link>https://forem.com/kiran_kumar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2087891%2F79add923-f15d-4460-9ebf-7fcee0296a3b.png</url>
      <title>Forem: Kiran Kumar</title>
      <link>https://forem.com/kiran_kumar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kiran_kumar"/>
    <language>en</language>
    <item>
      <title>Understanding JavaScript Equality: A Deep Dive into Array and Object Comparisons</title>
      <dc:creator>Kiran Kumar</dc:creator>
      <pubDate>Wed, 25 Sep 2024 09:59:08 +0000</pubDate>
      <link>https://forem.com/kiran_kumar/understanding-javascript-equality-a-deep-dive-into-array-and-object-comparisons-37j3</link>
      <guid>https://forem.com/kiran_kumar/understanding-javascript-equality-a-deep-dive-into-array-and-object-comparisons-37j3</guid>
      <description>&lt;p&gt;JavaScript’s type coercion and equality comparison can sometimes lead to unexpected results, especially when dealing with arrays and objects. In this blog post, we’ll explore the nuances of these comparisons through three key examples. We’ll break down each case, provide detailed explanations, and include examples to solidify your understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. Comparing Two Arrays: [] == []&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt; &lt;br&gt;
In JavaScript, arrays are reference types. This means that each array is stored in a different location in memory. When you create two separate arrays, even if they are empty, they are treated as distinct objects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzgiwdsuidihutvvt8t8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzgiwdsuidihutvvt8t8w.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The two arrays (array1 and array2) occupy different memory locations, so when you compare them, JavaScript checks if they reference the same location. Since they don’t, the comparison evaluates to false.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxec4an11wds7f7vsztux.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxec4an11wds7f7vsztux.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Comparing an Array and a String: [] == ""
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;br&gt;
When comparing an array with a string, JavaScript performs type coercion, converting the array to a primitive value. An empty array converts to an empty string (“”).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8irlvthkb8ukecd5lbff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8irlvthkb8ukecd5lbff.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Type Coercion: The empty array [] is coerced to a primitive value, resulting in “”. Thus, the comparison evaluates to “” == “”, which is &lt;strong&gt;true&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frxk60wysa4otyesytuw8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frxk60wysa4otyesytuw8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Comparing an Array and an Object: [] == {}
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;br&gt;
When comparing an empty array and an empty object, the array gets converted to a primitive value (empty string), while the object remains a reference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhjy1t4iwij4kcfzzvmi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhjy1t4iwij4kcfzzvmi.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The empty array [] converts to “”, while the empty object {} remains as an object reference. Thus, the comparison effectively becomes “” == [object object], which evaluates to &lt;strong&gt;false&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;JavaScript’s equality comparisons can be confusing, particularly when dealing with arrays and objects. Understanding how type coercion works and the nature of references in JavaScript is crucial for avoiding unexpected behavior in your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Reference Types:&lt;/strong&gt; Arrays and objects are stored in memory as references. Two separate arrays or objects will not be equal, even if they have the same content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type Coercion:&lt;/strong&gt; JavaScript may convert types during comparison, leading to results that might not be intuitive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explicit Comparisons:&lt;/strong&gt; Using === is often preferred to avoid the pitfalls of type coercion. This operator checks both value and type.&lt;/p&gt;

&lt;p&gt;Thank you for following and liking. Happy Coding 🤝👋&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>Mastering JavaScript Functions: From Basics to Advanced Techniques</title>
      <dc:creator>Kiran Kumar</dc:creator>
      <pubDate>Tue, 17 Sep 2024 16:53:05 +0000</pubDate>
      <link>https://forem.com/kiran_kumar/mastering-javascript-functions-from-basics-to-advanced-techniques-45fa</link>
      <guid>https://forem.com/kiran_kumar/mastering-javascript-functions-from-basics-to-advanced-techniques-45fa</guid>
      <description>&lt;p&gt;JavaScript functions are fundamental building blocks in web development. We’ll explore different ways to define functions in JavaScript, including function statements, function expressions, anonymous functions, named functions, and function declarations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fui33deo5qwqp6zyn80ae.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fui33deo5qwqp6zyn80ae.png" alt="Image description" width="590" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Function Declaration
&lt;/h2&gt;

&lt;p&gt;A function declaration defines a named function. It consists of the function keyword, followed by the function name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvyk6shud2jou8w4qly32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvyk6shud2jou8w4qly32.png" alt="Image description" width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; &lt;br&gt;
Function declarations are hoisted, which means they can be used even before they are specified in the code. This makes them handy for arranging code in a top-down manner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fle0632mowqb2ucx3kzmw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fle0632mowqb2ucx3kzmw.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Function Expression
&lt;/h2&gt;

&lt;p&gt;The function keyword can be used to specify a function within an expression. A function expression is very similar to a function declaration, with nearly identical syntax. The primary distinction between a function expression and a function declaration is the function name, which can be omitted in function expressions to produce anonymous functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh5vg8aey9urnltb4pw1a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh5vg8aey9urnltb4pw1a.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; &lt;br&gt;
 Function expressions are not hoisted, so they are useful when you want to define a function within a specific scope or context.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjix2h5ind9tv3vvll8v6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjix2h5ind9tv3vvll8v6.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Anonymous Function
&lt;/h2&gt;

&lt;p&gt;An anonymous function is a function without a name. Also, it’s a JavaScript function that runs as soon as it is defined. Also known as an IIFE (Immediately Invoked Function Expression).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftt2knwutyx72vraa76k5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftt2knwutyx72vraa76k5.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Anonymous functions are often used as callbacks in event handling or asynchronous operations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctevi9c5ynzlyq0afzdf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctevi9c5ynzlyq0afzdf.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Named Function
&lt;/h2&gt;

&lt;p&gt;A named function is a function expression with a name. This name is local to the function body.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhiafbk50gpx48jwgkbd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhiafbk50gpx48jwgkbd.png" alt="Image description" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Named functions are useful for recursion or when you need to refer to the function within its own body.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5oxtl9rzscsllmeam4k8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5oxtl9rzscsllmeam4k8.png" alt="Image description" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Arrow Function
&lt;/h2&gt;

&lt;p&gt;An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations.&lt;/p&gt;

&lt;p&gt;Arrow functions don’t have their own bindings to this, arguments, or super, and should not be used as methods.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd6aiqcw9va0d4zaq1mn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd6aiqcw9va0d4zaq1mn.png" alt="Image description" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Arrow functions are ideal for short, simple functions&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F47587z92ap59yotz5t2x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F47587z92ap59yotz5t2x.png" alt="Image description" width="800" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding the different ways to define functions in JavaScript is crucial for writing clean, efficient, and maintainable code. Each method has its own advantages and use cases, so choose the one that best fits your needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>reactjsdevelopment</category>
    </item>
  </channel>
</rss>
