<?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: AditiJ</title>
    <description>The latest articles on Forem by AditiJ (@aditi05).</description>
    <link>https://forem.com/aditi05</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%2F715068%2F7ba96ace-4a8c-4304-927d-59cf0e11959d.jpg</url>
      <title>Forem: AditiJ</title>
      <link>https://forem.com/aditi05</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aditi05"/>
    <language>en</language>
    <item>
      <title>Shallow Copy and Deep Copy</title>
      <dc:creator>AditiJ</dc:creator>
      <pubDate>Mon, 06 Jun 2022 13:23:11 +0000</pubDate>
      <link>https://forem.com/aditi05/shallow-copy-and-deep-copy-10hh</link>
      <guid>https://forem.com/aditi05/shallow-copy-and-deep-copy-10hh</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Shallow Copy&lt;/li&gt;
&lt;li&gt;Deep Copy&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;li&gt;References&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Good day, folks! &lt;br&gt;
Often times we want to create copies of variables for different purposes. There can be two different kinds of copies created - Shallow copy and Deep copy. We commonly encounter shallow copy and deep copy understanding and differences during interviews. So, let's go over the terminologies and related examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shallow Copy
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Shallow copy method creates a copy where the source and the copied variable reference remain the same. This means that modifications made in one place would affect both places. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's an example to get a better understanding:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const first_person = {
name: "Jack",
age: 24,
}

const second_person = first_person;

second_person.age = 25;

console.log(first_person.age); // output: 25
console.log(second_person.age); // output: 25


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

&lt;/div&gt;

&lt;p&gt;Changing the &lt;em&gt;age&lt;/em&gt; property value of &lt;code&gt;second_person&lt;/code&gt; object, changes the &lt;code&gt;first_person&lt;/code&gt; object's &lt;em&gt;age&lt;/em&gt; property too.&lt;/p&gt;

&lt;p&gt;Now let's look at another example: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const first_person = {
  name: "Jack",
  age: 24
};
let second_person = first_person;
second_person = {
  name: "Jack",
  age: 23
};

console.log(first_person.age); // Output: 24
console.log(second_person.age); // Output: 23


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

&lt;/div&gt;

&lt;p&gt;Uh, what happened here? 🤔 Why aren't the values same?&lt;br&gt;
Well, here we are not changing the values of a specific property rather we are assigning a new object. &lt;/p&gt;

&lt;h2&gt;
  
  
  Deep Copy
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Deep copy method creates a copy where the source and the copied variable reference are completely different. This means that modifications made in one place would only affect the variable where we are making a change. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now let's look at the examples:&lt;/p&gt;

&lt;p&gt;If the objects/arrays are not nested, then we can achieve deep copy by using&lt;/p&gt;

&lt;h3&gt;
  
  
  Spread (...) operator
&lt;/h3&gt;

&lt;p&gt;Using the spread syntax, we can create a deep copy if there is no nesting.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const first_person = {
name: "Jack",
age: 24,
}

const second_person = { ...first_person };

second_person.age = 25;

console.log(first_person.age); // output: 24
console.log(second_person.age); // output: 25


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

&lt;/div&gt;

&lt;p&gt;Let's take a look when there is nesting.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const first_person = {
  name: "Jack",
  age: 24,
  address: {
    apartment: "A",
    city: "London"
  }
};

const second_person = { ...first_person };

second_person.age = 25;
second_person.address.apartment = "N";
console.log(first_person.address.apartment); // output: N
console.log(second_person.address.apartment); // output: N


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

&lt;/div&gt;

&lt;p&gt;In case of nesting, the spread operator creates a shallow copy.&lt;/p&gt;

&lt;p&gt;If the objects/arrays are nested, then we can achieve deep copy by using&lt;/p&gt;

&lt;h3&gt;
  
  
  JSON.parse() and JSON.stringify()
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const first_person = {
  name: "Jack",
  age: 24,
  address: {
    apartment: "A",
    city: "London"
  }
};

const second_person = JSON.parse(JSON.stringify(first_person));

second_person.age = 25;
second_person.address.apartment = "N";
console.log(first_person);
console.log(second_person);


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

&lt;/div&gt;

&lt;p&gt;Output:&lt;br&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%2Fdo4hnojf8xtbsohg446r.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%2Fdo4hnojf8xtbsohg446r.png" alt="Output of JSON.parse and JSON.stringify"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Combining these two creates a deep copy even when there is nesting involved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Shallow copy method creates a copy where the source and the copied variable reference remain the same. Changing one, would change the other as well.&lt;/li&gt;
&lt;li&gt;Deep copy method creates a copy where the source and the copied variable reference are entirely different. Changing one, would not affect the other one.&lt;/li&gt;
&lt;li&gt;Common methods like Array.concat(), Array.from(), Object.assign(), etc creates a shallow copy.&lt;/li&gt;
&lt;li&gt;Spread(...) operator, creates a deep copy when there is no nesting.&lt;/li&gt;
&lt;li&gt;One of the ways to create a deep copy is by using JSON.parse() and JSON.stringify().&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Promises Methods- At a glance</title>
      <dc:creator>AditiJ</dc:creator>
      <pubDate>Fri, 13 May 2022 20:10:31 +0000</pubDate>
      <link>https://forem.com/aditi05/promises-methods-at-a-glance-4d88</link>
      <guid>https://forem.com/aditi05/promises-methods-at-a-glance-4d88</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Promise&lt;/li&gt;
&lt;li&gt;The Promise States&lt;/li&gt;
&lt;li&gt;Create a new Promise&lt;/li&gt;
&lt;li&gt;Promise Handlers&lt;/li&gt;
&lt;li&gt;Promise Methods&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;li&gt;References&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;They say &lt;em&gt;"A Promise is meant to be fulfilled"&lt;/em&gt;. The truth is- In JavaScript, it might as well be rejected. &lt;br&gt;
&lt;a href="https://i.giphy.com/media/Uryv1zlSjV4ZuDTy94/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Uryv1zlSjV4ZuDTy94/giphy.gif" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: This blog assumes you have a basic understanding of &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing"&gt;asynchronous programming&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Promise
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A Promise literally promises to get back with a value or information for something which hasn't even happened till now.&lt;/li&gt;
&lt;li&gt;It is simply a JavaScript object that holds a result of an asynchronous operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Promise States
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Pending&lt;/em&gt;&lt;/strong&gt;: This is the initial state of a promise. This depicts that the task is in progress.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Fulfilled&lt;/em&gt;&lt;/strong&gt;: If the task was successfully done, then the promise returns a fulfilled state.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Rejected&lt;/em&gt;&lt;/strong&gt;: If the task failed, then the promise returns a rejected state. &lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Settled&lt;/em&gt;&lt;/strong&gt;: Once the promise is resolved or rejected, we say that the promise has been settled. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Create a new Promise
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To create a Promise object, we use the Promise constructor.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myPromise = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled!");
  } else {
    reject("Promise Rejected!");
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try to find out the state of the promise by running &lt;code&gt;console.log(myPromise);&lt;/code&gt;. &lt;br&gt;
We will get a Promise Object in a Pending state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promise Handlers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  then()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To get the value of a resolved promise, we use then() method. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;syntax: promiseObj.then(callBackFunction)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eg: &lt;code&gt;myPromise.then((res) =&amp;gt; console.log(res));&lt;/code&gt; This will return &lt;code&gt;Promise Fulfilled!&lt;/code&gt; as an output. If the promise would have been reject, it would have given an error. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  catch()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To handle the error once a resolved promise is rejected, we use catch() method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;syntax: promiseObj.then(callBackFunction).catch(callBackFunction)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eg: &lt;code&gt;myPromise.then((res) =&amp;gt; console.log(res)).catch((e) =&amp;gt; console.log(e));&lt;/code&gt; This will return &lt;code&gt;Promise Rejected!&lt;/code&gt; as an output.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  finally()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The code inside finally() method will run irrespective of the promise state. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;syntax: promiseObj.then(callBackFunction).catch(callBackFunction).finally(callbackFunction)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eg: &lt;code&gt;myPromise.then((res) =&amp;gt; console.log(res)).catch((e) =&amp;gt; console.log(e));&lt;/code&gt; This will return &lt;code&gt;Promise Rejected!&lt;/code&gt; as an output.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Promise Methods
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;These methods are used to handle multiple promises &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Promise.all()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Promise.all() takes an array of promises as input.&lt;/li&gt;
&lt;li&gt;It waits for all promises to be resolved.&lt;/li&gt;
&lt;li&gt;It returns an array containing all values if all of them are fulfilled.&lt;/li&gt;
&lt;li&gt;It returns an error of the first rejected promise.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myPromise1 = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled 1!");
  } else {
    reject("Promise Rejected 1!");
  }
});

const myPromise2 = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled 2!");
  } else {
    reject("Promise Rejected 2!");
  }
});
const myPromise3 = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled 3!");
  } else {
    reject("Promise Rejected 3!");
  }
});

Promise.all([myPromise1, myPromise2, myPromise3]).then((res) =&amp;gt;
  console.log(res)
// output 
// ["Promise Fulfilled 1!", "Promise Fulfilled 2!", "Promise Fulfilled 3!"]
);

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Promise.any()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Promise.any() takes an array of promises as an input.&lt;/li&gt;
&lt;li&gt;It returns a value of the first fulfilled promise.&lt;/li&gt;
&lt;li&gt;Once it finds a fulfilled promise, it returns a value. It doesn't wait for any other promise to be complete.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myPromise1 = new Promise((resolve, reject) =&amp;gt; {
  const success = false;
  if (success) {
    resolve("Promise Fulfilled 1!");
  } else {
    reject("Promise Rejected 1!");
  }
});

const myPromise2 = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled 2!");
  } else {
    reject("Promise Rejected 2!");
  }
});
const myPromise3 = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled 3!");
  } else {
    reject("Promise Rejected 3!");
  }
});

Promise.any([myPromise1, myPromise2, myPromise3]).then((res) =&amp;gt;
  console.log(res)
// output 
// "Promise Fulfilled 2!"
);

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Promise.allSettled()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Promise.allSettled() takes an array of promises as an input.&lt;/li&gt;
&lt;li&gt;It waits for all promises to be either fulfilled or rejected.&lt;/li&gt;
&lt;li&gt;It returns an object containing status and reason in case a promise is rejected or value in case a promise is fulfilled.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myPromise1 = new Promise((resolve, reject) =&amp;gt; {
  const success = false;
  if (success) {
    resolve("Promise Fulfilled 1!");
  } else {
    reject("Promise Rejected 1!");
  }
});

const myPromise2 = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled 2!");
  } else {
    reject("Promise Rejected 2!");
  }
});
const myPromise3 = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled 3!");
  } else {
    reject("Promise Rejected 3!");
  }
});

Promise.allSettled([myPromise1, myPromise2, myPromise3]).then((res) =&amp;gt;
  console.log(res));

// output 
/* 
[
{
status: "rejected"
reason: "Promise Rejected 1!"
},
{
status: "fulfilled"
value: "Promise Fulfilled 2!"
},
{
status: "fulfilled"
value: "Promise Fulfilled 3!"
}
]
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  promise.race()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Promise.race() takes array of promises as an input.&lt;/li&gt;
&lt;li&gt;It returns a value of the first fulfilled promise or error in case of the first promise is rejected.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myPromise1 = new Promise((resolve, reject) =&amp;gt; {
  const success = false;
  if (success) {
    resolve("Promise Fulfilled 1!");
  } else {
    reject("Promise Rejected 1!");
  }
});

const myPromise2 = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled 2!");
  } else {
    reject("Promise Rejected 2!");
  }
});
const myPromise3 = new Promise((resolve, reject) =&amp;gt; {
  const success = true;
  if (success) {
    resolve("Promise Fulfilled 3!");
  } else {
    reject("Promise Rejected 3!");
  }
});

Promise.race([myPromise1, myPromise2, myPromise3]).then((res) =&amp;gt;
  console.log(res)
// output 
// "Promise Rejected 1!"
);

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Promise is a JavaScript object that holds a result of an asynchronous operation.&lt;/li&gt;
&lt;li&gt;It has 3 states- pending, fulfilled, and rejected.&lt;/li&gt;
&lt;li&gt;.then() and .catch() are promise handlers to access promise values once it is resolved.&lt;/li&gt;
&lt;li&gt;.finally() is used to execute a code irrespective of the promise state.&lt;/li&gt;
&lt;li&gt;Promise.all() waits for all promises to be resolved or for the first rejected promise.&lt;/li&gt;
&lt;li&gt;Promise.any() returns a value of the first fulfilled promise.&lt;/li&gt;
&lt;li&gt;Promise.allSettled() waits for all promises to be either fulfilled or rejected.&lt;/li&gt;
&lt;li&gt;Promise.race() returns a value of the first fulfilled promise or error in case of the first promise is rejected.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/javascript-promise-tutorial-how-to-resolve-or-reject-promises-in-js/"&gt;https://www.freecodecamp.org/news/javascript-promise-tutorial-how-to-resolve-or-reject-promises-in-js/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Call, Bind and Apply - The Trinity</title>
      <dc:creator>AditiJ</dc:creator>
      <pubDate>Fri, 13 May 2022 08:26:56 +0000</pubDate>
      <link>https://forem.com/aditi05/call-bind-and-apply-the-trinity-19eb</link>
      <guid>https://forem.com/aditi05/call-bind-and-apply-the-trinity-19eb</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction to Call Bind and Apply&lt;/li&gt;
&lt;li&gt;Definition&lt;/li&gt;
&lt;li&gt;Why to use?&lt;/li&gt;
&lt;li&gt;How to use?&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;li&gt;References&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction to Call Bind and Apply
&lt;/h2&gt;

&lt;p&gt;When I first came across these terms, I was super confused and clueless. How are these different from each other? Function borrowing!? - What does that even mean?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/gzQ1X1Fk25UwE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/gzQ1X1Fk25UwE/giphy.gif" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's my attempt to share what I have learned so far!&lt;br&gt;
&lt;em&gt;Note: This blog assumes you have a basic understanding of&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"&gt; this &lt;/a&gt;keyword.&lt;/em&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Function borrowing
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;It allows us to use methods of an object on another object without having to create a copy or rewrite it in several places.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  call()
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;call() is used to invoke a method. &lt;br&gt;
syntax- &lt;code&gt;call(thisArg, arg1,arg2, ...., argN)&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  apply()
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;apply() is also used to invoke a method.&lt;br&gt;
 It is similar to call(), with the only difference that call() takes all the arguments individually, while apply() take array like object as an argument.&lt;br&gt;
syntax- &lt;code&gt;apply(thisArg,[arg1,agr2,...,agrN])&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  bind()
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;bind() creates a new function which can be invoked somewhere later when needed.&lt;br&gt;
syntax- &lt;code&gt;bind(thisArg, arg1,arg2, ...., argN)&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Why to even use these in the first place?
&lt;/h2&gt;

&lt;p&gt;Let's take an example to understand when and why to use these methods. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First things first: We know how to access a normal Object property.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userList = {
  user: "Aditi",
  age: 25
};
//accessing the properties
console.log(`Hi ${userList.user}. How's ${userList.age} 
treating you?`);

// output
// Hi Aditi. How's 25 treating you?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now let's suppose we have a method inside userList.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userList = {
  user: "Aditi",
  age: 25,
printDetails: function(){
console.log(`Hi ${this.user}. How's ${this.age} 
treating you?`);
}
};
// output
// Hi Aditi. How's 25 treating you? 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we call &lt;code&gt;userList.printDetails();&lt;/code&gt; we get the same output.&lt;/p&gt;

&lt;p&gt;Now suppose we have another object &lt;code&gt;adminList&lt;/code&gt; and we want to access the &lt;code&gt;printDetails()&lt;/code&gt; method. What to do now? &lt;br&gt;
You can say we can create another object with a different value and copy the printDetails() method to access it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const adminList = {
  user: "Ananya",
  age: 26,
printDetails: function(){
console.log(`Hi ${this.user}. How's ${this.age} 
treating you?`);
}
};

// output
// Hi Ananya. How's 26 treating you? 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sounds good? &lt;br&gt;
Now suppose your app grows, and you have more numbers of objects. You would need to copy and repeat this method in all your objects. There also might be a case where the number of methods increases too. &lt;br&gt;
So, to avoid repetition we should use these function borrowing methods! &lt;/p&gt;

&lt;h2&gt;
  
  
  How to use these methods?
&lt;/h2&gt;

&lt;p&gt;Now let's understand how to solve the above-discussed problem. &lt;/p&gt;

&lt;h3&gt;
  
  
  Using call()
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userList = {
  user: "Aditi",
  age: 25,
  printDetails: function (city, country) {
    console.log(`Hi ${this.user} from ${city}, ${country}.
    How's ${this.age} treating you?`
    );
  }
};
const adminList = {
  user: "Ananya",
  age: 26
};

// borrowing printDetails() using call()

userList.printDetails.call(adminList, "Patna", "India");

// output
// Hi Ananya from Patna, India. How's 26 treating you? 

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using apply()
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userList = {
  user: "Aditi",
  age: 25,
  printDetails: function (city, country) {
    console.log(`Hi ${this.user} from ${city}, ${country}.
    How's ${this.age} treating you?`
    );
  }
};
const adminList = {
  user: "Ananya",
  age: 26
};

// borrowing printDetails() using apply()

userList.printDetails.apply(adminList, ["Patna", "India"]);

// output
// Hi Ananya from Patna, India. How's 26 treating you? 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using bind()
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userList = {
  user: "Aditi",
  age: 25,
  printDetails: function (city, country) {
    console.log(`Hi ${this.user} from ${city}, ${country}.
    How's ${this.age} treating you?`
    );
  }
};
const adminList = {
  user: "Ananya",
  age: 26
};

// borrowing printDetails() using bind()

const printAdminDetails = userList.printDetails.bind(
  adminList,
  "Patna",
  "India"
);

printAdminDetails();

// output
// Hi Ananya from Patna, India. How's 26 treating you? 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, without having to copy the &lt;code&gt;printDetails()&lt;/code&gt; inside the second object &lt;code&gt;adminlist&lt;/code&gt; we were able to access the method with the help of call(), apply() and bind().&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;call(), bind() and apply() are used to borrow methods of a object. &lt;/li&gt;
&lt;li&gt;call() and apply() are invoked and executed immediately.&lt;/li&gt;
&lt;li&gt;The difference between call() and apply() is the way they take arguments. call() takes all arguments separately while apply() takes arguments in an array.&lt;/li&gt;
&lt;li&gt;bind() creates a new function which is executed later when invoked.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>What is Virtual DOM doing?</title>
      <dc:creator>AditiJ</dc:creator>
      <pubDate>Tue, 28 Sep 2021 19:03:09 +0000</pubDate>
      <link>https://forem.com/aditi05/what-is-virtual-dom-doing-3h2f</link>
      <guid>https://forem.com/aditi05/what-is-virtual-dom-doing-3h2f</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In Vanilla JS or jQuery, we need to manipulate DOM and add event handlers to handle every change. This makes the UI slow.&lt;/p&gt;

&lt;p&gt;A React Application is responsible for the View part of the system. &lt;br&gt;
This means it handles the user interface for the application.&lt;/p&gt;

&lt;p&gt;When a user interacts with the system, changes take place.&lt;br&gt;
&lt;code&gt;For example&lt;/code&gt; The user fills the username and password and then clicks on the Login button. We take that user to the Homepage after successful login or give an error message if the username or password is wrong.&lt;br&gt;
This leads to updating the prior state to the current state. Then the app describes what the UI should look like after these changes by the user. This process is called rendering and the output of this render is a React Element. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A react element is a simple plain JavaScript Object that maps to the DOM Element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It keeps a light-weight representation of the actual DOM element and performs all operations on it. This is called the Virtual DOM. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ip5S6zO7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hjjwhp7bk5l3rs4do67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ip5S6zO7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hjjwhp7bk5l3rs4do67.png" alt="DOM state before state change"&gt;&lt;/a&gt;&lt;br&gt;
Whenever we change the state of a component, we get a new React Element.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W0YfUxVm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ybqvxgixrdld2l9v5yn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W0YfUxVm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ybqvxgixrdld2l9v5yn.png" alt="Virtual DOM changed state"&gt;&lt;/a&gt; &lt;br&gt;
React then compares this element along with its children with the prior state in the virtual DOM and figures out what is changed. After this, it updates the part of the actual DOM to keep it in sync with the Virtual DOM.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w6NeQU1e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c7u2cjjf5hdp1deswxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w6NeQU1e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c7u2cjjf5hdp1deswxy.png" alt="After re-rendering"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>React Components In Theory</title>
      <dc:creator>AditiJ</dc:creator>
      <pubDate>Tue, 28 Sep 2021 17:21:20 +0000</pubDate>
      <link>https://forem.com/aditi05/react-components-in-theory-3n9f</link>
      <guid>https://forem.com/aditi05/react-components-in-theory-3n9f</guid>
      <description>&lt;h2&gt;
  
  
  Development
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;React development was led by &lt;strong&gt;Jordan Walke&lt;/strong&gt; who is a software engineer at Facebook. React was first deployed in 2011. &lt;br&gt;
Now it has become super famous and a lot of contributions have been made globally. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;React is a JavaScript library used for building fast and interactive user interfaces.  &lt;/p&gt;

&lt;p&gt;The heart and soul of every React-based Application are &lt;strong&gt;Components.&lt;/strong&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Components
&lt;/h3&gt;

&lt;p&gt;A Component in plain words, is just like a building block. We take a bunch of isolated code blocks and fit them together to form a large scale application.&lt;/p&gt;

&lt;p&gt;We can also visualize a React App as a tree of Components.&lt;br&gt;
&lt;code&gt;Taking Twitter for Example&lt;/code&gt; We can split the home page into independent components. The App becomes the &lt;strong&gt;Root component&lt;/strong&gt; or the &lt;strong&gt;parent&lt;/strong&gt; component which contains all the other components as its &lt;strong&gt;child components&lt;/strong&gt;. &lt;br&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%2Ftnmho3v4ims7jjaeeiqu.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%2Ftnmho3v4ims7jjaeeiqu.png" alt="React Twitter tree component example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here in this example, we see how a large app like Twitter can be made manageable with the help of components. &lt;br&gt;
Components allow us to make use of reusability in our apps thus increasing code readability and code management. &lt;br&gt;
&lt;code&gt;For instance:&lt;/code&gt; The Tweet button can be used across the Twitter application for different purposes with a different look and feel!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Let's take another example&lt;/code&gt; We can split the home page of a website into independent components like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SearchBar&lt;/li&gt;
&lt;li&gt;SideBar&lt;/li&gt;
&lt;li&gt;NavBar&lt;/li&gt;
&lt;li&gt;Card
&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%2Fr8s41627kmbds87dsaw5.png" alt="Website component example"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The component-based design in React increases the efficiency to write code and makes it easier to follow and learn. The components also provide their own logic and a layer of separation in general so that one can add and update logic without causing a lot of issues. &lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
