<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>https://web.dev/</id>
  <title>Umar Hansa on web.dev</title>
  <updated>2026-04-15T23:21:06Z</updated>
  <author>
    <name>Umar Hansa</name>
  </author>
  <link href="https://web.dev/authors/umarhansa/feed.xml" rel="self"/>
  <link href="https://web.dev/"/>
  <icon>https://web-dev.imgix.net/image/eHjLPdOn1XcKYDm76GytojNeEkZ2/C0zDh9w4i3wZ8oOunJvR.jpg?auto=format</icon>
  <logo>https://web.dev/images/shared/rss-banner.png</logo>
  <subtitle>Umar is a front-end web developer</subtitle>
  
  
  <entry>
    <title>Choose a JavaScript library or framework</title>
    <link href="https://web.dev/choose-js-library-or-framework/"/>
    <updated>2022-05-23T00:00:00Z</updated>
    <id>https://web.dev/choose-js-library-or-framework/</id>
    <content type="html" mode="escaped">&lt;p&gt;This article shares insights on how you might pick a library or framework to use within your web application. The discussions herein will help you to weigh the pros and cons in finding the JavaScript library or framework that is right for the business problem you&#39;re trying to solve. Understanding which pros and which cons apply in different situations is key to vetting the large number of JavaScript library choices that are available.&lt;/p&gt;
&lt;h2 id=&quot;what-are-javascript-libraries-and-frameworks&quot;&gt;What are JavaScript libraries and frameworks &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#what-are-javascript-libraries-and-frameworks&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;What is a JavaScript library? In its simplest form, a JavaScript library is prewritten code that you can call in your project&#39;s code to achieve a specific task.&lt;/p&gt;
&lt;p&gt;This post mainly mentions &amp;quot;libraries&amp;quot;. However, many of the discussions are also applicable to frameworks. Basically, the difference between the two can be summarized as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For a library, your application code calls the library code.&lt;/li&gt;
&lt;li&gt;For a framework, your application code is called by the framework.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The following practical examples help to illustrate the differences.&lt;/p&gt;
&lt;h3 id=&quot;example-call-to-a-javascript-library&quot;&gt;Example call to a JavaScript library &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#example-call-to-a-javascript-library&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A JavaScript library performs a specific task and then returns control to your application. When you use a library, you control the application flow and choose when to call the library.&lt;/p&gt;
&lt;p&gt;In the following example, application code imports a method from the &lt;a href=&quot;https://lodash.com/&quot; rel=&quot;noopener&quot;&gt;lodash&lt;/a&gt; library. After the function completes, control is returned to your application.&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; capitalize &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;lodash.capitalize&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token function&quot;&gt;capitalize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;hello&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Hello&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;When the &lt;a href=&quot;https://www.npmjs.com/package/lodash.capitalize&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;lodash.capitalize&lt;/code&gt;&lt;/a&gt; method is executed, it calls pre-written JavaScript code that capitalizes the first character of a string.&lt;/p&gt;
&lt;h3 id=&quot;example-use-of-a-javascript-framework&quot;&gt;Example use of a JavaScript Framework &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#example-use-of-a-javascript-framework&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A JavaScript framework is a predefined code template within which you construct your application&#39;s behavior. That is, when you use a framework, the framework controls the application flow. To use a framework, you write your custom application code, and then the framework calls your application code.&lt;/p&gt;
&lt;p&gt;The following example shows a code snippet that uses the &lt;a href=&quot;https://preactjs.com/&quot; rel=&quot;noopener&quot;&gt;Preact&lt;/a&gt; JavaScript framework:&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; createElement &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;preact&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;p &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;big&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;Hello World&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;p&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;In the example, notice that the framework has a lot more control over the code you write, and in some cases, the framework even takes control over when to execute your code.&lt;/p&gt;
&lt;h2 id=&quot;why-use-a-library&quot;&gt;Why use a library? &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#why-use-a-library&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Using a JavaScript library can help to avoid unnecessary &lt;a href=&quot;https://en.wikipedia.org/wiki/Don%27t_repeat_yourself&quot; rel=&quot;noopener&quot;&gt;code repetition&lt;/a&gt;. Libraries can abstract away complex logic, such as date manipulation or financial calculations. A library can also help get your &lt;a href=&quot;https://en.wikipedia.org/wiki/Minimum_viable_product&quot; rel=&quot;noopener&quot;&gt;initial product&lt;/a&gt; out, rather than having to write all the code from scratch, which can take time.&lt;/p&gt;
&lt;p&gt;Some client-side JavaScript libraries help abstract away quirks of the web platform. A library can also serve as a learning tool. For example, if you&#39;re unfamiliar with &lt;a href=&quot;https://easings.net/&quot; rel=&quot;noopener&quot;&gt;animation easing functions&lt;/a&gt;, the source code of a library can teach you how such easings work.&lt;/p&gt;
&lt;p&gt;Some libraries are backed by large companies that invest time and money into keeping libraries up to date and secure. Many libraries are accompanied by extensive documentation, which offers you and your team a quick way to familiarize yourself with the library&#39;s usage.&lt;/p&gt;
&lt;p&gt;Ultimately, using a JavaScript library saves you time.&lt;/p&gt;
&lt;h2 id=&quot;why-should-you-care-about-library-usage&quot;&gt;Why should you care about library usage? &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#why-should-you-care-about-library-usage&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Technically, you can develop your web application from scratch, but why go to the trouble when you can use free (open source) software, or purchase a solution that, in the long run, can save time and money? There are a large number of JavaScript libraries and frameworks available, each offering a unique approach to solving problems, and each having different characteristics. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A library can be written and maintained internally rather than by a third party.&lt;/li&gt;
&lt;li&gt;A library can have specific legal licenses that make it suitable or unsuitable for your web application.&lt;/li&gt;
&lt;li&gt;A library can be outdated or unmaintained.&lt;/li&gt;
&lt;li&gt;A library can simplify a set of complex tasks and save you a lot of time and money.&lt;/li&gt;
&lt;li&gt;A library can be widely used in the community, and can be well known amongst developers.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you might suspect, different characteristics can affect your web application in different ways. Sometimes, the decision is just not that deep, and you can safely swap out a library if you don&#39;t like it. However, sometimes a library can have a significant effect on your work and your web application, which suggests a more informed approach could be necessary.&lt;/p&gt;
&lt;aside class=&quot;aside flow bg-tertiary-box-bg color-tertiary-box-text&quot;&gt;&lt;p class=&quot;cluster &quot;&gt;&lt;span class=&quot;aside__icon box-block &quot;&gt;&lt;svg width=&quot;24&quot; height=&quot;24&quot; viewBox=&quot;0 0 24 24&quot; role=&quot;img&quot; aria-label=&quot;Lightbulb&quot; fill=&quot;currentColor&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;   &lt;path d=&quot;M9 21c0 .55.45 1 1 1h4c.55 0 1-.45 1-1v-1H9v1zm3-19C8.14 2 5 5.14 5 9c0 2.38 1.19 4.47 3 5.74V17c0 .55.45 1 1 1h6c.55 0 1-.45 1-1v-2.26c1.81-1.27 3-3.36 3-5.74 0-3.86-3.14-7-7-7zm2.85 11.1l-.85.6V16h-4v-2.3l-.85-.6A4.997 4.997 0 017 9c0-2.76 2.24-5 5-5s5 2.24 5 5c0 1.63-.8 3.16-2.15 4.1z&quot;&gt;&lt;/path&gt; &lt;/svg&gt;&lt;/span&gt;&lt;strong&gt;Important&lt;/strong&gt;&lt;/p&gt;&lt;div class=&quot; flow&quot;&gt; Many of the points raised in the following performance section—and also points raised throughout this article—are applicable to client-side environments. Client-side code is run on the end user&#39;s device, such as through their web browser. &lt;/div&gt;&lt;/aside&gt;
&lt;p&gt;There are some non client-side JavaScript environments, such as on the server (run in a cloud environment) or on a Raspberry Pi, where you may need to adjust the criteria you use to vet libraries and frameworks.&lt;/p&gt;
&lt;h2 id=&quot;performance&quot;&gt;Performance &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#performance&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The performance effect of a JavaScript library on a client-side web application should not be ignored. A large JavaScript library can disrupt the loading performance of your page;  remember, &lt;a href=&quot;https://web.dev/milliseconds-make-millions/&quot;&gt;milliseconds make millions&lt;/a&gt;.&lt;/p&gt;
&lt;aside class=&quot;aside flow bg-state-info-bg color-state-info-text&quot;&gt;&lt;div class=&quot; flow&quot;&gt; Many JavaScript libraries are open source, and the efforts of the library maintainers cannot be overstated—open source is not easy. &lt;/div&gt;&lt;/aside&gt;
&lt;p&gt;Consider a scenario where you use a JavaScript library for animation. Some libraries can easily add tens of kilobytes, and in some cases, even hundreds of kilobytes. JavaScript resources like this can add a significant delay to your page load as the browser needs to &lt;a href=&quot;https://web.dev/optimizing-content-efficiency-javascript-startup-optimization/&quot;&gt;download, parse, compile and execute&lt;/a&gt; the code.&lt;/p&gt;
&lt;p&gt;The bigger the JavaScript library, the bigger the performance effect on your users.&lt;/p&gt;
&lt;p&gt;When evaluating or using a JavaScript library or framework, consider the following suggestions to improve performance:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Given a large JavaScript library, consider using a smaller alternative. For example, &lt;a href=&quot;https://date-fns.org/&quot; rel=&quot;noopener&quot;&gt;date-fns&lt;/a&gt; offers a lot of functionality at a more reasonable size than some other options.&lt;/li&gt;
&lt;li&gt;Following on from the previous date-fns example, import only the functions that you need, such as: &lt;code&gt;import { format } from &#39;date-fns&#39;&lt;/code&gt;. Be sure to combine this approach with &lt;a href=&quot;https://web.dev/reduce-javascript-payloads-with-tree-shaking/&quot;&gt;tree shaking&lt;/a&gt;, so that a minimal JavaScript payload is built and sent to your users.&lt;/li&gt;
&lt;li&gt;Use performance testing tools such as &lt;a href=&quot;https://developer.chrome.com/docs/lighthouse/overview/&quot; rel=&quot;noopener&quot;&gt;Lighthouse&lt;/a&gt; to observe the performance effect of using a certain JavaScript library. If a library adds a one-second delay to your page load time (don&#39;t forget to &lt;a href=&quot;https://developer.mozilla.org/docs/Glossary/Network_throttling&quot; rel=&quot;noopener&quot;&gt;throttle&lt;/a&gt; your network and CPU during testing), then you may need to reevaluate your library of choice. In addition to checking page load, be sure to profile any web page behavior that invokes code from the library in question—page load performance does not tell the full story.&lt;/li&gt;
&lt;li&gt;If comments are welcomed by the library author, then submit your performance observations, suggestions, and even contributions to the project. This is where the open source community shines! If you decide to make a contribution, then you may need to check with your employer first.&lt;/li&gt;
&lt;li&gt;Use an automated bundle tracking tool, such as &lt;a href=&quot;https://github.com/siddharthkp/bundlesize&quot; rel=&quot;noopener&quot;&gt;bundlesize&lt;/a&gt;, to watch for unexpectedly large updates to a library. It&#39;s common that a JavaScript library will grow over time. Feature additions, bug fixes, edge cases, and others, can all add to the file size of a library. Once you/your team have agreed to use a library, updating the library may be less of an issue and could raise little-to-no questions. This is where it&#39;s helpful to rely on automation.&lt;/li&gt;
&lt;li&gt;Look at your requirements for a library and evaluate whether or not the web platform offers the same functionality natively. For example, the web platform already offers a &lt;a href=&quot;https://codepen.io/una/pen/BLxjoo&quot; rel=&quot;noopener&quot;&gt;color picker&lt;/a&gt;, which removes the need to use a third-party JavaScript library to implement the same functionality.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;security&quot;&gt;Security &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#security&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Using a third-party module carries some inherent security risks. A malicious package within your web application codebase can compromise the security of both your development team and your users.&lt;/p&gt;
&lt;p&gt;Consider a library published to the NPM ecosystem. Such a package may be legitimate. However, over time, the package can be compromised.&lt;/p&gt;
&lt;p&gt;Here are some security tips to consider when using or evaluating third-party code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you use GitHub, then consider the code&#39;s security &lt;a href=&quot;https://github.com/features/security&quot; rel=&quot;noopener&quot;&gt;offerings&lt;/a&gt;, such as &lt;a href=&quot;https://docs.github.com/en/code-security/supply-chain-security/keeping-your-dependencies-updated-automatically/about-dependabot-version-updates&quot; rel=&quot;noopener&quot;&gt;Dependabot&lt;/a&gt;. Or, consider alternative services that scan for vulnerabilities in your code, such as &lt;a href=&quot;https://snyk.io/&quot; rel=&quot;noopener&quot;&gt;snyk.io&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Consider using code-auditing services, a team of engineers who can manually audit the third-party code you&#39;re using.&lt;/li&gt;
&lt;li&gt;Evaluate whether you should lock your dependencies to a specific version, or commit your third-party code within your version control. This can help lock your dependency to one particular version—that is presumably deemed safe. Ironically, this can have a counter-effect within security, as you may miss out on vital updates to the library.&lt;/li&gt;
&lt;li&gt;Scan the project home page, or GitHub page, if one exists. Research whether outstanding security issues exist, and whether previous security issues were resolved within a reasonable timeframe.&lt;/li&gt;
&lt;li&gt;Third-party code that uses other third-party code can carry more risk than a library that has &lt;a href=&quot;https://github.com/topics/zero-dependency&quot; rel=&quot;noopener&quot;&gt;zero dependencies&lt;/a&gt;. Be mindful of this risk.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;accessibility&quot;&gt;Accessibility &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#accessibility&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You may be wondering how software libraries are related to web accessibility. While a software library can be used in different environments, in the context of a client-side JavaScript-based library, &lt;a href=&quot;https://web.dev/accessible/&quot;&gt;web accessibility&lt;/a&gt; is of high importance.&lt;/p&gt;
&lt;p&gt;A client-side JavaScript-based library (or framework, for that matter) can increase or decrease the accessibility of your website. Consider a third-party JavaScript library that adds an image slider to a page. If the image slider does not account for web accessibility, you as the web developer may overlook such an important feature, and release a product that misses critical features, such as the slider being keyboard navigable!&lt;/p&gt;
&lt;aside class=&quot;aside flow bg-state-info-bg color-state-info-text&quot;&gt;&lt;div class=&quot; flow&quot;&gt; In the previous example, it is the responsibility of the web developer building the website to make sure they use libraries that are fit for purpose. It is not the responsibility of the library author. This image slider is one particular example, but do consider other JavaScript library examples: &lt;/div&gt;&lt;/aside&gt;
&lt;ul&gt;
&lt;li&gt;Does the responsive typography plugin support users who zoom in or out of the page?&lt;/li&gt;
&lt;li&gt;Does the file uploader plugin support file uploads from assistive devices?&lt;/li&gt;
&lt;li&gt;Does the animation library offer support for users who &lt;a href=&quot;https://web.dev/prefers-reduced-motion/&quot;&gt;prefer reduced motion&lt;/a&gt;?&lt;/li&gt;
&lt;li&gt;Does the interactive maps plugin support keyboard-only usage?&lt;/li&gt;
&lt;li&gt;Does the audio player library offer an appropriate experience on screen readers?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It&#39;s reasonable to expect that some level of involvement is needed from you, the web developer, to meet such accessibility requirements. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For any missing features, you can implement such features within your codebase, even while continuing to use the library in question.&lt;/li&gt;
&lt;li&gt;With the support of your employer, you can contribute such a missing feature to the library, if the library author allows for such a contribution.&lt;/li&gt;
&lt;li&gt;You can open up a dialogue with the library author. For example, are these specific accessibility features on your roadmap? Do you agree they belong in the library?&lt;/li&gt;
&lt;li&gt;For popular use cases, you can explore alternative library options that are more accessible; they may exist but are harder to find.&lt;/li&gt;
&lt;li&gt;In the extreme case, you may need to ditch a library entirely and implement your features from scratch. This can happen when a library or framework has a degraded accessibility experience on initial use, and you need to undo a lot of what the library or framework is supposedly giving you for free.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conventions&quot;&gt;Conventions &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#conventions&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A software library that uses established &lt;a href=&quot;https://en.wikipedia.org/wiki/Coding_conventions&quot; rel=&quot;noopener&quot;&gt;coding conventions&lt;/a&gt; is easier to work with. If a library uses a coding convention that is unheard of, then it may be difficult for you and your team to work with such a library.&lt;/p&gt;
&lt;p&gt;If a library does not follow common coding conventions (for example, a common style guide), there is not much you can do as an immediate fix. However, there are still a few options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Be sure to differentiate between the library source code and the API exposed to you, the library user. While the internal source code may use unfamiliar conventions, if the API (the part of the library you interact with) uses familiar conventions, then there may be nothing to worry about.&lt;/li&gt;
&lt;li&gt;If the library API does not follow common coding conventions, you can use a JavaScript design pattern, like the &lt;a href=&quot;https://www.patterns.dev/posts/proxy-pattern/&quot; rel=&quot;noopener&quot;&gt;proxy pattern&lt;/a&gt;, to wrap and contain all interactions with the library to a single file in the codebase. Your proxy can then offer a more intuitive API to other parts of the code within your codebase.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Conventions play a big role with ease of use. A library that includes an intuitive API can save many hours&#39;, or even days&#39; worth of people hours, when compared with a counter-intuitive API that needs a lot of experimentation to figure out.&lt;/p&gt;
&lt;h2 id=&quot;updates&quot;&gt;Updates &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#updates&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As an example, for a fully working library that performs a few mathematical calculations, such a library may rarely need updates. In fact, a feature-complete library is a rare find in the ever-changing world of web development! However, there are times where you want the library author to be responsive and willing to make updates. New research and findings can reveal better ways of doing things, so the techniques used in libraries and frameworks are always subject to change.&lt;/p&gt;
&lt;p&gt;When you pick a library or framework, pay attention to how updates are handled, and be aware that such decisions can affect you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Does the library have a sensible release schedule? For example, updates to the source code repository may happen frequently, but if such updates are not &amp;quot;published&amp;quot; or &amp;quot;released&amp;quot; accordingly, you&#39;ll find it can be a struggle to download such updates.&lt;/li&gt;
&lt;li&gt;Does the library release updates with a sensible &lt;a href=&quot;https://en.wikipedia.org/wiki/Software_versioning&quot; rel=&quot;noopener&quot;&gt;software versioning&lt;/a&gt; scheme? A library should save you time. If you have to unexpectedly change your code every time you update the library version, this may defeat the purpose of using that library in the first place. Breaking changes are sometimes unavoidable, but in an ideal world, changes are infrequent and not forced upon library consumers.&lt;/li&gt;
&lt;li&gt;Does the library invest effort towards &lt;a href=&quot;https://en.wikipedia.org/wiki/Backward_compatibility&quot; rel=&quot;noopener&quot;&gt;backward compatibility&lt;/a&gt;? Sometimes, software updates can come with breaking changes, but also provide a layer of backward compatibility. This allows the library consumer to use the latest library version with minimal changes to their code.&lt;/li&gt;
&lt;/ul&gt;
&lt;aside class=&quot;aside flow bg-state-info-bg color-state-info-text&quot;&gt;&lt;div class=&quot; flow&quot;&gt; Library authors and maintainers invest huge amounts of time supporting web developers. It&#39;s not always practical for a library author to provide updates in the ways previously discussed. For example, independent library authors who work on open source may not even receive the financial support they need to keep up with the demands of the project.  &lt;/div&gt;&lt;/aside&gt;
&lt;h2 id=&quot;licensing&quot;&gt;Licensing &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#licensing&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;aside class=&quot;aside flow bg-state-warn-bg color-state-warn-text&quot;&gt;&lt;p class=&quot;cluster &quot;&gt;&lt;span class=&quot;aside__icon box-block color-state-warn-text&quot;&gt;&lt;svg width=&quot;24&quot; height=&quot;24&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;currentColor&quot; role=&quot;img&quot; aria-label=&quot;Warning sign&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;   &lt;path fill-rule=&quot;evenodd&quot; clip-rule=&quot;evenodd&quot; d=&quot;M23 21L12 2 1 21h22zm-12-3v-2h2v2h-2zm0-4h2v-4h-2v4z&quot;&gt;&lt;/path&gt; &lt;/svg&gt;&lt;/span&gt;&lt;strong&gt;Warning&lt;/strong&gt;&lt;/p&gt;&lt;div class=&quot; flow&quot;&gt; This is not legal advice. &lt;/div&gt;&lt;/aside&gt;
&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Software_license&quot; rel=&quot;noopener&quot;&gt;Software licensing&lt;/a&gt; is an important aspect of using third-party software libraries. A library author may assign a license to their library. If you are considering using the library, then their &lt;a href=&quot;https://choosealicense.com/&quot; rel=&quot;noopener&quot;&gt;choice of license&lt;/a&gt; may affect you.&lt;/p&gt;
&lt;p&gt;For example, a JavaScript library may have a software license that permits you to use it in a non-commercial environment. For a personal hobby project, this could be a great choice. If your project has a commercial element, then you may need to consider an enterprise license.&lt;/p&gt;
&lt;p&gt;When in doubt, consider seeking professional legal advice or defer to the legal team within your company.&lt;/p&gt;
&lt;h2 id=&quot;community&quot;&gt;Community &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#community&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A library or framework that has a large community of users/contributors can be beneficial, but this isn&#39;t a guarantee. In general, the more users a library or framework has, the more likely it is to benefit. Consider the following pros and cons to participation in a development community:&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A large user base could mean a greater chance of bugs being caught early and often.&lt;/li&gt;
&lt;li&gt;A large active community could mean more tutorials, guides, videos, and even courses, on the library or framework in question.&lt;/li&gt;
&lt;li&gt;A large active community could mean more support on forums and question and answer websites, increasing the likelihood that support questions are answered.&lt;/li&gt;
&lt;li&gt;An engaged community can mean more external contributors to the library or framework. They can help deliver features that are otherwise not on the author&#39;s roadmap.&lt;/li&gt;
&lt;li&gt;When a library or framework is popular within a community, there&#39;s an increased likelihood that your peers and colleagues will have heard of, or even be familiar with, such a library or framework.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A project with a large and diverse user base can become bloated from constant feature additions. Bloated libraries can harm web performance.&lt;/li&gt;
&lt;li&gt;A project with an active and engaged community can be stressful for the authors and maintainers, and may require heavy community moderation.&lt;/li&gt;
&lt;li&gt;A project that grows rapidly, but does not have the appropriate support in place, can begin to exhibit signs of having a toxic community. As an example, beginner or junior web developers can be made to feel unwelcome in a certain community due to gatekeeping.&lt;/li&gt;
&lt;/ul&gt;
&lt;aside class=&quot;aside flow bg-state-info-bg color-state-info-text&quot;&gt;&lt;div class=&quot; flow&quot;&gt; The web development community is, by far, wonderful! Every community has its struggles, but you should not feel dissuaded from engaging with a particular community based on what you read here. It can be especially challenging for a new web developer to feel part of a community, especially those communities with polarizing views. You should know that this is a common challenge within the web community, and is an area in which more open conversation is needed. &lt;/div&gt;&lt;/aside&gt;
&lt;h2 id=&quot;documentation&quot;&gt;Documentation &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#documentation&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;No matter how simple or complex a JavaScript library or framework might be, &lt;a href=&quot;https://en.wikipedia.org/wiki/Software_documentation&quot; rel=&quot;noopener&quot;&gt;software documentation&lt;/a&gt; can always help. Even very experienced developers use documentation, rather than figuring out the code themselves. Documentation clarifies the API you should use, and how you should use it.&lt;/p&gt;
&lt;p&gt;Documentation can even provide sample code, making it easier for you to get started quickly. When you evaluate a library or framework, you can ask some of these questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Does the library include documentation? If it does not, you&#39;ll need to be comfortable with figuring things out on your own.&lt;/li&gt;
&lt;li&gt;Is the documentation clear, easy to understand, and free of ambiguity? Many developers spend large amounts of time on documentation. It may seem small, but clarity within textual documentation can have a big effect on your productivity.&lt;/li&gt;
&lt;li&gt;Is the documentation completely automatically generated? Such documentation can be harder to digest and does not always provide clear guidance on how to use an API.&lt;/li&gt;
&lt;li&gt;Is the documentation up to date? Documentation maintenance is sometimes treated as an afterthought. If the library is updated but the documentation is not, this can lead to wasted development time.&lt;/li&gt;
&lt;li&gt;Is the documentation comprehensive and available in multiple formats? User guides, sample code, reference documentation, live demos, and tutorials are all valuable documentation formats that can help you be successful in using a library or framework.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Documentation cannot always be complete, and that&#39;s ok. You&#39;ll need to assess the needs of your organization, your project requirements, and the complexity of your software, and use that to determine the level of documentation you need.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/choose-js-library-or-framework/#conclusion&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;It&#39;s normal to feel &lt;a href=&quot;https://en.wikipedia.org/wiki/Analysis_paralysis&quot; rel=&quot;noopener&quot;&gt;overwhelmed&lt;/a&gt; when picking a library or framework for the first time. Just like everything else, the more you learn and practice a task, the better you become. It might be helpful to refer to this post when you next choose a library or framework to use. You can use the headings within this post as a checklist. For example: Is this library performant? Does this library meet my business standards for web accessibility?&lt;/p&gt;
&lt;p&gt;There are other aspects of libraries and frameworks that you may wish to consider, and that have not been heavily discussed this post:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Extendibility:&lt;/strong&gt; how easy is it to extend the library with custom logic and/or behavior?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tooling:&lt;/strong&gt; if applicable, does the library have tooling such as code editor plugins, debugging tools, and build system plugins?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Architecture:&lt;/strong&gt; clean code is important, but is the overall &lt;a href=&quot;https://en.wikipedia.org/wiki/Software_architecture&quot; rel=&quot;noopener&quot;&gt;architecture&lt;/a&gt; of the library sensible?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tests:&lt;/strong&gt; does the project have a test suite? Does the project website use badges or indicators which the test suite is passing against the latest commit?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compatibility:&lt;/strong&gt; does the library work well with other libraries and/or frameworks you are currently using?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cost:&lt;/strong&gt; what is the cost of a framework? Is it open-source or available for purchase?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vanity metrics:&lt;/strong&gt; this should be low down in your criteria list, or even ignored entirely, but you may wish to consider project &amp;quot;votes&amp;quot;, social media accounts that represent the project, and/or how many open bugs/issues there are on the project page.&lt;/li&gt;
&lt;/ul&gt;
</content>
    <author>
      <name>Umar Hansa</name>
    </author>
  </entry>
  
  <entry>
    <title>Implement error handling when using the Fetch API</title>
    <link href="https://web.dev/fetch-api-error-handling/"/>
    <updated>2022-05-23T00:00:00Z</updated>
    <id>https://web.dev/fetch-api-error-handling/</id>
    <content type="html" mode="escaped">&lt;p&gt;This article demonstrates some error handling approaches when working with the &lt;a href=&quot;https://web.dev/learn/pwa/serving/&quot;&gt;Fetch API&lt;/a&gt;. The Fetch API lets you make a request to a remote network resource. When you make a remote network call, your web page becomes subject to a variety of potential network errors.&lt;/p&gt;
&lt;p&gt;The following sections describe potential errors and describe how to write code that provides a sensible level of functionality that is resilient to errors and unexpected network conditions. Resilient code keeps your users happy and maintains a standard level of service for your website.&lt;/p&gt;
&lt;h2 id=&quot;anticipate-potential-network-errors&quot;&gt;Anticipate potential network errors &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#anticipate-potential-network-errors&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This section describes a scenario in which the user creates a new video named
&lt;code&gt;&amp;quot;My Travels.mp4&amp;quot;&lt;/code&gt; and then attempts to upload the video to a video-sharing website.&lt;/p&gt;
&lt;p&gt;When working with Fetch, it&#39;s easy to consider the &lt;a href=&quot;https://en.wikipedia.org/wiki/Happy_path&quot; rel=&quot;noopener&quot;&gt;happy path&lt;/a&gt; where the user successfully uploads the video. However, there are other paths that are not as smooth, but for which web developers must plan. Such (unhappy) paths can happen due to user error, through unexpected environmental conditions, or because of a bug on the video-sharing website.&lt;/p&gt;
&lt;h3 id=&quot;examples-of-user-errors&quot;&gt;Examples of user errors &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#examples-of-user-errors&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The user uploads an image file (such as JPEG) instead of a video file.&lt;/li&gt;
&lt;li&gt;The user begins uploading the wrong video file. Then, part way through the upload, the user specifies the correct video file for upload.&lt;/li&gt;
&lt;li&gt;The user accidentally clicks &amp;quot;Cancel upload&amp;quot; while the video is uploading.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;examples-of-environmental-changes&quot;&gt;Examples of environmental changes &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#examples-of-environmental-changes&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The internet connection goes offline while the video is uploading.&lt;/li&gt;
&lt;li&gt;The browser restarts while the video is uploading.&lt;/li&gt;
&lt;li&gt;The servers for the video-sharing website restart while the video is uploading.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;examples-of-errors-with-the-video-sharing-website&quot;&gt;Examples of errors with the video-sharing website &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#examples-of-errors-with-the-video-sharing-website&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The video-sharing website cannot handle a filename with a space. Instead of &lt;code&gt;&amp;quot;My Travels.mp4&amp;quot;&lt;/code&gt;, it expects a name such as &lt;code&gt;&amp;quot;My_Travels.mp4&amp;quot;&lt;/code&gt; or &lt;code&gt;&amp;quot;MyTravels.mp4&amp;quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The video-sharing website cannot upload a video that exceeds the maximum acceptable file size.&lt;/li&gt;
&lt;li&gt;The video-sharing website does not support the video codec in the uploaded video.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These examples can and do happen in the real world. You may have encountered such examples in the past! Let&#39;s pick one example from each of the previous categories, and discuss the following points:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What is the default behavior if the video-sharing service cannot handle the given example?&lt;/li&gt;
&lt;li&gt;What does the user expect to happen in the example?&lt;/li&gt;
&lt;li&gt;How can we improve the process?&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;table-wrapper scrollbar&quot;&gt;
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;Action&lt;/th&gt;
      &lt;td&gt;The user begins uploading the wrong video file. Then, part way through the upload, the user specifies the correct video file for upload.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;What happens by default&lt;/th&gt;
      &lt;td&gt;The original file continues to upload in the background while the new file uploads at the same time.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;What the user expects&lt;/th&gt;
      &lt;td&gt;The user expects the original upload to stop so that no extra internet bandwidth is wasted.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;What can be improved&lt;/th&gt;
      &lt;td&gt;JavaScript cancels the Fetch request for the original file before the new file begins to upload.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;table-wrapper scrollbar&quot;&gt;
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;Action&lt;/th&gt;
      &lt;th&gt;The user loses their internet connection part way through uploading the video.&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;What happens by default&lt;/th&gt;
      &lt;td&gt;The upload progress bar appears to be stuck on 50%. Eventually, the Fetch API experiences a timeout and the uploaded data is discarded. When internet connectivity returns, the user has to reupload their file.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;What the user expects&lt;/th&gt;
      &lt;td&gt;The user expects to be notified when their file cannot be uploaded, and they expect their upload to automatically resume at 50% when they are back online.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;What can be improved&lt;/th&gt;
      &lt;td&gt;The upload page informs the user of internet connectivity issues, and reassures the user that the upload will resume when internet connectivity has resumed.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;table-wrapper scrollbar&quot;&gt;
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;Action&lt;/th&gt;
      &lt;th&gt;The video-sharing website cannot handle a filename with a space. Instead of &quot;My Travels.mp4&quot;, it expects names such as &quot;My_Travels.mp4&quot; or &quot;MyTravels.mp4&quot;.&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;What happens by default&lt;/th&gt;
      &lt;td&gt;The user must wait for the upload to completely finish. Once the file is uploaded, and the progress bar reads &quot;100%&quot;, the progress bar displays the message: &quot;Please try again.&quot;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;What the user expects&lt;/th&gt;
      &lt;td&gt;The user expects to be told of filename limitations before upload begins, or at least within the first second of uploading.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;row&quot;&gt;What can be improved&lt;/th&gt;
      &lt;td&gt;Ideally, the video-sharing service supports filenames with spaces. Alternative options are to notify the user of filename limitations before uploading begins. Or, the video-sharing service should reject the upload with a detailed error message.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;h2 id=&quot;handle-errors-with-the-fetch-api&quot;&gt;Handle errors with the Fetch API &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#handle-errors-with-the-fetch-api&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Note that the following code examples use &lt;a href=&quot;https://v8.dev/features/top-level-await&quot; rel=&quot;noopener&quot;&gt;top-level &lt;code&gt;await&lt;/code&gt;&lt;/a&gt; (&lt;a href=&quot;https://caniuse.com/mdn-javascript_operators_await_top_level&quot; rel=&quot;noopener&quot;&gt;browser support&lt;/a&gt;) because this feature can simplify your code.&lt;/p&gt;
&lt;h3 id=&quot;when-the-fetch-api-throws-errors&quot;&gt;When the Fetch API throws errors &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#when-the-fetch-api-throws-errors&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;This example uses a &lt;a href=&quot;https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/try...catch&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; block&lt;/a&gt; statement to catch any errors thrown within the &lt;code&gt;try&lt;/code&gt; block. For example, if the Fetch API cannot fetch the specified resource, then an error is thrown. Within a &lt;code&gt;catch&lt;/code&gt; block like this, take care to provide a &lt;a href=&quot;https://developer.mozilla.org/docs/Glossary/Graceful_degradation&quot; rel=&quot;noopener&quot;&gt;meaningful&lt;/a&gt; user experience. If a spinner, a common user interface that represents some sort of progress, is shown to the user, then you could take the following actions within a &lt;code&gt;catch&lt;/code&gt; block:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Remove the spinner from the page.&lt;/li&gt;
&lt;li&gt;Provide helpful messaging that explains what went wrong, and what options the user can take.&lt;/li&gt;
&lt;li&gt;Based on the available options, present a &amp;quot;Try again&amp;quot; button to the user.&lt;/li&gt;
&lt;li&gt;Behind the scenes, send the details of the error to your error-tracking service, or to the back-end. This action logs the error so it can be diagnosed at a later stage.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;https://website&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// TypeError: Failed to fetch&lt;/span&gt;&lt;br /&gt;  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;There was an error&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;At a later stage, while you diagnose the error that you logged, you can write a &lt;a href=&quot;https://en.wikipedia.org/wiki/Test_case&quot; rel=&quot;noopener&quot;&gt;test case&lt;/a&gt; to catch such an error before your users are aware something is wrong. Depending on the error, the test could be a unit, integration, or acceptance test.&lt;/p&gt;
&lt;h3 id=&quot;when-the-network-status-code-represents-an-error&quot;&gt;When the network status code represents an error &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#when-the-network-status-code-represents-an-error&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;This code example makes a request to an HTTP testing service that always responds with the HTTP status code &lt;a href=&quot;https://developer.mozilla.org/docs/Web/HTTP/Status/429&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;429 Too Many Requests&lt;/code&gt;&lt;/a&gt;. Interestingly, the response does not reach the &lt;code&gt;catch&lt;/code&gt; block. A 404 status, amongst certain other status codes, does not return &lt;a href=&quot;https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch#checking_that_the_fetch_was_successful&quot; rel=&quot;noopener&quot;&gt;a network error&lt;/a&gt; but instead resolves normally.&lt;/p&gt;
&lt;p&gt;To check that the HTTP status code was successful, you can use any of the following options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use the &lt;a href=&quot;https://developer.mozilla.org/docs/Web/API/Response/ok&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;Response.ok&lt;/code&gt;&lt;/a&gt; property to determine whether the status code was in the range from &lt;code&gt;200&lt;/code&gt; to &lt;code&gt;299&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use the &lt;a href=&quot;https://developer.mozilla.org/docs/Web/API/Response/status&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;Response.status&lt;/code&gt;&lt;/a&gt; property to determine whether the response was successful.&lt;/li&gt;
&lt;li&gt;Use any other metadata, such as &lt;a href=&quot;https://developer.mozilla.org/docs/Web/API/Response/headers&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;Response.headers&lt;/code&gt;&lt;/a&gt;, to assess whether the response was successful.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; response&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;https://httpbin.org/status/429&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;There was an error&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// Uses the &#39;optional chaining&#39; operator&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token operator&quot;&gt;?.&lt;/span&gt;ok&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Use the response here!&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;HTTP Response Code: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;response&lt;span class=&quot;token operator&quot;&gt;?.&lt;/span&gt;status&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;The best practice is to work with people in your organization and team to understand potential HTTP response status codes. Backend developers, developer operations, and service engineers can sometimes provide unique insight into possible edge cases that you might not anticipate.&lt;/p&gt;
&lt;aside class=&quot;aside flow bg-state-info-bg color-state-info-text&quot;&gt;&lt;div class=&quot; flow&quot;&gt; Note from the author: I was recently scrolling through an infinite scrolling list on a website, trying to quickly reach the bottom. Eventually, the page seemed to break with no helpful messaging. Upon inspecting the network request via DevTools, it became clear that the HTTP status code error &lt;code&gt;429 Too Many Requests&lt;/code&gt; was returned. In an ideal scenario, such internal errors are surfaced to the web page interface. &lt;/div&gt;&lt;/aside&gt;
&lt;h3 id=&quot;when-there-is-an-error-parsing-the-network-response&quot;&gt;When there is an error parsing the network response &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#when-there-is-an-error-parsing-the-network-response&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;This code example demonstrates another type of error that can arise with parsing a response body. The &lt;a href=&quot;https://developer.mozilla.org/docs/Web/API/Response&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;Response&lt;/code&gt;&lt;/a&gt; interface offers convenient methods to parse different types of data, such as text or JSON. In the following code, a network request is made to an HTTP testing service that returns an HTML string as the response body. However, an attempt is made to parse the response body as &lt;a href=&quot;https://developer.mozilla.org/docs/Web/API/Response/json&quot; rel=&quot;noopener&quot;&gt;JSON&lt;/a&gt;, throwing an error.&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;https://httpbin.org/html&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;  json &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; response&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error &lt;span class=&quot;token keyword&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SyntaxError&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;// Unexpected token &amp;lt; in JSON&lt;/span&gt;&lt;br /&gt;    console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;There was a SyntaxError&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;There was an error&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;json&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Use the JSON here!&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;You must prepare your code to take in a variety of response formats, and verify that an unexpected response doesn&#39;t break the web page for the user.&lt;/p&gt;
&lt;p&gt;Consider the following scenario: You have a remote resource that returns a valid JSON response, and it is parsed successfully with the &lt;code&gt;Response.json()&lt;/code&gt; method. It may happen that the service goes down. Once down, a &lt;a href=&quot;https://developer.mozilla.org/docs/Web/HTTP/Status/500&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;500 Internal Server Error&lt;/code&gt;&lt;/a&gt; is returned. If appropriate error-handling techniques are not used during the parsing of JSON, this could break the page for the user because an unhandled error is thrown.&lt;/p&gt;
&lt;h3 id=&quot;when-the-network-request-must-be-canceled-before-it-completes&quot;&gt;When the network request must be canceled before it completes &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#when-the-network-request-must-be-canceled-before-it-completes&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;This code example uses an &lt;a href=&quot;https://developer.mozilla.org/docs/Web/API/AbortController&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;AbortController&lt;/code&gt;&lt;/a&gt; to cancel an in-flight request. An in-flight request is a network request that has started but has not completed.&lt;/p&gt;
&lt;p&gt;The scenarios where you may need to cancel an in-flight request can vary, but it ultimately depends on your use case and environment. The following code demonstrates how to pass an &lt;a href=&quot;https://developer.mozilla.org/docs/Web/API/AbortSignal&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;AbortSignal&lt;/code&gt;&lt;/a&gt; to the Fetch API. The &lt;code&gt;AbortSignal&lt;/code&gt; is attached to an &lt;code&gt;AbortController&lt;/code&gt;, and the &lt;code&gt;AbortController&lt;/code&gt; includes an &lt;a href=&quot;https://developer.mozilla.org/docs/Web/API/AbortController/abort&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;abort()&lt;/code&gt;&lt;/a&gt; method, which signifies to the browser that the network request should be canceled.&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; controller &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AbortController&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; signal &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; controller&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;signal&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// Cancel the fetch request in 500ms&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token function&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; controller&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;abort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; url &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;https://httpbin.org/delay/1&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;url&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; signal &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// DOMException: The user aborted a request.&lt;/span&gt;&lt;br /&gt;  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Error: &#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/fetch-api-error-handling/#conclusion&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One important aspect of handling errors is to define the various parts that can go wrong. For each scenario, make sure you have an appropriate fallback in place for the user. With regards to a fetch request, ask yourself questions such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What happens if the target server goes down?&lt;/li&gt;
&lt;li&gt;What happens if Fetch receives an unexpected response?&lt;/li&gt;
&lt;li&gt;What happens if the user&#39;s internet connection fails?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Depending on the complexity of your web page, you can also sketch out a flowchart which describes the functionality and user interface for different scenarios.&lt;/p&gt;
</content>
    <author>
      <name>Umar Hansa</name>
    </author>
  </entry>
  
  <entry>
    <title>The difference between JavaScript libraries and frameworks</title>
    <link href="https://web.dev/js-libraries-vs-frameworks/"/>
    <updated>2022-05-23T00:00:00Z</updated>
    <id>https://web.dev/js-libraries-vs-frameworks/</id>
    <content type="html" mode="escaped">&lt;p&gt;This article teaches you about the differences between frameworks and libraries in the context of a client-side JavaScript environment, which is the code that runs in your web browser. However, some of the points raised in this article also apply to other environments because libraries and frameworks are part of many software engineering fields, such as native mobile app development.&lt;/p&gt;
&lt;p&gt;The discussions in this post focus on the qualitative differences rather than the quantitative differences between libraries and frameworks. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Quantitative:&lt;/strong&gt; Frameworks typically adhere to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Inversion_of_control&quot; rel=&quot;noopener&quot;&gt;inversion of control&lt;/a&gt; principle.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Qualitative:&lt;/strong&gt; The framework experience can appeal more to future employers when you hunt for jobs.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;why-learn-about-libraries-and-frameworks&quot;&gt;Why learn about libraries and frameworks? &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#why-learn-about-libraries-and-frameworks&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;JavaScript library and framework usage is prolific across the web. Every other website seems to use some third-party code as part of its JavaScript resources. Web page &lt;a href=&quot;https://almanac.httparchive.org/en/2021/page-weight#fig-3&quot; rel=&quot;noopener&quot;&gt;weight is getting worse&lt;/a&gt; with time, which &lt;a href=&quot;https://timkadlec.com/remembers/2020-04-21-the-cost-of-javascript-frameworks/&quot; rel=&quot;noopener&quot;&gt;affects users&lt;/a&gt;. &lt;a href=&quot;https://almanac.httparchive.org/en/2021/javascript&quot; rel=&quot;noopener&quot;&gt;JavaScript&lt;/a&gt; is a large contributing factor toward overall page weight, and it&#39;s this same JavaScript that often comprises third-party libraries and frameworks.&lt;/p&gt;
&lt;p&gt;It&#39;s not good enough to say, &amp;quot;Stop using JavaScript frameworks,&amp;quot; because frameworks provide a big benefit to developers. Frameworks can help you to code efficiently and deliver features quickly, among other benefits. Instead, you should educate yourself so that you can make an informed decision when the time arises.&lt;/p&gt;
&lt;p&gt;&amp;quot;Should I use a library or framework today?&amp;quot; is an uncommon question to ask yourself. Libraries and frameworks are two very different things. However, libraries and frameworks are often conflated and the more knowledge you have about the two, the more likely you are to make educated decisions about their use.&lt;/p&gt;
&lt;h2 id=&quot;examples-of-libraries-and-frameworks&quot;&gt;Examples of libraries and frameworks &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#examples-of-libraries-and-frameworks&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You might notice third-party code by other names, such as widgets, plugins, polyfills, or packages. However, they all typically fall into the category of a library or a framework. Basically, the difference between the two can be summarized as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For a library, your app code calls the library code.&lt;/li&gt;
&lt;li&gt;For a framework, &lt;a href=&quot;https://en.wikipedia.org/wiki/Inversion_of_control&quot; rel=&quot;noopener&quot;&gt;your app code is called by the framework&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;library&quot;&gt;Library &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#library&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Libraries tend to be simpler than frameworks and offer a narrow scope of functionality. If you pass an input to a method and receive an output, you probably used a library.&lt;/p&gt;
&lt;p&gt;Look at this example of the &lt;a href=&quot;https://lodash.com/&quot; rel=&quot;noopener&quot;&gt;&lt;code&gt;lodash&lt;/code&gt;&lt;/a&gt; library:&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; lodash &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;lodash&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [1]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; lodash&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;capitalize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;hello&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [2]&lt;/span&gt;&lt;br /&gt;console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Hello&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;As is the case with many libraries, it&#39;s practical to read through this code and understand what it does. &lt;a href=&quot;https://en.wikipedia.org/wiki/Magic_(programming)&quot; rel=&quot;noopener&quot;&gt;There&#39;s very little magic involved&lt;/a&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;An &lt;code&gt;import&lt;/code&gt; statement imports the lodash library into the JavaScript program.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;capitalize()&lt;/code&gt; method is invoked.&lt;/li&gt;
&lt;li&gt;A single argument is passed to the method.&lt;/li&gt;
&lt;li&gt;The return value is captured in a variable.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;framework&quot;&gt;Framework &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#framework&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Frameworks tend to be bigger than libraries and contribute more to overall page weight. In fact, a framework can include a library.&lt;/p&gt;
&lt;p&gt;This example shows a plain framework without a library and uses &lt;a href=&quot;https://vuejs.org/&quot; rel=&quot;noopener&quot;&gt;Vue&lt;/a&gt;, which is a &lt;a href=&quot;https://2021.stateofjs.com/en-US/libraries/front-end-frameworks/#front_end_frameworks_experience_ranking&quot; rel=&quot;noopener&quot;&gt;popular&lt;/a&gt; JavaScript framework:&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-html&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- index.html --&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;main&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  {{ message }}&lt;br /&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;script&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token script&quot;&gt;&lt;span class=&quot;token language-javascript&quot;&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Vue &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;./node_modules/vue/dist/vue.esm.browser.js&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token literal-property property&quot;&gt;el&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;#main&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token literal-property property&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token literal-property property&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Hello, world&#39;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;script&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;If you compare this framework example to the earlier library example, you might notice these differences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The framework code encompasses multiple techniques and abstracts them away into its own opinionated API.&lt;/li&gt;
&lt;li&gt;Developers don&#39;t have full control over how and when operations occur. For example, how and when Vue writes the &lt;code&gt;&#39;Hello, world&#39;&lt;/code&gt; string to the page is abstracted away from you.&lt;/li&gt;
&lt;li&gt;The instantiation of the &lt;code&gt;Vue&lt;/code&gt; class carries some &lt;a href=&quot;https://simple.wikipedia.org/wiki/Side_effect_(computer_science)&quot; rel=&quot;noopener&quot;&gt;side effects&lt;/a&gt;, which are common when you use frameworks, whereas a library may offer &lt;a href=&quot;https://en.wikipedia.org/wiki/Pure_function&quot; rel=&quot;noopener&quot;&gt;pure functions&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The framework prescribes a particular HTML template system rather than using your own.&lt;/li&gt;
&lt;li&gt;If you read further into the Vue framework documentation or most other framework documentations, you can see how frameworks prescribe architectural patterns that you can use. JavaScript frameworks take some cognitive burden away from you because you don&#39;t have to figure this out yourself.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;when-to-use-a-library-versus-a-framework&quot;&gt;When to use a library versus a framework &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#when-to-use-a-library-versus-a-framework&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;After you read the comparisons between libraries and frameworks, you may begin to understand when to use one or the other:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A framework can reduce complexity for you, the developer. As discussed, a framework can abstract away logic, behavior, and even architectural patterns. It&#39;s especially useful when you begin a new project. A library &lt;em&gt;can&lt;/em&gt; help with complexity, but typically focuses on code reuse.&lt;/li&gt;
&lt;li&gt;Framework authors want you to be productive and often develop extra tools, debugging software, and comprehensive guides among other resources to help you use a framework effectively. Library authors also want you to be productive, but specialized tools are uncommon in libraries.&lt;/li&gt;
&lt;li&gt;Most frameworks provide a functional starting point, such as a skeleton or boilerplate, to help you build web apps quickly. A library becomes part of your already established codebase.&lt;/li&gt;
&lt;li&gt;In general, frameworks introduce some complexity to your codebase. The complexity isn&#39;t always obvious at the start, but can reveal itself over time.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a reminder, you don&#39;t typically compare a library with a framework because they&#39;re different things that achieve different tasks. However, the more knowledge you have about the two, the more empowered you are to decide which is best for you. The decision to use a framework or library ultimately depends on your requirements.&lt;/p&gt;
&lt;h2 id=&quot;swapability&quot;&gt;Swapability &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#swapability&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You won&#39;t change your library or framework every week. However, it&#39;s good practice to understand the downsides of a package that locks you into its ecosystem. It&#39;s also recommended that you understand that the developer who decides to use a third-party package is somewhat responsible for the creation of a loose coupling between the package and the app source code.&lt;/p&gt;
&lt;p&gt;A package that&#39;s tied to the source code is harder to remove and swap for another package. You may need to swap a package when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You must make updates to a package that&#39;s no longer maintained.&lt;/li&gt;
&lt;li&gt;You discover that the package is too buggy to work with.&lt;/li&gt;
&lt;li&gt;You learn about a new package that meets your needs better.&lt;/li&gt;
&lt;li&gt;Your product requirements change and the package isn&#39;t needed anymore.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Consider this example:&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// header.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; color &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;@package/set-color&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token function&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;header&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;dark&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// article.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; color &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;@package/set-color&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token function&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;.article-post&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;dark&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// footer.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; color &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;@package/set-color&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token function&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;.footer-container&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;dark&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;The previous example uses the third-party &lt;code&gt;@package/set-color&lt;/code&gt; package across three separate files. If you work on this code and need to replace the third-party package, you must update the code in three places.&lt;/p&gt;
&lt;p&gt;Alternatively, you can simplify maintenance and abstract the library usage to one place, which you can see in this example:&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// lib/set-color.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; color &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;@package/set-color&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;element&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; theme &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;dark&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token function&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;element&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; theme&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// header.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; color &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;./lib/set-color.js&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token function&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;header&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// article.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; color &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;./lib/set-color.js&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token function&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;.article-post&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// footer.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; color &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;./lib/set-color.js&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token function&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;.footer-container&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;In the previous example, direct library usage is abstracted away. Thus, if you must swap out the third-party package, you only update one file. In addition, the code is now easier to work with because the internal &lt;code&gt;set-color.js&lt;/code&gt; file sets a default color theme to use.&lt;/p&gt;
&lt;aside class=&quot;aside flow bg-state-info-bg color-state-info-text&quot;&gt;&lt;div class=&quot; flow&quot;&gt; When you read the old code and new code, you may be unconvinced with the new code and ask questions such as:  - Isn&#39;t the new code more complex because there&#39;s an additional layer of indirection? - What if I never replace the package? How can I justify the time taken to abstract code away?  These are valid questions and the answers ultimately depend on the use case. With some analysis or simple prototyping, you can weigh the pros and cons to answer such questions, but often there isn&#39;t a right or wrong answer! &lt;/div&gt;&lt;/aside&gt;
&lt;h2 id=&quot;ease-of-use&quot;&gt;Ease of use &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#ease-of-use&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A framework may have a complex API, but the framework could offer developer tools that make it easier to use overall. Ease of use is based on many factors and can be highly subjective. A framework can be hard to use because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The framework has an inherently complex API.&lt;/li&gt;
&lt;li&gt;The framework is poorly documented, and requires a lot of trial and error to solve problems.&lt;/li&gt;
&lt;li&gt;The framework uses techniques that are unfamiliar to you and your team.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Frameworks can mitigate against these challenges through common best practices, such as these:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The framework offers developer and diagnostic tools to make debugging easier.&lt;/li&gt;
&lt;li&gt;The framework has an active community of developers who collaborate on free documentation, guides, tutorials, and videos. After you consume this content, you&#39;re productive with the framework.&lt;/li&gt;
&lt;li&gt;The framework offers an API that follows common &lt;a href=&quot;https://en.wikipedia.org/wiki/Coding_conventions&quot; rel=&quot;noopener&quot;&gt;coding conventions&lt;/a&gt;. You&#39;re productive with the framework because you learned such conventions previously and have an increased familiarity with coding styles.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While these points are commonly attributed to frameworks, they can also be attributed to libraries. For example, the &lt;a href=&quot;https://d3js.org/&quot; rel=&quot;noopener&quot;&gt;D3.js&lt;/a&gt; JavaScript library is powerful and has a large ecosystem that offers workshops, guides, and documentation among other resources, all of which impact its ease of use.&lt;/p&gt;
&lt;p&gt;Additionally, a framework typically prescribes an architecture for your web app while a library is typically compatible with your existing architecture, whatever it might be.&lt;/p&gt;
&lt;h2 id=&quot;performance&quot;&gt;Performance &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#performance&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In general, frameworks can affect performance more than libraries, although there are exceptions to this case. Web performance is a huge area with many topics, so these sections touch on two topics of note: tree shaking and software updates.&lt;/p&gt;
&lt;h3 id=&quot;tree-shaking&quot;&gt;Tree shaking &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#tree-shaking&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Bundling is only one facet of web performance, but it has a big performance effect, especially with larger libraries. The use of tree shaking during import and export helps performance because it finds and prunes code that&#39;s unnecessary to the app.&lt;/p&gt;
&lt;p&gt;When you bundle JavaScript code, there&#39;s a useful step known as tree shaking that&#39;s a valuable performance &lt;a href=&quot;https://developer.mozilla.org//docs/Glossary/Tree_shaking&quot; rel=&quot;noopener&quot;&gt;optimization&lt;/a&gt; that you can make to your code, although it&#39;s easier to do with libraries than frameworks.&lt;/p&gt;
&lt;p&gt;When you import third-party code into your source code, you typically bundle the code into one or a few output files. For example, the &lt;code&gt;header.js&lt;/code&gt;, &lt;code&gt;footer.js&lt;/code&gt;, and &lt;code&gt;sidebar.js&lt;/code&gt; files are all combined into the &lt;code&gt;output.js&lt;/code&gt; file, which is the output file that you load in your web app.&lt;/p&gt;
&lt;p&gt;To better understand tree shaking, consider these code examples:&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// library.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// main.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;./library.js&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;For the purpose of demonstration, the &lt;code&gt;library.js&lt;/code&gt; code sample is kept intentionally small compared to what you might find in the real world, where the library can be thousands of lines long.&lt;/p&gt;
&lt;p&gt;A naive bundle process may export the code with this output:&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// output.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;Even though the &lt;code&gt;subtract()&lt;/code&gt; function isn&#39;t needed in this app, it&#39;s still included in the final bundle. Unnecessary code like this increases the download size, parse and compile time, and execution costs that your users must pay. A basic tree-shaking approach removes the dead code and produces this output:&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// output.js file&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;Notice that the code is shorter and more succinct. In this example, the performance improvement is negligible, but in a real-world app where the library is thousands of lines long, the performance effect can be much more significant. Interestingly, modern bundle tools, such as Parcel, Webpack, and Rollup go a step further because they combine minification and tree shaking to create a highly optimized bundle. To demonstrate the effectiveness of bundle tools, we used &lt;a href=&quot;https://parceljs.org/&quot; rel=&quot;noopener&quot;&gt;Parcel&lt;/a&gt; to create a bundle file with the previous code examples. Parcel removed all unused code and exported this single module:&lt;/p&gt;
&lt;div&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;Parcel is smart enough to remove import statements, function definitions, and behavior among other items to create highly optimized code.&lt;/p&gt;
&lt;p&gt;Bundling is only one facet of web performance, but it has a big performance effect, especially with larger libraries. Tree shaking is typically simpler to do with libraries than with frameworks.&lt;/p&gt;
&lt;h3 id=&quot;software-updates&quot;&gt;Software updates &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#software-updates&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;For many libraries and frameworks, software updates add functionality, fix bugs, and ultimately grow in size over time. It&#39;s not always necessary to download updates, but if the updates include bug fixes, desired feature enhancements, or security fixes, then you should probably update. However, the more data you send over the wire, the less performant your app and the greater the performance effect on your user experience.&lt;/p&gt;
&lt;p&gt;If a library grows in size, you can use tree shaking to mitigate the growth. Alternatively, you can use a smaller alternative to the JavaScript library. For more information, see &lt;a href=&quot;https://web.dev/js-libraries-vs-frameworks/#swapability&quot;&gt;Swapability&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If a framework grows in size, not only is tree shaking more of a challenge, but it can be harder to swap out one framework for another. For more information, see &lt;a href=&quot;https://web.dev/js-libraries-vs-frameworks/#swapability&quot;&gt;Swapability&lt;/a&gt;.&lt;/p&gt;
&lt;aside class=&quot;aside flow bg-state-info-bg color-state-info-text&quot;&gt;&lt;div class=&quot; flow&quot;&gt; Framework authors and maintainers invest large efforts to optimize their code so you and your users can benefit. For many popular frameworks and libraries, performance fixes and improvements are often shipped with the software updates. Performance doesn&#39;t always get worse over time! &lt;/div&gt;&lt;/aside&gt;
&lt;h2 id=&quot;employability&quot;&gt;Employability &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#employability&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;It&#39;s a bit of an open secret that many companies have hard requirements for developers who know a particular framework. They may disregard your knowledge of web fundamentals and focus only on your specific knowledge of a certain JavaScript framework! Right or wrong, this is the reality for many jobs.&lt;/p&gt;
&lt;p&gt;Knowledge of a few JavaScript libraries won&#39;t harm your job application, but there&#39;s little guarantee that it&#39;ll make you stand out. If you know a few popular JavaScript frameworks very well, there&#39;s a good chance that employers see this knowledge as favorable in the current job market for web developers. Some large enterprise organizations are stuck with very old JavaScript frameworks and may even be desperate for candidates who are comfortable with such frameworks.&lt;/p&gt;
&lt;p&gt;You can use this open secret to your advantage. However, approach the job market with caution and with these considerations in mind:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Remember that if you spend a large amount of time in your career with only one framework, you may miss out on learning experiences with other, more modern frameworks.&lt;/li&gt;
&lt;li&gt;Consider a developer who doesn&#39;t solidly understand software development or web development fundamentals, yet is hired as a framework developer. This developer doesn&#39;t write effective code, and you may find it daunting or overwhelming to work on such a codebase. In some cases, this scenario can lead to burnout. For example, you may have to refactor code or performance-tune the code because it&#39;s slow.&lt;/li&gt;
&lt;li&gt;When you learn web development, the best path is to begin with a heavy focus on the fundamentals of web development, software development, and software engineering. Such a strong foundation helps you to pick up any JavaScript framework quickly and effectively.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion &lt;a class=&quot;headline-link&quot; href=&quot;https://web.dev/js-libraries-vs-frameworks/#conclusion&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Well done on your hard work in understanding how JavaScript frameworks and libraries compare. You won&#39;t pick frameworks or libraries often unless you work on &lt;a href=&quot;https://en.wikipedia.org/wiki/Greenfield_project&quot; rel=&quot;noopener&quot;&gt;greenfield&lt;/a&gt; projects or as a consultant. However, when such decisions do arise, the more knowledge you have on the subject, the better informed your decision.&lt;/p&gt;
&lt;p&gt;As you have learned, the choice of framework you make—and in some cases, the choice of library—can significantly affect your development experience and end users, such as with performance.&lt;/p&gt;
</content>
    <author>
      <name>Umar Hansa</name>
    </author>
  </entry>
</feed>
