Course Content
Data types and Values
0/1
Object-oriented programming in JavaScript
0/1
Error handling and debugging in JavaScript
0/1
JavaScript functions for string and array manipulation
0/1
JavaScript Libraries and Frameworks
0/1
JavaScript
About Lesson

Polyfilling

Polyfilling in JavaScript refers to the practice of providing modern functionality to older web browsers that lack support for certain features or APIs. It’s a way to bridge the gap between older browsers and modern web standards, enabling developers to use the latest features while ensuring compatibility with a wider range of browsers.

When a new feature or API is introduced in JavaScript, not all browsers may support it immediately. This can create inconsistencies in how websites and web applications behave across different browsers. Polyfills address this issue by offering a JavaScript implementation of the missing feature, allowing developers to use the feature regardless of the browser’s native support.

How Polyfilling Works

  1. Detect Browser Support: Before using a particular feature or API, developers can use feature detection techniques to determine if the browser natively supports it.
  2. Check for Polyfill: If the feature is not supported, developers can include a polyfill script in their code. The polyfill script contains JavaScript code that emulates the desired functionality.
  3. Provide Fallback: The polyfill script is loaded and executed only if the feature is not supported by the browser. This allows the developer to provide a fallback solution that maintains consistent behavior across different browsers.
  4. Use Modern Features: With the polyfill in place, developers can now use the modern feature or API as if it were natively supported, even on older browsers.
  5. Progressive Enhancement: As browser support improves over time, developers can remove unnecessary polyfills to optimize performance for modern browsers.

Examples of Polyfilling

0. Number.isNaN() Polyfill Example

ES6 introduces a utility called Number.isNaN() to accurately check for NaN values, replacing the old isNaN() function. However, you can create a polyfill to use Number.isNaN() in your code, regardless of browser support for ES6.

Polyfill Code


if (!Number.isNaN) {
  Number.isNaN = function isNaN(x) {
    return x !== x;
  };
}
  

The if statement ensures that the polyfill is applied only in non-ES6 browsers where Number.isNaN() might not be available yet.
If the utility is not present, the code snippet defines Number.isNaN() to provide consistent functionality across browsers.

Note: This evaluation capitalizes on a unique characteristic of NaN values: they stand alone in the language as the sole value that doesn’t equate to itself. Consequently, the NaN value is the sole exception that causes the condition x !== x to yield a true outcome.

1. Promises

Older browsers like Internet Explorer lack native support for Promises. You can use a polyfill library like “es6-promise” to provide Promises in older browsers.

2. Fetch API

The Fetch API simplifies making network requests. Older browsers may not support Fetch. You can use a polyfill library like “whatwg-fetch” to add Fetch support to older browsers.

Conclusion

Not all new features can be perfectly polyfilled. Even when most of the behavior can be replicated, there might be slight differences. If you decide to create a polyfill, be very cautious and ensure you closely follow the specifications. Alternatively, it’s a good idea to rely on well-established polyfills that are trusted, like the ones offered by ES5-Shim and ES6-Shim.