Skip to main content

String Polyfills and Common Interview Methods in JavaScript

Updated 4 min readMarkdown
Part of seriesbasic-of-javascript

JavaScript gives us many built-in string methods like split(), includes(), trim(), toUpperCase(), and more.
Most developers use them every day — but in interviews, you may be asked something deeper:

Can you implement similar behavior yourself?

In this article, we’ll learn:

  • What string methods are

  • Why developers write polyfills

  • How built-in methods work conceptually

  • How to implement simple string utilities


What Are String Methods in JavaScript?

A string is a sequence of characters.

Example:

const name = "JavaScript";

JavaScript provides many built-in methods to work with strings, such as:

  • toUpperCase()

  • toLowerCase()

  • includes()

  • startsWith()

  • endsWith()

  • slice()

  • substring()

  • trim()

  • replace()

  • split()

Example:

const text = "hello world";

console.log(text.toUpperCase()); // HELLO WORLD
console.log(text.includes("world")); // true
console.log(text.startsWith("he")); // true

These methods save time and make code readable.

But if you really want to become strong in JavaScript, you should also understand:

How these methods work internally (at a basic level)

That’s where polyfills come in.

What Is a Polyfill?

A polyfill is a custom implementation of a feature that behaves like a built-in JavaScript method.

In simple words:

A polyfill is your own version of a built-in method.

For example, if an environment doesn’t support String.prototype.includes, you can create your own version.

Why Do Developers Write Polyfills?

Polyfills are useful for two major reasons:

1. Browser Compatibility (historically very important)

Older browsers may not support newer JavaScript features.

For example, in older environments, includes() might not exist.

So developers wrote polyfills to make modern features work everywhere.

2. Interview Preparation (very important )

In interviews, companies often ask you to:

  • implement built-in methods manually

  • avoid using direct helper methods

  • show your logic and understanding

  • solve string problems using loops and conditions

Why?

Because they want to test:

  • problem-solving ability

  • understanding of strings

  • knowledge of loops and indexing

  • edge-case handling

  • awareness of built-in behavior

So even if you use built-in methods daily, you should know the logic behind them.

3) Understanding Strings Conceptually

Before writing polyfills, remember this:

A string behaves like an array of characters (but it is not actually an array).

Example:

const str = "hello";

console.log(str[0]); // h
console.log(str[1]); // e
console.log(str.length); // 5

That means many string methods can be recreated using:

  • loops

  • indexes

  • conditions

  • accumulation

This is exactly how interviewers expect you to think.


4) Common Interview String Problems

These are very common in JavaScript interviews.


A) Reverse a String

function reverseString(str) {
  let result = "";

  for (let i = str.length - 1; i >= 0; i--) {
    result += str[i];
  }

  return result;
}

console.log(reverseString("hello")); // olleh

B) Check Palindrome

function isPalindrome(str) {
  let left = 0;
  let right = str.length - 1;

  while (left < right) {
    if (str[left] !== str[right]) return false;
    left++;
    right--;
  }

  return true;
}

console.log(isPalindrome("madam")); // true

C) Count Characters

function charCount(str) {
  const result = {};

  for (let char of str) {
    result[char] = (result[char] || 0) + 1;
  }

  return result;
}

console.log(charCount("hello"));
// { h: 1, e: 1, l: 2, o: 1 }

5) Importance of Understanding Built-in Behavior

This is what makes your interview answers strong.

Instead of only using:

str.includes("abc")

You should understand:

  • how searching works internally

  • how loops compare characters

  • how indexes move

  • edge cases

  • time complexity

Interviewers like this because:

  • It shows logic-building skills

  • It proves you understand fundamentals

  • It helps in DSA + frontend interviews

  • It improves debugging ability

No comments yet. Be the first to comment.