Skip to main content

Command Palette

Search for a command to run...

String Polyfills and Common Interview Methods in JavaScript

Updated
4 min read
String Polyfills and Common Interview Methods in JavaScript

Hello readers,

Welcome to another blog in this JS series. In the last blog, I covered Array Flatten in JavaScript and covered how nested structures can be simplified into a single level.

Now, let’s shift our focus to something that shows up a lot in interviews — Strings.

Not just using string methods… but actually understanding how they work under the hood.

Because trust me —
Writing str.includes() is easy
Explaining how it works internally is what gets you hired

So in this blog, we’ll break down:

  • What string methods are

  • Why polyfills matter

  • How to implement them

  • Common interview patterns

Let’s start

String Methods

String methods are built-in functions provided by JavaScript to manipulate and work with strings.

For example:

const str = "Hello World";

console.log(str.toUpperCase());
console.log(str.includes("World")); 
console.log(str.slice(0, 5));

These methods make our lives easier…

But here’s the real question
Do you know how they actually work internally?

That's what I'm going to cover in this blog.

Why write Polyfills

A polyfill is basically your own implementation of a built-in method. So, if there are built-in methods already available, then why write your own (polyfills)?

Here are the reasons:

  1. Interview perspective

  2. For a deeper understanding

  3. Browser compatibility: Earlier, not all browsers supported modern methods. Developers wrote polyfills to ensure consistency.

Now, you know the importance of polyfills. Let's write some polyfills of string methods.

1. Polyfill for includes()

Steps:

  1. Loop through the main string

  2. At each index, try to match the target string

  3. Compare characters one by one

  4. If all characters match → return true

  5. If no match found → return false

String.prototype.myIncludes = function(searchStr) {
  for (let i = 0; i <= this.length - searchStr.length; i++) {
    let match = true;

    for (let j = 0; j < searchStr.length; j++) {
      if (this[i + j] !== searchStr[j]) {
        match = false;
        break;
      }
    }

    if (match) return true;
  }

  return false;
};

// Usage
console.log("Hello World".myIncludes("World"));

2. Polyfill for indexOf()

Steps:

  1. Loop through the string

  2. Try matching a substring at each position

  3. If match found → return index

  4. If not found → return -1

String.prototype.myIndexOf = function(searchStr) {
  for (let i = 0; i <= this.length - searchStr.length; i++) {
    let match = true;

    for (let j = 0; j < searchStr.length; j++) {
      if (this[i + j] !== searchStr[j]) {
        match = false;
        break;
      }
    }

    // Return index instead of true
    if (match) return i;
  }

  return -1; // not found
};

3. Polyfill for startsWith()

Steps:

  1. Check if the search string is longer than the main string

  2. Compare characters from the beginning

  3. If any mismatch → return false

  4. If all match → return true

String.prototype.myStartsWith = function(searchStr) {
  // Edge case: search string longer than original
  if (searchStr.length > this.length) return false;

  // Compare from start
  for (let i = 0; i < searchStr.length; i++) {
    if (this[i] !== searchStr[i]) {
      return false; // mismatch
    }
  }

  return true; // all matched
};

4. Polyfill for endsWith()

Steps:

  1. Find the starting index from the end

  2. Compare characters from that position

  3. If mismatch → return false

  4. If all match → return true

String.prototype.myEndsWith = function(searchStr) {
  // Calculate start index
  let start = this.length - searchStr.length;

  // Edge case: search string longer
  if (start < 0) return false;

  // Compare characters
  for (let i = 0; i < searchStr.length; i++) {
    if (this[start + i] !== searchStr[i]) {
      return false; // mismatch
    }
  }

  return true; // match found
};

If you have noticed carefully....

includes() and indexOf() Use sliding window logic
startsWith() and endsWith() use direct comparison

This is exactly what interviews want:

Not just code — but pattern recognition

That’s it for this blog. These concepts may look simple, but mastering them will significantly improve your problem-solving skills and interview confidence.

In the next blog, we’ll explore one of the most important and confusing concepts in JavaScript — the new keyword.

I will see you in another interesting and useful blog. Till then...

Stay consistent and keep grinding....

Peace ✌️