<?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: nouman shah</title>
    <description>The latest articles on DEV Community by nouman shah (@nomishah).</description>
    <link>https://dev.to/nomishah</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%2F220063%2F418d9728-bdf6-4d10-9764-0d4cb0eb0450.jpeg</url>
      <title>DEV Community: nouman shah</title>
      <link>https://dev.to/nomishah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nomishah"/>
    <language>en</language>
    <item>
      <title>call(), apply() and bind() methods in Javascript</title>
      <dc:creator>nouman shah</dc:creator>
      <pubDate>Sun, 28 Mar 2021 11:30:35 +0000</pubDate>
      <link>https://dev.to/nomishah/call-apply-and-bind-methods-in-javascript-2m40</link>
      <guid>https://dev.to/nomishah/call-apply-and-bind-methods-in-javascript-2m40</guid>
      <description>&lt;p&gt;We know that functions are a special kind of objects in JavaScript. So they have access to some methods and properties. To prove functions are objects, we can do something like this, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greeting() {
  console.log('Hello World');
}
greeting.lang = 'English';
// Prints 'English'
console.log(greeting.lang);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript also provides some special methods and properties to every function object. So every function in JavaScript inherits those methods. Call, bind, and apply are some of the methods that every function inherits.&lt;/p&gt;

&lt;h1&gt;
  
  
  call( )
&lt;/h1&gt;

&lt;p&gt;The call( ) allows for a function/method belonging to one object to be assigned and called for a different object.&lt;br&gt;
Using the call method we can do a function borrowing.&lt;br&gt;
We can borrow a method from some object and use it with the data of some other object.&lt;br&gt;
We can do like following-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = {
  firstName: "nouman",
  lastName: "shah",
}

let printFullName = function () {
  console.log(this.firstName + " " + this.lastName)
}
printFullName.call(name)
//nouman shah
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name2 = {
  firstName: "Ali",
  lastName: "khan"
}
printFullName.call(name2)

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

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;In call( ), we pass arguments individually.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let printFullName = function (hometown, gender, job) {
  console.log(this.firstName + " " + this.lastName + " " + "from" + " " + hometown + " " + gender + " " + job)
}
printFullName.call(name, "Pakistan", "Male", "Software Engineer")

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  apply( )
&lt;/h1&gt;

&lt;p&gt;The apply() method is similar to call(). The difference is that the apply() method accepts an array of arguments instead of comma-separated values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;printFullName.apply(name,["Pakistan","Male", "Software Engineer"])

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  bind( )
&lt;/h1&gt;

&lt;p&gt;bind method looks similar to call but instead of directly calling the method, the bind method binds the printFullName with an object and returns a copy of that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let printMyName=printFullName.bind(name)
printMyName();
// nouman shah
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What it will do is that it will create a copy of printFullName and it will bind that with name object and will return a function that can be called later.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The difference between call() and bind() is that the call()  does not create a new copy of the function, &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can also pass extra arguments to the bind method. The general syntax for this is function.bind(this, arg1, arg2, ...).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you have any questions or suggestions please let me know.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Promises vs Observables</title>
      <dc:creator>nouman shah</dc:creator>
      <pubDate>Wed, 06 Jan 2021 07:51:01 +0000</pubDate>
      <link>https://dev.to/nomishah/javascript-promises-vs-observables-fpn</link>
      <guid>https://dev.to/nomishah/javascript-promises-vs-observables-fpn</guid>
      <description>&lt;p&gt;&lt;strong&gt;Asynchronous Programming in JavaScript&lt;/strong&gt;&lt;br&gt;
 There are different ways in JavaScript to create asynchronous code. The most important ones are the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Callbacks&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Promises&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Async/Await&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RxJS Observables&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Callbacks&lt;/strong&gt;&lt;br&gt;
This is the old-fashioned classical approach to asynchronous programming. You provide a function as an argument to another function that executes an asynchronous task. When the asynchronous task completes, the executing function calls your callback function.&lt;br&gt;
The main disadvantage of this approach occurs when you have multiple chained asynchronous tasks, which requires you to define callback functions within callback functions within callback functions… This is called &lt;strong&gt;callback hell&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greeting(name) {
  console.log(`Hello ${name}!`);
}

function introduction(firstName, lastName, callback) {
  const fullName = `${firstName} ${lastName}`;

  callback(fullName);
}

introduction('Nouman','shah', greeting); 
//"Hello Nouman shah!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Promises&lt;/strong&gt;&lt;br&gt;
Promises have been introduced in ES6 (2015) to allow for more readable asynchronous code than is possible with callbacks.&lt;br&gt;
The main difference between callbacks and promises is that with callbacks you tell the executing function what to do when the asynchronous task completes, whereas with promises the executing function returns a special object to you (the promise) and then you tell the promise what to do when the asynchronous task completes.&lt;br&gt;
Promises have three states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pending:&lt;/strong&gt; This is the initial state of the Promise before an
       operation begins&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fulfilled:&lt;/strong&gt; This means the specified operation was completed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rejected:&lt;/strong&gt; The operation did not complete; an error value is usually thrown
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getUsers(onSuccess) {
  return new Promise((resolve, reject) =&amp;gt; {
    setTimeout(() =&amp;gt; {
      // Handle resolve and reject in the asynchronous API
      if (onSuccess) {
        resolve([
          {id: 1, name: 'Jerry'},
          {id: 2, name: 'Elaine'},
          {id: 3, name: 'George'},
        ])
      } else {
         reject('Failed to fetch data!')
      }
    }, 1000)
  })
}

// Run the getUsers function with the false flag to trigger an error
getUsers(false)
  .then((response) =&amp;gt; {
    console.log(response)
  })
  .catch((error) =&amp;gt; {
    console.log(error)
  })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Async/Await&lt;/strong&gt;&lt;br&gt;
There’s a special syntax to work with promises in a more comfortable fashion, called “async/await”. It’s surprisingly easy to understand and use.&lt;/p&gt;

&lt;p&gt;Basically, you can declare a function to be async, which allows you to use the await keyword in the body of this function. The await keyword can be put in front of an expression that evaluates to a promise. The await keyword pauses the execution of the async function until the promise is resolved. When this happens, the entire await expression evaluates to the result value of the promise, and then the execution of the async function resumes.&lt;br&gt;
Furthermore, the async function itself returns a promise as well that is resolved when the execution of the function body completes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function asyncTask(i) {
    return new Promise(resolve =&amp;gt; resolve(i + 1));
}
async function runAsyncTasks() {
    const res1 = await asyncTask(0);
    const res2 = await asyncTask(res1);
    const res3 = await asyncTask(res2);
    return "Everything done"
}
runAsyncTasks().then(result =&amp;gt; console.log(result));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;RxJS Observables&lt;/strong&gt;&lt;br&gt;
Observables are also like callbacks and promises that are responsible for handling async requests. Observables are a part of the RXJS library that makes use of Observables, making it really easy to write asynchronous code. &lt;/p&gt;

&lt;p&gt;There are four stages through which observables pass. They are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Creation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Subscription&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Destruction&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Creation&lt;/strong&gt; of an observable is done using a create function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var observable = Rx.Observable.create((observer: any) =&amp;gt;{
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make an observable work, we have to &lt;strong&gt;subscribe&lt;/strong&gt; it. This can be done using the subscribe method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;observable.subscribe((data)=&amp;gt;{
   console.log(data);    
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Execution&lt;/strong&gt; of observables is what is inside of the create block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Destruction&lt;/strong&gt; after an error or a complete notification, the observable is automatically unsubscribed. But there are cases where we have to manually unsubscribe it. To manually do this task, just use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var subscription = observable.subscribe(x =&amp;gt; console.log(x)); // Later: subscription.unsubscribe();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Promises vs observables
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Observables are lazy whereas promises are not&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promises are eager: the executor function is called as soon as the promise is created.&lt;/li&gt;
&lt;li&gt;Observables are lazy: the subscriber function is only called when a client subscribes to the observable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Observables handle multiple values unlike promises&lt;/strong&gt;&lt;br&gt;
Promises can only provide a single value whereas observables can give you multiple values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Observables are cancelable&lt;/strong&gt;&lt;br&gt;
You can cancel observables by unsubscribing it using the unsubscribe method whereas promises don’t have such a feature.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>forEach(), map(), filter() What's the difference?
</title>
      <dc:creator>nouman shah</dc:creator>
      <pubDate>Fri, 01 Jan 2021 13:00:26 +0000</pubDate>
      <link>https://dev.to/nomishah/foreach-map-filter-what-s-the-difference-1ogo</link>
      <guid>https://dev.to/nomishah/foreach-map-filter-what-s-the-difference-1ogo</guid>
      <description>&lt;h1&gt;
  
  
  The map() method
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;map()&lt;/code&gt; method loops through each element in array and calls the provided function for each element. This method creates a new array and doesn’t alter the original array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [5, 6, 8, 14, 32];

const updatedNumbers = numbers.map((number) =&amp;gt; {
    return number + 10;
});

console.log(updatedNumbers); // [15, 16, 18, 24, 42]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The filter() method
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;filter()&lt;/code&gt; method in JavaScript creates a new array with the elements that satisfies the provided condition. This method calls a provided function for each element in array and verifies the condition given in the provided function and passes only those elements that satisfy the given condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [5, 6, 9, 32, 14];

const even = numbers.filter((number) =&amp;gt; {
     return number % 2 === 0;
});

console.log(even); // [6, 32, 14]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The forEach() method
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;forEach()&lt;/code&gt; is used to execute the same code on every element in an array but does not change the array and it returns undefined.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
In the example below we would use .forEach() to iterate over an array of food and log that we would want to eat each of them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let food = ['mango','rice','pepper','pear'];

food.forEach(function(foodItem){ 

console.log('I want to eat '+foodItem);
});

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

&lt;/div&gt;



&lt;p&gt;Hope you have got a clear idea about both JavaScript array methods &lt;code&gt;map()&lt;/code&gt; &lt;code&gt;filter()&lt;/code&gt; and &lt;code&gt;forEach()&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Remove duplicates from an array using indexOf() and filter() methods</title>
      <dc:creator>nouman shah</dc:creator>
      <pubDate>Fri, 01 Jan 2021 06:42:43 +0000</pubDate>
      <link>https://dev.to/nomishah/remove-duplicates-from-an-array-using-indexof-and-filter-methods-2jeh</link>
      <guid>https://dev.to/nomishah/remove-duplicates-from-an-array-using-indexof-and-filter-methods-2jeh</guid>
      <description>&lt;p&gt;&lt;strong&gt;There are many ways to remove duplicates from array in JavaScript but today I will use indexOf and filter methods!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The indexOf() method returns the index of the first occurrence of an element in an array. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let chars = ['A', 'B', 'A', 'C', 'B'];
chars.indexOf('B'); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove the duplicates, you use the filter() method to include only elements whose indexes match their indexOf values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = ['A', 'B', 'A', 'C', 'B'];
const uniqueArr = arr.filter((c, index) =&amp;gt; {
    return arr.indexOf(c) === index;
});
console.log(uniqueArr);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output: [ 'A', 'B', 'C' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To find the duplicate values, you just need to reverse the condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = ['A', 'B', 'A', 'C', 'B'];
const uniqueArr = arr.filter((c, index) =&amp;gt; {
    return arr.indexOf(c) !== index;
});
console.log(uniqueArr);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output: [ 'A', 'B' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
