<?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>DEV Community: cpcjain</title>
    <description>The latest articles on DEV Community by cpcjain (@cpcjain).</description>
    <link>https://dev.to/cpcjain</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%2F873897%2F243f9a1c-38e2-4a50-87da-1dd1f3fcc40f.jpeg</url>
      <title>DEV Community: cpcjain</title>
      <link>https://dev.to/cpcjain</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cpcjain"/>
    <language>en</language>
    <item>
      <title>React Hooks Crash Course</title>
      <dc:creator>cpcjain</dc:creator>
      <pubDate>Mon, 07 Aug 2023 04:49:56 +0000</pubDate>
      <link>https://dev.to/cpcjain/react-hooks-crash-course-p9m</link>
      <guid>https://dev.to/cpcjain/react-hooks-crash-course-p9m</guid>
      <description>&lt;h1 id="heading-react-hooks"&gt;React Hooks&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useEffect&lt;/code&gt; is use to handle component side effects. Generally used in fetching data, subscribing to events or updating title. The first argument is an callback function that contains the effect we want to run and callback will be called after the component is rendered.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;import&lt;/span&gt; { useEffect, useState } &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;'react'&lt;/span&gt;;

&lt;span class="hljs-function"&gt;&lt;span&gt;function&lt;/span&gt; &lt;span&gt;MyComponent&lt;/span&gt;(&lt;span&gt;{ id }&lt;/span&gt;) &lt;/span&gt;{
  &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; [data, setData] = useState(&lt;span class="hljs-literal"&gt;null&lt;/span&gt;);

  useEffect(&lt;span class="hljs-function"&gt;() =&amp;gt;&lt;/span&gt; {
    fetch(&lt;span class="hljs-string"&gt;`https://my-api.com/data/&lt;span&gt;${id}&lt;/span&gt;`&lt;/span&gt;)
      .then(&lt;span class="hljs-function"&gt;&lt;span&gt;response&lt;/span&gt; =&amp;gt;&lt;/span&gt; response.json())
      .then(&lt;span class="hljs-function"&gt;&lt;span&gt;data&lt;/span&gt; =&amp;gt;&lt;/span&gt; setData(data));
  }, [id]);

  &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; (
    &lt;span class="xml"&gt;&lt;span&gt;&amp;lt;&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;
      {data ? &lt;span&gt;&amp;lt;&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt;Data: {data}&lt;span&gt;&amp;lt;/&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt; : &lt;span&gt;&amp;lt;&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt;Loading...&lt;span&gt;&amp;lt;/&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt;}
    &lt;span&gt;&amp;lt;/&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useState&lt;/code&gt; is a hook that allows you to add state to a functional component in React. State is a way to store and manage data that can change over time. It allows you to keep track of data that your component needs to render, such as a value entered in an input field or a toggle state.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;import&lt;/span&gt; { useState } &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;'react'&lt;/span&gt;;

&lt;span class="hljs-function"&gt;&lt;span&gt;function&lt;/span&gt; &lt;span&gt;MyComponent&lt;/span&gt;(&lt;span&gt;&lt;/span&gt;) &lt;/span&gt;{
  &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; [count, setCount] = useState(&lt;span class="hljs-number"&gt;0&lt;/span&gt;);

  &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; (
    &lt;span class="xml"&gt;&lt;span&gt;&amp;lt;&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;
      &lt;span&gt;&amp;lt;&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt;Count: {count}&lt;span&gt;&amp;lt;/&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt;
      &lt;span&gt;&amp;lt;&lt;span&gt;button&lt;/span&gt; &lt;span&gt;onClick&lt;/span&gt;=&lt;span&gt;{()&lt;/span&gt; =&amp;gt;&lt;/span&gt; setCount(count + 1)}&amp;gt;Increment&lt;span&gt;&amp;lt;/&lt;span&gt;button&lt;/span&gt;&amp;gt;&lt;/span&gt;
    &lt;span&gt;&amp;lt;/&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;useState returns an array containing two elements, the first one is the current state, and the second one is a function that allows you to update the state.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useMemo&lt;/code&gt; is used to memoize a value that's expensive to calculate. It takes a function that returns a value and an array of dependencies as arguments. It will only recalculate the value when one or more of the dependencies have changed. The value is then passed as a prop to a child component, and the child component only re-renders when the value changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;import&lt;/span&gt; { useMemo } &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;'react'&lt;/span&gt;;

&lt;span class="hljs-function"&gt;&lt;span&gt;function&lt;/span&gt; &lt;span&gt;MyComponent&lt;/span&gt;(&lt;span&gt;{data}&lt;/span&gt;) &lt;/span&gt;{
  &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; memoizedData = useMemo(&lt;span class="hljs-function"&gt;() =&amp;gt;&lt;/span&gt; expensiveCalculation(data), [data]);

  &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; (
    &lt;span class="xml"&gt;&lt;span&gt;&amp;lt;&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;
      &lt;span&gt;&amp;lt;&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt;Data: {memoizedData}&lt;span&gt;&amp;lt;/&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt;
    &lt;span&gt;&amp;lt;/&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useCallback&lt;/code&gt; is used to memoize a callback function. It takes a callback function and an array of dependencies as arguments. It will return a new callback function only when one or more of the dependencies have changed. The callback function is then passed as a prop to a child component, and the child component only re-renders when the callback function changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;import&lt;/span&gt; { useCallback } &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;'react'&lt;/span&gt;;

&lt;span class="hljs-function"&gt;&lt;span&gt;function&lt;/span&gt; &lt;span&gt;MyParentComponent&lt;/span&gt;(&lt;span&gt;{data}&lt;/span&gt;) &lt;/span&gt;{
  &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; handleClick = useCallback(&lt;span class="hljs-function"&gt;() =&amp;gt;&lt;/span&gt; {
    &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(data);
  }, [data]);

  &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; (
    &lt;span class="xml"&gt;&lt;span&gt;&amp;lt;&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;
      &lt;span&gt;&amp;lt;&lt;span&gt;MyChildComponent&lt;/span&gt; &lt;span&gt;onClick&lt;/span&gt;=&lt;span&gt;{handleClick}&lt;/span&gt; /&amp;gt;&lt;/span&gt;
    &lt;span&gt;&amp;lt;/&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;In summary, &lt;code&gt;useMemo&lt;/code&gt; is used to memoize a value and &lt;code&gt;useCallback&lt;/code&gt; is used to memoize a function. Both are used to make your component more performant by avoiding unnecessary calculations or re-renders.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;useReducer&lt;/code&gt; is a hook that allows you to manage state in a functional component using the concept of a "reducer" function. It's similar to &lt;code&gt;useState&lt;/code&gt;, but it is more powerful and flexible, and is better suited for handling complex state updates. The &lt;code&gt;useReducer&lt;/code&gt; hook takes two arguments, the first one is the reducer function, and the second one is the initial state. It returns an array containing the current state and a dispatch function that allows you to update the state by dispatching an action.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;import&lt;/span&gt; { useReducer } &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;'react'&lt;/span&gt;;

&lt;span class="hljs-function"&gt;&lt;span&gt;function&lt;/span&gt; &lt;span&gt;reducer&lt;/span&gt;(&lt;span&gt;state, action&lt;/span&gt;) &lt;/span&gt;{
  &lt;span class="hljs-keyword"&gt;switch&lt;/span&gt; (action.type) {
    &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-string"&gt;'increment'&lt;/span&gt;:
      &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;count&lt;/span&gt;: state.count + &lt;span class="hljs-number"&gt;1&lt;/span&gt; };
    &lt;span class="hljs-keyword"&gt;case&lt;/span&gt; &lt;span class="hljs-string"&gt;'decrement'&lt;/span&gt;:
      &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;count&lt;/span&gt;: state.count - &lt;span class="hljs-number"&gt;1&lt;/span&gt; };
    &lt;span class="hljs-keyword"&gt;default&lt;/span&gt;:
      &lt;span class="hljs-keyword"&gt;throw&lt;/span&gt; &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Error&lt;/span&gt;();
  }
}

&lt;span class="hljs-function"&gt;&lt;span&gt;function&lt;/span&gt; &lt;span&gt;MyComponent&lt;/span&gt;(&lt;span&gt;&lt;/span&gt;) &lt;/span&gt;{
  &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; [state, dispatch] = useReducer(reducer, { &lt;span class="hljs-attr"&gt;count&lt;/span&gt;: &lt;span class="hljs-number"&gt;0&lt;/span&gt; });

  &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; (
    &lt;span class="xml"&gt;&lt;span&gt;&amp;lt;&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;
      &lt;span&gt;&amp;lt;&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt;Count: {state.count}&lt;span&gt;&amp;lt;/&lt;span&gt;p&lt;/span&gt;&amp;gt;&lt;/span&gt;
      &lt;span&gt;&amp;lt;&lt;span&gt;button&lt;/span&gt; &lt;span&gt;onClick&lt;/span&gt;=&lt;span&gt;{()&lt;/span&gt; =&amp;gt;&lt;/span&gt; dispatch({ type: 'increment' })}&amp;gt;Increment&lt;span&gt;&amp;lt;/&lt;span&gt;button&lt;/span&gt;&amp;gt;&lt;/span&gt;
      &lt;span&gt;&amp;lt;&lt;span&gt;button&lt;/span&gt; &lt;span&gt;onClick&lt;/span&gt;=&lt;span&gt;{()&lt;/span&gt; =&amp;gt;&lt;/span&gt; dispatch({ type: 'decrement' })}&amp;gt;Decrement&lt;span&gt;&amp;lt;/&lt;span&gt;button&lt;/span&gt;&amp;gt;&lt;/span&gt;
    &lt;span&gt;&amp;lt;/&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;memo&lt;/code&gt; takes a component as its first argument, and an optional &lt;code&gt;compare&lt;/code&gt; function as a second argument. The &lt;code&gt;compare&lt;/code&gt; function is used to determine if the component should re-render based on the props. If no &lt;code&gt;compare&lt;/code&gt; function is provided, &lt;code&gt;memo&lt;/code&gt; will use a shallow comparison of the props to determine if the component should re-render.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt; allows you to create a reference to an element or a value in a functional component. It's a way to store a reference to an element or a value that persists between renders.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;import&lt;/span&gt; { useRef } &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;'react'&lt;/span&gt;;

&lt;span class="hljs-function"&gt;&lt;span&gt;function&lt;/span&gt; &lt;span&gt;MyComponent&lt;/span&gt;(&lt;span&gt;&lt;/span&gt;) &lt;/span&gt;{
  &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; inputRef = useRef(&lt;span class="hljs-literal"&gt;null&lt;/span&gt;);

  &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; (
    &lt;span class="xml"&gt;&lt;span&gt;&amp;lt;&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;
      &lt;span&gt;&amp;lt;&lt;span&gt;input&lt;/span&gt; &lt;span&gt;ref&lt;/span&gt;=&lt;span&gt;{inputRef}&lt;/span&gt; /&amp;gt;&lt;/span&gt;
      &lt;span&gt;&amp;lt;&lt;span&gt;button&lt;/span&gt; &lt;span&gt;onClick&lt;/span&gt;=&lt;span&gt;{()&lt;/span&gt; =&amp;gt;&lt;/span&gt; inputRef.current.focus()}&amp;gt;Focus Input&lt;span&gt;&amp;lt;/&lt;span&gt;button&lt;/span&gt;&amp;gt;&lt;/span&gt;
    &lt;span&gt;&amp;lt;/&lt;span&gt;div&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;In summary, &lt;code&gt;useState&lt;/code&gt; is used to manage state in a component and &lt;code&gt;useRef&lt;/code&gt; is used to create a reference to an element or a value in a component.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; is useful when you need to keep track of a value that can change over time, and you want the component to re-render when that value changes. &lt;code&gt;useRef&lt;/code&gt; is useful when you need to access or manipulate an element in the DOM, or if you need to keep track of a value that should persist between renders but does not need to cause a re-render.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;createContext&lt;/code&gt; is an API in React that allows you to create a context, which is a way to share data between components. It allows you to pass data down through a component tree without having to pass props down manually at every level. context refers to a way to share data between components without having to pass props down manually at every level of the component tree.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="heading-when-shouldnt-you-use-the-context-api"&gt;When shouldn't you use the Context API?&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Everything that consumes a context re-renders every time that context’s state changes.&lt;/strong&gt; That means that if you’re consuming your context all over the place in your app, or worse, using one context for your entire app’s state, you’re causing a ton of re-renders all over the place&lt;/p&gt;

&lt;h2 id="heading-what-is-a-fragment"&gt;What is a Fragment?&lt;/h2&gt;

&lt;p&gt;A common pattern in React is for a component to return multiple elements. Fragments &lt;strong&gt;let you group a list of children without adding extra nodes to the DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id="heading-what-is-a-portal"&gt;What is a portal?&lt;/h2&gt;

&lt;p&gt;A portal is a way to render children in a dom node that exists outside the dom hierachy of the parent component and it can live anywhere in the dom tree and you most often in ui components like modal.&lt;/p&gt;

&lt;h2 id="heading-what-are-uncontrolled-and-controlled-components"&gt;What are uncontrolled and controlled components?&lt;/h2&gt;

&lt;p&gt;A controlled component in React is when the component's state directly controls the value of form elements. Changes to the form elements are managed through React's event handlers.
Uncontrolled are input values which are typically controlled by users and react does not control.&lt;/p&gt;


&lt;p&gt;This article is published w/ &lt;a href="https://scattr.io?ref=dev"&gt;Scattr  ↗️&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Exploring the Mystery of Inequality: Why Two Empty Arrays or Objects in JavaScript are Not Equal</title>
      <dc:creator>cpcjain</dc:creator>
      <pubDate>Wed, 08 Mar 2023 04:13:59 +0000</pubDate>
      <link>https://dev.to/cpcjain/exploring-the-mystery-of-inequality-why-two-empty-arrays-or-objects-in-javascript-are-not-equal-m10</link>
      <guid>https://dev.to/cpcjain/exploring-the-mystery-of-inequality-why-two-empty-arrays-or-objects-in-javascript-are-not-equal-m10</guid>
      <description>&lt;p&gt;In JavaScript, data can be passed either by value or reference. The data type passed as a value is called primitive type while the data type passed as a reference can be collectively called Objects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bSaLNxgn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673781216362/355e4d42-76b4-4a56-b4ba-00bf3c8ce2e3.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bSaLNxgn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673781216362/355e4d42-76b4-4a56-b4ba-00bf3c8ce2e3.jpeg" alt="" width="594" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Primitive values are stored directly in the variable, and when the variable is assigned a new value, the old value is overwritten. Think of the variables as having no relationship to each other.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;let&lt;/span&gt; x = &lt;span class="hljs-number"&gt;5&lt;/span&gt;;
&lt;span class="hljs-keyword"&gt;let&lt;/span&gt; y = x;
x = &lt;span class="hljs-number"&gt;10&lt;/span&gt;;
&lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(y);  &lt;span class="hljs-comment"&gt;// prints 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When a variable is assigned a non-primitive value, such as an object or array, it is given a reference to that value instead of the value itself. This reference points to the location in memory where the object is stored, and the variable does not contain the object's value directly.&lt;/p&gt;

&lt;p&gt;When a variable, &lt;code&gt;let arr = []&lt;/code&gt;, is assigned the object, it receives the address or location of that object in memory, rather than the object itself. This means that the variable holds a reference to the object rather than containing the object's value directly.&lt;/p&gt;

&lt;p&gt;Let's clear it out with examples&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iI1fdyY6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673781550282/45799cf2-d6eb-490b-babe-913d2d2eb571.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iI1fdyY6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673781550282/45799cf2-d6eb-490b-babe-913d2d2eb571.jpeg" alt="" width="880" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we have two variables a and b that are assigned to the same object, they will be referentially equal.&lt;/p&gt;

&lt;p&gt;However, if we create new objects with the same properties, they will not be referentially equal to the first object, even though they have the same content.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V7uflvmK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673781695854/c8522b90-5872-47a5-b456-4b3bf7bde61b.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V7uflvmK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673781695854/c8522b90-5872-47a5-b456-4b3bf7bde61b.jpeg" alt="" width="880" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although the previous explanations may have provided a clear understanding, some confusion may still exist. To further clarify, let's consider a final example and examine it in detail.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-comment"&gt;// Here b is assigned the reference to the object that a holds,&lt;/span&gt;
&lt;span class="hljs-comment"&gt;// so, the comparison returns true&lt;/span&gt;
&lt;span class="hljs-keyword"&gt;let&lt;/span&gt; a = {
    &lt;span class="hljs-attr"&gt;name&lt;/span&gt;: &lt;span class="hljs-string"&gt;"Michael"&lt;/span&gt;,
    &lt;span class="hljs-attr"&gt;age&lt;/span&gt;: &lt;span class="hljs-number"&gt;40&lt;/span&gt;
}
&lt;span class="hljs-keyword"&gt;let&lt;/span&gt; b = a
&lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(b === a) &lt;span class="hljs-comment"&gt;// prints true&lt;/span&gt;

&lt;span class="hljs-comment"&gt;//Now b holds a reference to a new object and a holds a reference to the first object, so the comparison returns false&lt;/span&gt;
b = {
    &lt;span class="hljs-attr"&gt;name&lt;/span&gt;: &lt;span class="hljs-string"&gt;"Richard"&lt;/span&gt;,
    &lt;span class="hljs-attr"&gt;age&lt;/span&gt;: &lt;span class="hljs-number"&gt;45&lt;/span&gt;
}
&lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(b === a) &lt;span class="hljs-comment"&gt;// prints false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you want to check the properties of two distinct objects, then there are tons of ways you can do that. But the easiest method is using the`&lt;code&gt;JSON.stringify&lt;/code&gt;` method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ta8u3m8n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673781823638/52782da2-45c6-467f-bc3b-5125ea27df70.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ta8u3m8n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1673781823638/52782da2-45c6-467f-bc3b-5125ea27df70.jpeg" alt="" width="880" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In summary, primitive equality compares values and referential equality compares references to objects.&lt;/p&gt;



</description>
    </item>
    <item>
      <title>What type of language is JavaScript?</title>
      <dc:creator>cpcjain</dc:creator>
      <pubDate>Wed, 08 Mar 2023 04:12:54 +0000</pubDate>
      <link>https://dev.to/cpcjain/what-type-of-language-is-javascript-3cm5</link>
      <guid>https://dev.to/cpcjain/what-type-of-language-is-javascript-3cm5</guid>
      <description>&lt;p&gt;JavaScript is a high-level single-threaded garbage collected interpreted prototype-based multi-paradigm dynamic language with a non-blocking event loop.&lt;/p&gt;

&lt;p&gt;But what does all that mean?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A high-level programming language is a programming language that is more abstract and is easier for humans to read and write than a low-level programming language.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Single threaded means that the JavaScript engine can only execute one piece of code at a time.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  console.time()

  for (let i = 0; i &amp;lt; 100000000; i++){
      // do nothing
  }

  console.timeEnd()
  // default 5.705s

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the console would start the time then it will loop through a large number of iterations. While the loop is running, the JavaScript engine will not be able to execute any other code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A garbage collector is a program that automatically frees up memory that is no longer being used by the program. Garbage collection is useful because it helps to prevent memory leaks, which can cause a program to use up more and more memory over time.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An interpreted language is a type of programming language in which the source code is not compiled into machine code that can be run directly on a computer. Instead, the source code is interpreted by another program at runtime, which translates the code into instructions that the computer can execute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In a prototype-based language, objects can have properties and methods (functions). An object can inherit the properties and methods of its prototype, and it can also have its own properties and methods. This allows objects to be customized and extended in a flexible way.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A multi-paradigm programming language is a language that supports more than one programming paradigm, or approach to programming like object-oriented programming, functional programming, imperative programming, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In a dynamic language, the programmer does not need to specify the type of a value when declaring a variable or a function. This makes writing code easier because you don't have to wait to declare the type of variable when it's declared. But in the long run the freedom and wild nature of JavaScript can work against you making it more difficult to debug and maintain code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Event loop is a mechanism that allows the program to execute asynchronous code in a non-blocking manner. This feature is important in the context of JavaScript. Since JavaScript is a single-thread language, if a piece of code takes a long time to run, it can block the execution of other code which can make the program appear unresponsive.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  console.log('Start')

  setTimeout(() =&amp;gt; {
      console.log('Timeout expired')
  }, 1000);

  // Console
  // Start
  // End
  // Timeout expired

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, the setTimeout() function is used to set a timer that will expire after 1000 milliseconds (1 second). When the timer expires, it will generate an event that will be added to the event queue. The program does not stop or block when the setTimeout() function is called, but instead continues to run and processes the event when it becomes available.&lt;/p&gt;

&lt;p&gt;And that's a wrap. Thank you for taking the time to read this article. I hope you found it informative and valuable. If you have any thoughts or insights on the topic, please feel free to share them in the comments below. If you'd like to connect or learn more about my work, please don't hesitate to reach out. Thanks again for reading and have a great day!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
