Skip to main content

Command Palette

Search for a command to run...

String Polyfills and Common Interview Methods in JavaScript

Updated
4 min read

Have you ever used methods like split(), includes(), startsWith(), or trim() and wondered how JavaScript actually performs these operations behind the scenes?

Most developers use built-in string methods every day. However, in frontend interviews, especially for JavaScript roles, interviewers often go one step further and ask:

"Can you implement this method yourself?"

This is where understanding string polyfills becomes valuable.

In this article, we'll learn what string methods are, why developers write polyfills, how common string utilities work internally, and some popular interview questions related to strings

The Problem

Imagine you're building a JavaScript application and need to check whether a string contains a specific word.

You can simply write:

const text = "JavaScript is awesome";

console.log(text.includes("awesome"));

Output:

true

Easy, right?

But what if this method didn't exist?

Could you build it yourself?

That's exactly what interviewers want to test.

They are not interested in whether you remember the method name.

They want to know whether you understand the logic behind it.

What Are String Methods?

String methods are built-in functions provided by JavaScript that allow us to manipulate and process text.

Examples:

let name = "Ashish";
name.toUpperCase();
name.toLowerCase();
name.includes("shi");
name.startsWith("A");
name.endsWith("h");
name.trim();
name.split("");

These methods help us perform common text operations without writing complex logic every time.

Why Developers Write Polyfills

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

Think of it as:

"If JavaScript didn't provide this method, how would I create it myself?"

Developers write polyfills to:

  • Understand internal working of JavaScript methods

  • Support older browsers

  • Improve problem-solving skills

  • Prepare for interviews

Many JavaScript interviews include questions like:

  • Create your own includes()

  • Implement startsWith()

  • Build a custom trim()

  • Create a custom split()

How Built-in Methods Work Conceptually

Let's understand the idea behind string processing.

String Processing Flow

Input String
      │
Character Traversal
      │
Condition Checking
      │
Result Generation
      │
Return Output

Almost every string method follows this pattern.

The method reads characters one by one, performs some checks, and returns a result.

Polyfill for includes()

Built-in Method

const text = "JavaScript";

console.log(text.includes("Script"));

Output:

true

Logic

  1. Traverse the string.

  2. Check every possible position.

  3. Compare characters.

  4. If all characters match, return true.

  5. Otherwise return false.

Polyfill

String.prototype.myIncludes = function(search) {
  const str = this;

  for (let i = 0; i <= str.length - search.length; i++) {
    let match = true;

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

    if (match) {
      return true;
    }
  }

  return false;
};

Usage:

console.log("JavaScript".myIncludes("Script"));

Output:

true

Polyfill for startsWith()

Built-in Method

console.log("JavaScript".startsWith("Java"));

Output:

true

Logic

Compare characters from the beginning of the string

Polyfill

String.prototype.myStartsWith = function(search) {
  const str = this;

  for (let i = 0; i < search.length; i++) {
    if (str[i] !== search[i]) {
      return false;
    }
  }

  return true;
};

Usage:

console.log("JavaScript".myStartsWith("Java"));

Output:

true

Polyfill for endsWith()

Built-in Method

console.log("JavaScript".endsWith("Script"));

Output:

true

Logic

Start comparing from the end of both strings.

Polyfill

String.prototype.myEndsWith = function(search) {
  const str = this;

  if (search.length > str.length) {
    return false;
  }

  let start = str.length - search.length;

  for (let i = 0; i < search.length; i++) {
    if (str[start + i] !== search[i]) {
      return false;
    }
  }

  return true;
};

Polyfill for trim()

Built-in Method

const text = "   hello world   ";

console.log(text.trim());

Output:

hello world

Logic

Remove spaces from:

  • Beginning

  • End

Keep spaces in the middle unchanged.

Polyfill

String.prototype.myTrim = function() {
  let start = 0;
  let end = this.length - 1;

  while (this[start] === " ") {
    start++;
  }

  while (this[end] === " ") {
    end--;
  }

  return this.slice(start, end + 1);
};

Final Thoughts

Most developers use methods like includes(), startsWith(), and trim() every day without thinking about what happens internally.

However, understanding the logic behind these methods helps you become a stronger JavaScript developer and perform better in interviews.

The next time you use a built-in string method, ask yourself:

"If JavaScript didn't provide this method, could I build it myself?"

If your answer is yes, you're developing the kind of problem-solving mindset that interviewers look for.