<?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: Kirtesh Bansal</title>
    <description>The latest articles on DEV Community by Kirtesh Bansal (@kirteshbansal).</description>
    <link>https://dev.to/kirteshbansal</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%2F712847%2Fbf4d6496-a179-4ffc-9ca9-ecb851ba1a7a.png</url>
      <title>DEV Community: Kirtesh Bansal</title>
      <link>https://dev.to/kirteshbansal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kirteshbansal"/>
    <language>en</language>
    <item>
      <title>🧰 Choosing the Right JavaScript Package Manager in 2025: npm vs Yarn vs pnpm vs Bun</title>
      <dc:creator>Kirtesh Bansal</dc:creator>
      <pubDate>Fri, 18 Apr 2025 06:11:48 +0000</pubDate>
      <link>https://dev.to/kirteshbansal/choosing-the-right-javascript-package-manager-in-2025-npm-vs-yarn-vs-pnpm-vs-bun-2jie</link>
      <guid>https://dev.to/kirteshbansal/choosing-the-right-javascript-package-manager-in-2025-npm-vs-yarn-vs-pnpm-vs-bun-2jie</guid>
      <description>&lt;p&gt;When building JavaScript applications, your choice of &lt;strong&gt;package manager&lt;/strong&gt; can have a big impact on development speed, reliability, and even how much space your dependencies take up.&lt;/p&gt;

&lt;p&gt;With options like &lt;strong&gt;npm&lt;/strong&gt;, &lt;strong&gt;Yarn&lt;/strong&gt;, &lt;strong&gt;pnpm&lt;/strong&gt;, and the rising star &lt;strong&gt;Bun&lt;/strong&gt;, developers often ask:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;“Which one should I use and why?”&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this post, I’ll break down what each package manager offers, where it shines, where it falls short, and when you should (or shouldn’t) use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  📦 1. npm — The OG (Node Package Manager)
&lt;/h2&gt;

&lt;h2&gt;
  
  
  ✅ Pros:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Comes bundled with &lt;strong&gt;Node.js&lt;/strong&gt;, so it’s always available out of the box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Huge community support and ecosystem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Familiar to almost every JavaScript developer.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ❌ Cons:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Slower installations compared to newer tools.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can duplicate a lot of dependencies across projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Less efficient use of disk space.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Limited monorepo support (requires external tools like Lerna).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎯 Ideal For:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simpler projects or teams already using it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Projects where compatibility and stability matter more than performance.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🚫 Avoid If:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You want maximum speed and disk efficiency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You’re managing a large monorepo.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🔧 Example:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Installing React:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Running a script:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Creating a package.json:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  🧶 2. Yarn — The Deterministic Pioneer
&lt;/h2&gt;

&lt;p&gt;Created by Facebook, &lt;strong&gt;Yarn&lt;/strong&gt; was built to solve npm’s early shortcomings by offering &lt;strong&gt;speed, determinism&lt;/strong&gt;, and better dependency resolution.&lt;/p&gt;
&lt;h2&gt;
  
  
  ✅ Pros:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Faster than npm, especially with caching.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deterministic installs via yarn.lock.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Built-in support for &lt;strong&gt;monorepos&lt;/strong&gt; (workspaces).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Yarn 2+ (Berry)&lt;/strong&gt; introduces innovative features like Plug’n’Play (PnP), which removes node_modules.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  ❌ Cons:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Yarn 2+ is a significant shift from v1 (some breaking changes).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Plug’n’Play can break some older packages that expect node_modules.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🎯 Ideal For:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Projects needing monorepos or strict dependency management.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Teams that want modern tooling with good performance.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🚫 Avoid If:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You rely on legacy packages or tooling that assume traditional node_modules.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🔧 Example:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Installing React:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Running a script:&lt;/strong&gt;&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Then:&lt;/strong&gt;&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
  
  
  🌱 3. pnpm — Performance &amp;amp; Efficiency First
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;pnpm&lt;/strong&gt; is all about speed and &lt;strong&gt;disk efficiency&lt;/strong&gt;. It introduces a unique way of storing dependencies using &lt;strong&gt;hard links&lt;/strong&gt; to a global store, saving space and avoiding duplication.&lt;/p&gt;
&lt;h2&gt;
  
  
  ✅ Pros:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Blazing fast installs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uses up to 70% less disk space than npm/Yarn.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enforces strict dependency declarations (no more accidental access to undeclared packages).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Excellent built-in support for &lt;strong&gt;monorepos&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  ❌ Cons:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Slight learning curve due to its non-standard node_modules layout.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Some tools (very few nowadays) might expect npm-style layouts.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🎯 Ideal For:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Projects with large dependency trees.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Monorepos and enterprise-scale applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Teams that value speed and discipline&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🚫 Avoid If:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You’re using very old tooling or dependencies that don’t like non-standard layouts (rare these days).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🔧 Example:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Installing React:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pnpm add react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Running a script:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pnpm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Creating a pnpm workspace:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# pnpm-workspace.yaml
packages:
  - 'apps/*'
  - 'packages/*'yam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Then:&lt;/strong&gt;&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
  
  
  ⚡️ 4. Bun — The All-In-One Newcomer
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Bun&lt;/strong&gt; is more than a package manager — it’s an entire &lt;strong&gt;JavaScript runtime&lt;/strong&gt;, bundler, and test runner, written in &lt;strong&gt;Zig&lt;/strong&gt;. It’s designed from the ground up for speed.&lt;/p&gt;
&lt;h2&gt;
  
  
  ✅ Pros:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Ridiculously fast installs (and everything else).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bundler, transpiler, test runner, and runtime — all in one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;TypeScript support out of the box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Modern APIs and first-class ES module support.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  ❌ Cons:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Still evolving — some features and compatibility can be hit-or-miss.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Not yet widely adopted or battle-tested like Node/npm.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Some Node.js APIs and npm packages may not be fully supported.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🎯 Ideal For:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Side projects, prototypes, and developers chasing the bleeding edge.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Projects where speed matters above all else.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🚫 Avoid If:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You need long-term production stability and full compatibility with the Node/npm ecosystem (for now).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🔧 Example:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Installing React:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bun add react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Running a script:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bun run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Creating a new project:&lt;/strong&gt;&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Running a dev server:&lt;/strong&gt;&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bun dev&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  🧾 Quick Comparison Table — TL; DR&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzqrfawceooyq84uvxk7t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzqrfawceooyq84uvxk7t.png" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Final Thoughts: What Should You Use?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;🟢 &lt;strong&gt;pnpm&lt;/strong&gt; — Best all-around for performance, workspaces, and modern development.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🔵 &lt;strong&gt;Yarn&lt;/strong&gt; — Great for monorepos and larger teams (just mind the Yarn 2+ learning curve).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🔴 &lt;strong&gt;npm&lt;/strong&gt; — Reliable and compatible, good for legacy or simple apps.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;⚡ &lt;strong&gt;Bun&lt;/strong&gt; — For speed-loving early adopters and greenfield projects.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💬 Have a favorite or horror story with one of these? Let me know in the comments! And if you found this post helpful, give it a clap 👏 or share it with your fellow devs!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>packagemanager</category>
      <category>javascript</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Downsize your JavaScript: Mastering Bundler Optimizations</title>
      <dc:creator>Kirtesh Bansal</dc:creator>
      <pubDate>Mon, 03 Mar 2025 10:12:23 +0000</pubDate>
      <link>https://dev.to/kirteshbansal/downsize-your-javascript-mastering-bundler-optimizations-43ae</link>
      <guid>https://dev.to/kirteshbansal/downsize-your-javascript-mastering-bundler-optimizations-43ae</guid>
      <description></description>
      <category>javascript</category>
      <category>webdev</category>
      <category>performance</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Javascript call(), apply(), bind() methods &amp; their polyfills</title>
      <dc:creator>Kirtesh Bansal</dc:creator>
      <pubDate>Sun, 30 Jan 2022 11:11:41 +0000</pubDate>
      <link>https://dev.to/kirteshbansal/call-apply-and-bind-javascript-methods-their-polyfills-k4e</link>
      <guid>https://dev.to/kirteshbansal/call-apply-and-bind-javascript-methods-their-polyfills-k4e</guid>
      <description>&lt;p&gt;In this article, I'll be explaining the call, apply &amp;amp; bind methods &amp;amp; how to write their polyfills. These three polyfills are very commonly asked questions in a javascript interview.&lt;/p&gt;

&lt;p&gt;Let's get started with an example to understand the need of these methods &amp;amp; then we'll jump on their implementations.&lt;/p&gt;

&lt;p&gt;Look at the below code, we have a &lt;strong&gt;person&lt;/strong&gt; object &amp;amp; a &lt;strong&gt;printName&lt;/strong&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
  firstname: "Kirtesh",
  lastname: "bansal"
}

let printName = function (country) {
  console.log(this.firstname + " " + this.lastname + " from " 
  + country);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see the person object is very generic object &amp;amp; we can have multiple object of same type with different values. Here, I'm using &lt;strong&gt;this&lt;/strong&gt; keyword in &lt;strong&gt;printName&lt;/strong&gt; method. If you are not familiar with it. Don't worry. we'll cover it later. &lt;/p&gt;

&lt;p&gt;Now, we want to perform a &lt;strong&gt;printName&lt;/strong&gt; method for each person object. &lt;/p&gt;

&lt;p&gt;How can we do that?&lt;/p&gt;

&lt;p&gt;First option is that, we add the &lt;strong&gt;printName&lt;/strong&gt; method to each object &amp;amp; call it as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
  firstname: "Kirtesh",
  lastname: "bansal",
  printName : function (country) {
             console.log(this.firstname + " " + this.lastname 
             + " from " + country);
             }    
}

person.printName("India");

Output: 
"Kirtesh bansal from India"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the above code. you will realise that we are duplicating the &lt;strong&gt;printName&lt;/strong&gt; method for each object. It doesn't seem to be a good practise. That is the reason, I've defined &lt;strong&gt;printName&lt;/strong&gt; method as a seperate method in the first code block.&lt;/p&gt;

&lt;p&gt;Now, What?&lt;/p&gt;

&lt;p&gt;Javascript provides us three methods to handle such cases without duplicating code. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. call(object, arguments)&lt;/strong&gt; - invokes the function on passed object along with passed arguments if there &lt;br&gt;
&lt;strong&gt;2. apply(object, [arguments])&lt;/strong&gt; - invokes the function on passed object along with passed array of arguments if there&lt;br&gt;
&lt;strong&gt;3. bind(object, arguments)&lt;/strong&gt; - returns a new function with referencing passed object and arguments&lt;/p&gt;

&lt;p&gt;Let's start with first the method.&lt;/p&gt;
&lt;h1&gt;
  
  
  1. call method
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;call&lt;/strong&gt; method invokes the function by taking the object on which the method has to be executed as first argument and accepts arguments which can be passed in that method like country in printName method.&lt;/p&gt;

&lt;p&gt;It becomes equal to &lt;code&gt;person.printName("India")&lt;/code&gt;. Where in the &lt;strong&gt;printName&lt;/strong&gt; method &lt;strong&gt;this&lt;/strong&gt; keyword refers to the person object. As &lt;strong&gt;this&lt;/strong&gt; always refers to left side of the &lt;code&gt;.&lt;/code&gt; on which method is being called. You can check this &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" rel="noopener noreferrer"&gt;link&lt;/a&gt; to know more about &lt;strong&gt;this&lt;/strong&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
  firstname: "Kirtesh",
  lastname: "bansal"
}

let printName = function (country) {
  console.log(this.firstname + " " + this.lastname + " from " 
  + country);
}

printName.call(person, "India");

Output: 
"Kirtesh bansal from India"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, We've attached &lt;strong&gt;call&lt;/strong&gt; method to &lt;strong&gt;printName&lt;/strong&gt; function where the printName method is called for person object therefore it takes the value of firstname &amp;amp; lastname from it &amp;amp; take "India" as parameter for country argument and results the above output.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. apply method
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;apply&lt;/strong&gt; method is very simillar to the &lt;strong&gt;call&lt;/strong&gt; method but the only difference is that &lt;strong&gt;call&lt;/strong&gt; method takes the argumetns as comma seperated values where as &lt;strong&gt;apply&lt;/strong&gt; method takes an array of arguments. If we write the apply method implemetation of the above code. It'll be like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
  firstname: "Kirtesh",
  lastname: "bansal"
}

let printName = function (country) {
  console.log(this.firstname + " " + this.lastname + " from " 
  + country);
}

printName.apply(person, ["India"]);

Output: 
"Kirtesh bansal from India"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we replaced &lt;strong&gt;call&lt;/strong&gt; with &lt;strong&gt;apply&lt;/strong&gt; method &amp;amp; passed the  argument in an array.&lt;/p&gt;

&lt;p&gt;let's move to last method.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. bind method
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;bind&lt;/strong&gt; method is simillar to the &lt;strong&gt;call&lt;/strong&gt; method but the only difference is that &lt;strong&gt;call&lt;/strong&gt; method invokes the function but incase of &lt;strong&gt;bind&lt;/strong&gt; it returns a new function which can be invoked later. let's implement &lt;strong&gt;bind&lt;/strong&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
  firstname: "Kirtesh",
  lastname: "bansal"
}

let printName = function (country) {
  console.log(this.firstname + " " + this.lastname + " from " 
  + country);
}

let newPrintName = printName.bind(person, "India");
newPrintName();

Output: 
"Kirtesh bansal from India"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the above code we have attached the &lt;strong&gt;bind&lt;/strong&gt; method to &lt;strong&gt;printName&lt;/strong&gt; &amp;amp; stored it into a new variable called &lt;strong&gt;newPrintName&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Now, we can call the &lt;strong&gt;newPrintName&lt;/strong&gt; any time later in code &amp;amp; it will result in the same output.&lt;/p&gt;

&lt;p&gt;Now, let's write the polyfills for all the three methods.&lt;/p&gt;

&lt;p&gt;I'll be using Javascript prototype inheritance to write the pollyfills. To make the pollyfills available to all functions.&lt;/p&gt;

&lt;p&gt;If you look at the all three methods. They are applied on a function so we will add our polyfill method to &lt;strong&gt;Function&lt;/strong&gt; prototype.&lt;/p&gt;

&lt;p&gt;You can read about prototype &lt;a href="https://javascript.info/prototypes" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;let's start with &lt;strong&gt;call&lt;/strong&gt; method polyfill.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
  firstname: "Kirtesh",
  lastname: "bansal"
}

let printName = function (country) {
  console.log(this.firstname + " " + this.lastname + " from " 
  + country);
}

Function.prototype.mycall = function(obj,...args){ 
    let sym = Symbol();                                     
    obj[sym] = this;
    let res = obj[sym](...args)
    delete obj[sym];
    return res;
}

/*
Note: Applying mycall method to printName function so this
will be equal to printName inside mycall function as 
printName is on the left side of the '.' 
*/

printName.mycall(person, "India");

Output: 
"Kirtesh bansal from India"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, I've user &lt;strong&gt;Function.prototype&lt;/strong&gt; to make &lt;strong&gt;mycall&lt;/strong&gt; method to be available for all the functions &amp;amp; assign it a new function. &lt;/p&gt;

&lt;p&gt;Same as &lt;strong&gt;call&lt;/strong&gt; method &lt;strong&gt;mycall&lt;/strong&gt; method takes object on which the method has to be invoked as first argument followed by rest of the arguments need to be passed to the function. &lt;/p&gt;

&lt;p&gt;Now, let's understand the inner implemetation of the function. &lt;/p&gt;

&lt;p&gt;Inside the &lt;strong&gt;mycall&lt;/strong&gt; function we've created a symbol &lt;strong&gt;sym&lt;/strong&gt; to create a unique property on the passed object to prevent existing property overwritten.&lt;/p&gt;

&lt;p&gt;Now, On passed object added &lt;strong&gt;sym&lt;/strong&gt; property &amp;amp; assigned the &lt;strong&gt;this&lt;/strong&gt; keyword to it. which is refering to the &lt;strong&gt;printName&lt;/strong&gt; function.&lt;/p&gt;

&lt;p&gt;In the next line, We call the function by passing the remaining arguments &amp;amp; store it's response in a new variable &lt;strong&gt;res&lt;/strong&gt;. After this, we delete the newly created property &lt;strong&gt;sym&lt;/strong&gt; from the passed object as it do not exist on the object outside this function and then we return the response of the object. &lt;/p&gt;

&lt;p&gt;So, finally we have created our first polyfill and it results the same.&lt;/p&gt;

&lt;p&gt;Let's jump on the apply method polyfill.&lt;/p&gt;

&lt;p&gt;As we have seen thay &lt;strong&gt;apply&lt;/strong&gt; is very simillar to the &lt;strong&gt;call&lt;/strong&gt; method just it takes array of arguments instead of take comma seperated list of arguments. Therefore, the implementation for &lt;strong&gt;apply&lt;/strong&gt; remains same as &lt;strong&gt;call&lt;/strong&gt; method with a minor change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
  firstname: "Kirtesh",
  lastname: "bansal"
}

let printName = function (country) {
  console.log(this.firstname + " " + this.lastname + " from " 
  + country);
}

Function.prototype.myapply = function(obj,...args){
  let sym = Symbol();                                     
  obj[sym] = this;
  let res = obj[sym](...args[0]); 
  delete obj[sym];
  return res;
}

printName.myapply(person, ["India"]);

Output: 
"Kirtesh bansal from India"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the above code the implementation steps are same but when we invoke the function on object instead of passing &lt;strong&gt;...args&lt;/strong&gt; directly as argumetns. we'll pass the 0th index of &lt;strong&gt;args&lt;/strong&gt; using rest operator because rest operator '...' represents arguments array &amp;amp; in this array we've our passed arguments array at 0th index so will pick that array &amp;amp; spread that into the function.&lt;/p&gt;

&lt;p&gt;Let's write the final bind method polyfill.&lt;/p&gt;

&lt;p&gt;If we recall from bind method implementation. we know that it is same as &lt;strong&gt;call&lt;/strong&gt; but instead of invoking the function return new function. Let's see the implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
  firstname: "Kirtesh",
  lastname: "bansal"
}

let printName = function (country) {
  console.log(this.firstname + " " + this.lastname + " from " 
  + country);
}

Function.prototype.mybind = function(object,...args){
  let func = this;
  return function (...args1) {
    return func.apply(object, [...args, ...args1]);
  }
}

let newPrintName = printName.mybind(person, "India");
newPrintName();

Output: 
"Kirtesh bansal from India"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, Same as mycall &amp;amp; myapply methods. We've created a mybind method on Function.prototype &amp;amp; assigned a function to it. This function accepts object &amp;amp; arguments simillar to &lt;strong&gt;bind&lt;/strong&gt; method. We already know form the above polyfill implementations that the &lt;strong&gt;this&lt;/strong&gt; keyword referes the function. In case on bind, we'll store the &lt;strong&gt;this&lt;/strong&gt; on a variable called &lt;strong&gt;func&lt;/strong&gt;. since &lt;strong&gt;bind&lt;/strong&gt; returns a new function. we'll also return a anonymous function which will act as closure in &lt;strong&gt;mybind&lt;/strong&gt; function. Now, this returing function can also accepts arguments which will represent the arguments passed during the invocation of new function returned by &lt;strong&gt;mybind&lt;/strong&gt; method. Inside this returning function, we will use &lt;strong&gt;apply&lt;/strong&gt; method on the func variable to invoke it for passed object &amp;amp; arguments. In this apply method, we'll create an array to pass arguments &amp;amp; in this array we'll spread the &lt;strong&gt;args&lt;/strong&gt; &amp;amp; &lt;strong&gt;args1&lt;/strong&gt; to pass all the arguments to the function &amp;amp; store it in a new variable &lt;strong&gt;newPrintName&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Later, when we call this &lt;strong&gt;newPrintName&lt;/strong&gt;. It results the same. If we pass any argument in &lt;strong&gt;newPrintName&lt;/strong&gt; function, &lt;strong&gt;args1&lt;/strong&gt; represents these arguments in mybind method.&lt;/p&gt;

&lt;p&gt;That's all about call, apply, bind &amp;amp; their polyfills.&lt;/p&gt;

&lt;p&gt;Do share your comments and feedback about the article.&lt;/p&gt;

&lt;p&gt;Happy learing!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>polyfill</category>
      <category>interview</category>
      <category>es6</category>
    </item>
    <item>
      <title>Create a Spinner Loader in React using CSS</title>
      <dc:creator>Kirtesh Bansal</dc:creator>
      <pubDate>Sun, 10 Oct 2021 09:48:48 +0000</pubDate>
      <link>https://dev.to/kirteshbansal/spinner-loader-in-react-using-css-458h</link>
      <guid>https://dev.to/kirteshbansal/spinner-loader-in-react-using-css-458h</guid>
      <description>&lt;p&gt;Hi Folks,&lt;/p&gt;

&lt;p&gt;In this article, we'll talk about how to create a spinner loader in React using pure CSS.&lt;/p&gt;

&lt;p&gt;We will be creating a loader as shown below -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F3l26pl6ovfgvixgwvw8i.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3l26pl6ovfgvixgwvw8i.gif" alt="CPT2110092208-232x179" width="232" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's start creating a file called &lt;strong&gt;spinner.js&lt;/strong&gt; in the react project and add some JSX to it for the loader.&lt;/p&gt;

&lt;h6&gt;
  
  
  File: Spinner.js
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Spinner = () =&amp;gt; &amp;lt;div className="loader"&amp;gt;&amp;lt;/div&amp;gt;;

export default Spinner;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand the above code -&lt;/p&gt;

&lt;p&gt;Here, we have created a functional component called Spinner. Which is returning a div element with a class &lt;code&gt;loader&lt;/code&gt;. That's all the JSX required for it.&lt;/p&gt;

&lt;p&gt;Now, Let's add css for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.loader {
  border: 10px solid #f3f3f3;
  border-top: 10px solid #3498db;
  border-radius: 50%;
  width: 80px;
  height: 80px;
  animation: spin 1s linear infinite;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand the above CSS -&lt;/p&gt;

&lt;p&gt;We've added &lt;code&gt;border:10px solid #f3f3f3&lt;/code&gt; property to create the gray circle and &lt;code&gt;border-top: 10px solid #3498db&lt;/code&gt; to create the blue colored arc on top of the gray colored circle. After that &lt;code&gt;border-radius: 50%&lt;/code&gt; property to make it circular and have given same amount of height &amp;amp; width. &lt;/p&gt;

&lt;p&gt;Now, to add the spinning effect we've added &lt;code&gt;animation: spin 1s linear infinite&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;It will look like this -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fx5e5gb6l14oveaodidvn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fx5e5gb6l14oveaodidvn.png" alt="s" width="191" height="139"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's add the final CSS to create the spinning effect-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; @keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand the final CSS -&lt;/p&gt;

&lt;p&gt;Here, we have added keyframes for the spin animation. Where we are rotating the element using &lt;code&gt;transform: rotate()&lt;/code&gt; property from 0 degree to 360 degree.&lt;/p&gt;

&lt;p&gt;Finally, Our Sinnner loader is ready.&lt;/p&gt;

&lt;p&gt;That was it from this article. Do share your comments and feedback about the article.&lt;/p&gt;

&lt;p&gt;Find the code below.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/ozfrr"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Keep learning!&lt;/p&gt;

</description>
      <category>spinner</category>
      <category>loader</category>
      <category>react</category>
      <category>css</category>
    </item>
    <item>
      <title>Bouncing dots Loader in React</title>
      <dc:creator>Kirtesh Bansal</dc:creator>
      <pubDate>Sun, 26 Sep 2021 17:02:07 +0000</pubDate>
      <link>https://dev.to/kirteshbansal/bouncing-dots-loader-in-react-4jng</link>
      <guid>https://dev.to/kirteshbansal/bouncing-dots-loader-in-react-4jng</guid>
      <description>&lt;p&gt;Hi Folks,&lt;/p&gt;

&lt;p&gt;Loading component is a very common requirement in react to represent the loading state. In this tutorial, I will explain how you can create bouncing dots loader using CSS.&lt;/p&gt;

&lt;p&gt;We will be creating a loader as shown below -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fygbp638kpfa4c1ad9kab.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fygbp638kpfa4c1ad9kab.gif" alt="CPT2109251455-297x146" width="297" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's get started -&lt;/p&gt;

&lt;p&gt;For this tutorial, I'm assuming you already have a React project setup ready with you.&lt;/p&gt;

&lt;p&gt;Now, let's create a &lt;strong&gt;BouncingDotsLoader&lt;/strong&gt; component in React and add JSX to create those three dots. &lt;/p&gt;

&lt;h6&gt;
  
  
  BouncingDotsLoader.js
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

const BouncingDotsLoader = (props) =&amp;gt; {
  return (
      &amp;lt;div className="bouncing-loader"&amp;gt;
        &amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
  );
};

export default BouncingDotsLoader;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand the above code-&lt;/p&gt;

&lt;p&gt;Here, I have created an outer div container to wrap all the &lt;br&gt;
three identical dots with class &lt;code&gt;bouncing-loader&lt;/code&gt;. To create these three dots, I've added three div element inside this container. &lt;/p&gt;

&lt;p&gt;Now, Let's add CSS to style the dots-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.bouncing-loader {
  display: flex;
  justify-content: center;
}

.bouncing-loader &amp;gt; div {
  width: 16px;
  height: 16px;
  margin: 3px 6px;
  border-radius: 50%;
  background-color: #a3a1a1;
  opacity: 1;
  animation: bouncing-loader 0.6s infinite alternate;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand the above CSS -&lt;/p&gt;

&lt;p&gt;I've added &lt;code&gt;display: flex&lt;/code&gt; and &lt;code&gt;justify-content: center&lt;/code&gt; to &lt;code&gt;bouncing-loader&lt;/code&gt; class to make those dots inline &amp;amp; center. Also, added CSS to &lt;code&gt;bouncing-loader &amp;gt; div&lt;/code&gt; to style each dot with an &lt;code&gt;animation&lt;/code&gt; shortand property with value &lt;code&gt;bouncing-loader 0.6s infinite alternate&lt;/code&gt; representing CSS properties &lt;code&gt;animation-name animation-duration iteration-count animation-direction&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It will look like this -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Finx4ocuo6j4c6v9tuexw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Finx4ocuo6j4c6v9tuexw.png" alt="S" width="194" height="91"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Let's add the final CSS to create the bouncing effect-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@keyframes bouncing-loader {
  to {
    opacity: 0.1;
    transform: translateY(-16px);
  }
}

.bouncing-loader &amp;gt; div:nth-child(2) {
  animation-delay: 0.2s;
}

.bouncing-loader &amp;gt; div:nth-child(3) {
  animation-delay: 0.4s;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand the final CSS - &lt;/p&gt;

&lt;p&gt;I've added keyframes to the bouncing-loader animation at start point with &lt;code&gt;opacity: 0.1&lt;/code&gt; &amp;amp; &lt;code&gt;transform: translateY(-16px)&lt;/code&gt; &amp;amp; delayed the animation for second and third dot with 0.2 &amp;amp; 0.4 seconds respectively.&lt;/p&gt;

&lt;p&gt;This way, Our very simple bouncing dots loader is ready.&lt;/p&gt;

&lt;p&gt;That was it from this article. Do share your comments and feedback about the article.&lt;/p&gt;

&lt;p&gt;Find the code below.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/v13s4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Keep learning!&lt;/p&gt;

</description>
      <category>react</category>
      <category>loader</category>
      <category>css</category>
      <category>bouncing</category>
    </item>
  </channel>
</rss>
