Skip to main content

Command Palette

Search for a command to run...

String Polyfills and Common Interview Methods in JavaScript

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

JavaScript strings look simple on the surface, but they hide a lot of interesting behavior underneath. As developers, we use string methods like slice(), includes(), and trim() almost every day without thinking twice. They make our work easier, cleaner, and more readable. But in interviews and deeper problem-solving scenarios, just knowing how to use them is not enough.

In this blog, we’ll break down what string methods really are, why developers write polyfills, how to implement common string utilities on your own, and the types of string problems you’ll often face in interviews. The goal is simple: move from just using JavaScript to actually understanding it.

What string methods are

String methods are built-in functions that JavaScript gives us to work with text. Instead of manually looping through characters or writing complex logic, we can directly use methods like toUpperCase(), slice(), includes(), trim(), etc.

For example:

const name = "hello world";
console.log(name.toUpperCase()); // HELLO WORLD

These methods are already optimized and tested, so in real-world development we mostly rely on them instead of reinventing the wheel.

But interviews are different. There, you are often asked to implement these methods yourself to check your understanding.

Why developers write polyfills

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

Why would someone do that?

First, older browsers may not support newer methods. For example, includes() was not supported in very old browsers, so developers wrote polyfills to make it work everywhere.

Second, interviews. Interviewers want to see if you actually understand how things work behind the scenes instead of just using them.

Third, learning. Writing a polyfill forces you to think deeply about edge cases, loops, indexing, and behavior.

So instead of this:

"hello".includes("ell");

You might be asked to implement your own version.

Implementing simple string utilities

Let’s look at how we can build some basic string methods ourself.

  1. Custom includes()

function myIncludes(str, search) { 
for (let i = 0; i <= str.length - search.length; i++) { 
let found = true;
for (let j = 0; j < search.length; j++) {
  if (str[i + j] !== search[j]) {
    found = false;
    break;
  }
}

if (found) return true;
}

return false; 
}
  1. Custom reverse string

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

3. Custom trim()

function myTrim(str) { 
let start = 0;
let end = str.length - 1;

while (str[start] === " ") start++; 
while (str[end] === " ") end--;

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

When you write these, you start understanding how strings are just sequences of characters and how indexing works internally.

Common interview string problems

String questions are extremely common in interviews because they test logic, edge cases, and problem-solving.

Some popular ones:

  1. 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; 
}
  1. Count characters

    function charCount(str) {
      const map = {};
    
      for (let char of str) {
        map[char] = (map[char] || 0) + 1;
      }
    
      return map;
    }
    
  2. Find first non-repeating character

    function firstUnique(str) { 
    const map = {};
    
    for (let char of str) { 
    map[char] = (map[char] || 0) + 1; 
    }
    
    for (let char of str) {
    if (map[char] === 1) return char; 
    }
    
    return null; }
    
  3. Anagram check

function isAnagram(a, b) { 
if (a.length !== b.length) return false;

const count = {};

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

for (let char of b) { 
if (!count[char]) return false; count[char]--; 
}

return true; }

These problems are not about memorizing solutions, but understanding patterns like frequency maps, two pointers, and traversal.

Importance of understanding built-in behavior

Most developers use built-in methods daily, but very few understand how they actually behave.

For example:

  • slice() does not modify the original string

  • substring() handles negative values differently

  • trim() removes only whitespace, not all characters

  • Strings are immutable in JavaScript

If you don’t know these details, you can easily make mistakes.

Understanding internals helps you:

  • Debug faster

  • Write optimized code

  • Handle edge cases better

  • Perform well in interviews

It also builds strong fundamentals, which is what companies actually look for.

Final thought

Using string methods is easy. Understanding them is what makes you strong. If you can both use a method and implement it yourself, you are no longer just a user of JavaScript, you actually understand how it works.