Skip to main content

Command Palette

Search for a command to run...

String Polyfills and Common Interview Methods in JavaScript

Updated
•5 min read•View as Markdown
N

I build clean and simple web experiences and learn something new every day.

At first, string methods in JavaScript feel very normal.

You use things like:

str.toUpperCase()
str.includes()
str.trim()

…and life goes on. You type them. They work.
Simple.

But one day I stopped and thought: How do these methods actually work internally?
Because JavaScript didn’t magically create them out of nowhere.

Somewhere behind the scenes… someone implemented the logic. And that realization led me into something really interesting:

Polyfills

And honestly…
understanding polyfills changed how I think about JavaScript methods completely.


What Are String Methods?

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

Example:

const text = "hello";

console.log(text.toUpperCase());

Output:

HELLO

Methods help us:

  • search strings

  • modify text

  • extract characters

  • format data

Without them… working with strings would be painful.


But Here’s the Important Question

When you run:

text.includes("he")

HOW does JavaScript know whether "he" exists?
What logic is happening internally?

That’s the deeper understanding interviews often test.


What Is a Polyfill?

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

Meaning: If JavaScript didn’t provide the method… you could create it yourself.


Real Mental Model

Built-in method missing?
        ↓
Create your own version
        ↓
That becomes a polyfill

Why Developers Write Polyfills

Originally, browsers didn’t support every modern JavaScript feature. So developers created polyfills to mimic missing functionality.

Example: old browsers didn’t support includes()

So developers implemented it manually.


But Polyfills Are Also Amazing for Learning

Because when you build methods yourself…
you finally understand: how they actually work internally.

And honestly…
that’s where real JavaScript understanding begins.


Example 1: Polyfill for includes()

Let’s build a simple version.

function myIncludes(str, search) {
  return str.indexOf(search) !== -1;
}

Usage:

console.log(myIncludes("hello", "he"));

Output:

true

What’s Happening Here?

indexOf() returns:

position of substring

If substring doesn’t exist:

-1

So:

!== -1

means: substring exists.


Small But Important Insight

When you create polyfills… you stop treating methods like magic.

You start seeing: the actual logic underneath.


Example 2: Polyfill for reverse()

JavaScript does NOT have a built-in string reverse method.

So this becomes a common interview question.


Problem:

Convert:

"hello"

into:

"olleh"

Solution:

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

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

  return reversed;
}

What’s Happening?

We start from the last character:

o ← l ← l ← e ← h

…and keep building a new string.


Why Interviewers Love This Question

Because it tests:

  • loops

  • string traversal

  • logic building

  • understanding of indexing

Not just memorization.


Example 3: Palindrome Check

Another classic interview question. A palindrome reads the same forward and backward.

Example:

madam
racecar

Solution:

function isPalindrome(str) {
  const reversed = str.split("").reverse().join("");

  return str === reversed;
}

Conceptual Understanding

Original → Reverse → Compare

If both match: palindrome.


Why Understanding Built-In Behavior Matters

Most people memorize methods.

But interviews often ask: “Can you implement it yourself?”

That changes everything.

Because now:

syntax knowledge is not enough
logic understanding matters


Example 4: Polyfill for startsWith()

Built-in:

"hello".startsWith("he");

Our version:

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

  return true;
}

What’s Happening Here?

We compare characters one by one.

h === h
e === e

If all match: string starts with search value.


Real Interview Thinking

Interviewers usually don’t care if you remember methods. They care whether you can think logically.

For example:

Instead of asking:

Do you know reverse()?

They ask:

How would YOU reverse a string?

That’s a completely different skill.


String Processing Visualization

Input String
      ↓
Character Processing
      ↓
Logic Applied
      ↓
New Output

Common Interview String Problems

You’ll frequently see:

  • reverse a string

  • check palindrome

  • count characters

  • remove duplicates

  • find longest word

  • capitalize words

  • implement includes()

  • implement trim()

These are popular because strings test problem-solving very well.


Example 5: Remove Spaces (Trim Concept)

function myTrim(str) {
  return str.replace(/^\\s+|\\s+$/g, "");
}

This removes spaces from start and end.


Small But Powerful Realization

At first, built-in methods feel like shortcuts. But internally… they are just logic.

That realization is huge. Because once you understand the logic…
you can build solutions even when methods don’t exist.


Why Polyfills Actually Matter

Polyfills teach you: how JavaScript behaves internally.

And that understanding helps with:

  • debugging

  • interviews

  • problem solving

  • writing cleaner logic


Assignment Practice

Try implementing these yourself:

1. includes()
2. startsWith()
3. string reverse
4 palindrome checker
5. capitalize first letter

Don’t Google immediately.

Think first.
That struggle is where learning happens.


Final Understanding

At first, string methods feel like built-in magic. But once you start writing polyfills… you realize: every method is just logic hidden behind a clean interface.

And honestly… that changes how you look at JavaScript completely.

Because now…
you’re not just USING methods anymore.

You understand how they work.

More from this blog

C

codeXninjaDev

54 posts

I build clean and simple web experiences and learn something new every day.