Skip to main content

Command Palette

Search for a command to run...

String Polyfills and Common Interview Methods in JavaScript

Go beyond using methods -- understand how they work internally

Updated
3 min readView as Markdown
String Polyfills and Common Interview Methods in JavaScript

What Are String Methods?

String methods are built in functions provided by JavaScript to work with text.

Example:

const str = "hello world";

str.toUpperCase();   // "HELLO WORLD"
str.includes("world"); // true
str.slice(0, 5);     // "hello"

But Here's the Real Question...

Do we know how this methods work internally?

That's where polyfills come in.

What is a Polyfill?

A polyfill is:

A custom implementation of a built-in method.

Why Developers Write Polyfills?

  • To support older browsers

  • To understand internal logic

  • To prepare for interviews

  • To customize behavior

Concept: How Built-in Method Works

Let's take an example:

"hello".includes("ll"); // true

Internally JavaScript:

  1. Loops through the string

  2. Checks substring match

  3. Returns True/False

Visual Understanding:

"hello"
  ↓
Check: "he", "el", "ll"
  ↓
Return true

Implementing Simple String Polyfills

1. Polyfill for includes()

String.prototype.myIncludes = function (search) {
  for (let i = 0; i <= this.length - search.length; i++) {
    if (this.slice(i, i + search.length) === search) {
      return true;
    }
  }
  return false;
};

Thinking Process:

Loop → Compare substring → Match? → Return true

2. Polyfill for startsWith()

String.prototype.myStartsWith = function (search) {
  return this.slice(0, search.length) === search;
};

3. Polyfill for endsWith()

String.prototype.myEndsWith = function (search) {
  return this.slice(-search.length) === search;
};

4. Polyfill for repeat()

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

Core Idea Behind All Polyfills

Break the problem into smaller steps:

Input → Process → Output

Common Interview String Problems

1. Reverse a String

function reverse(str) {
  return str.split("").reverse().join("");
}

2. Check Palindrom

function isPalindrome(str) {
  return str === str.split("").reverse().join("");
}

3. Count Characters

function countChars(str) {
  let map = {};

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

  return map;
}

4. Find First non Repeating Character

function firstUnique(str) {
  let map = {};

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

  for (let char of str) {
    if (map[char] === 1) return char;
  }
}

Important Concept

Built-in methods are optimized internally.

Our polyfills are for:

  • Learning

  • Interviews

  • Understanding Logic

Summary

Concept            | Meaning
------------------|----------------------------
String methods    | Built-in utilities
Polyfills         | Custom implementations
Goal              | Understand internal logic
Interview focus   | Problem-solving ability

Why This Matters

  • Build strong fundamentals

  • Helps crack interviews

  • Improves debugging skills

  • Makes us a better developer