Skip to main content

Command Palette

Search for a command to run...

String Polyfills

Updated
3 min read
String Polyfills

What String methods are

The string methods are the built in methods of the strings that are used while manipulating and accessing the strings. these methods are related to the string.prototype .

  • toUpperCase() → returns a new uppercase version.

  • toLowerCase() → returns a new lowercase version.

  • trim() / trimStart() / trimEnd() → return a new string without whitespace.

  • replace(search, newValue) → returns a new string with substitution.

  • replaceAll(search, newValue) → returns a new string with all substitutions.

  • slice(start, end) → returns a substring.

  • substring(start, end) → similar to slice, returns a substring.

  • substr(start, length) → (deprecated) returns a substring of fixed length.

  • padStart(targetLength, padString) → returns a padded copy.

  • padEnd(targetLength, padString) → returns a padded copy.

These are some built in methods of the string which are available in the latest version , and there are few methods some developers need while working with the js strings and they are different for different users .

If i am working with strings and i want a different method that does not exist in the string.prototype so i can create it manually.


String Polyfills

When you are working with older versions of js where some methods are not their then you define them in the prototypes and use as a built in method.They are called as the polyfills .

What Is a Polyfill?

  • A polyfill is custom code that replicates the behavior of a newer built-in method.

  • It usually checks if the method exists, and if not, defines it on the prototype.

  • This ensures backward compatibility across browsers or runtimes.

Example :-

if (!String.prototype.repeat) {
  String.prototype.repeat = function(count) {
    let result = "";
    
    for (let i = 0; i < count; i++) {
      result += this;
    }
    
    return result;
  };
}

this code creates a repeat() function that repeats the string the times you want .


  • Reverse a String

Using built-ins:

"hello".split("").reverse().join(""); // "olleh"
  • Without built-ins: loop from end to start.

Check for Palindrome

function isPalindrome(str) {
  return str === str.split("").reverse().join("");
}
  • Find Longest Substring Without Repeating Characters

    • Sliding window technique.

    • Tests algorithmic thinking beyond just built-ins.

  • Implement Custom Replace

    • Interviewers may ask you to mimic .replace() logic using loops or regex.

Built-in Behaviour

  • Strings are immutable → every method returns a new string, never mutates the original.

  • Subtle differences matter:

    • .slice(start, end) vs .substring(start, end) → negative indexes behave differently.

    • .replace() → replaces only the first match unless you use regex with /g.

  • Performance considerations:

    • Repeated concatenation (+=) can be costly compared to using arrays and .join().
  • Spec compliance: Knowing quirks (like Unicode handling in .charCodeAt()) shows depth.


Conclusion

This is how we have to handle and create the string polyfills there are like the methods but you can use them as the built-in methods . Which helps you to program easily and use the features .