String Polyfills and Common Interview Methods in JavaScript

Hello readers,
Welcome to another blog in this JS series. In the last blog, I covered Array Flatten in JavaScript and covered how nested structures can be simplified into a single level.
Now, let’s shift our focus to something that shows up a lot in interviews — Strings.
Not just using string methods… but actually understanding how they work under the hood.
Because trust me —
Writing str.includes() is easy
Explaining how it works internally is what gets you hired
So in this blog, we’ll break down:
What string methods are
Why polyfills matter
How to implement them
Common interview patterns
Let’s start
String Methods
String methods are built-in functions provided by JavaScript to manipulate and work with strings.
For example:
const str = "Hello World";
console.log(str.toUpperCase());
console.log(str.includes("World"));
console.log(str.slice(0, 5));
These methods make our lives easier…
But here’s the real question
Do you know how they actually work internally?
That's what I'm going to cover in this blog.
Why write Polyfills
A polyfill is basically your own implementation of a built-in method. So, if there are built-in methods already available, then why write your own (polyfills)?
Here are the reasons:
Interview perspective
For a deeper understanding
Browser compatibility: Earlier, not all browsers supported modern methods. Developers wrote polyfills to ensure consistency.
Now, you know the importance of polyfills. Let's write some polyfills of string methods.
1. Polyfill for includes()
Steps:
Loop through the main string
At each index, try to match the target string
Compare characters one by one
If all characters match → return
trueIf no match found → return
false
String.prototype.myIncludes = function(searchStr) {
for (let i = 0; i <= this.length - searchStr.length; i++) {
let match = true;
for (let j = 0; j < searchStr.length; j++) {
if (this[i + j] !== searchStr[j]) {
match = false;
break;
}
}
if (match) return true;
}
return false;
};
// Usage
console.log("Hello World".myIncludes("World"));
2. Polyfill for indexOf()
Steps:
Loop through the string
Try matching a substring at each position
If match found → return index
If not found → return
-1
String.prototype.myIndexOf = function(searchStr) {
for (let i = 0; i <= this.length - searchStr.length; i++) {
let match = true;
for (let j = 0; j < searchStr.length; j++) {
if (this[i + j] !== searchStr[j]) {
match = false;
break;
}
}
// Return index instead of true
if (match) return i;
}
return -1; // not found
};
3. Polyfill for startsWith()
Steps:
Check if the search string is longer than the main string
Compare characters from the beginning
If any mismatch → return false
If all match → return true
String.prototype.myStartsWith = function(searchStr) {
// Edge case: search string longer than original
if (searchStr.length > this.length) return false;
// Compare from start
for (let i = 0; i < searchStr.length; i++) {
if (this[i] !== searchStr[i]) {
return false; // mismatch
}
}
return true; // all matched
};
4. Polyfill for endsWith()
Steps:
Find the starting index from the end
Compare characters from that position
If mismatch → return false
If all match → return true
String.prototype.myEndsWith = function(searchStr) {
// Calculate start index
let start = this.length - searchStr.length;
// Edge case: search string longer
if (start < 0) return false;
// Compare characters
for (let i = 0; i < searchStr.length; i++) {
if (this[start + i] !== searchStr[i]) {
return false; // mismatch
}
}
return true; // match found
};
If you have noticed carefully....
includes() and indexOf() Use sliding window logicstartsWith() and endsWith() use direct comparison
This is exactly what interviews want:
Not just code — but pattern recognition
That’s it for this blog. These concepts may look simple, but mastering them will significantly improve your problem-solving skills and interview confidence.
In the next blog, we’ll explore one of the most important and confusing concepts in JavaScript — the new keyword.
I will see you in another interesting and useful blog. Till then...
Stay consistent and keep grinding....
Peace ✌️




