String Polyfills and Common Interview Methods in JavaScript

Introdcution
Strings are everywhere in JavaScript—user input, API responses, UI rendering, logs, and more. Yet many developers rely on built-in methods without truly understanding how they work under the hood.
This article bridges that gap by exploring string methods, polyfills, and common interview problems, with a strong focus on logic and implementation.
What are String Methods?
String methods are built-in functions provided by JavaScript to manipulate and work with text.
Some commonly used ones:
toUpperCase()toLowerCase()trim()includes()slice()replace()
Example:
const str = ' hello world ';
console.log(str.trim().toUpperCase()); // "HELLO WORLD"
Why Do Developers Write Polyfills?
A polyfill is a custom implementation of a built-in method.
Reasons:
Browser compatibility (older environments)
Deep understanding of internals
Interview preparation
Custom behavior when needed
Implementing simple string utilities
myTrim()Polyfill:
String.prototype.myTrim = function () {
let start = 0;
let end = this.length - 1;
while (this[start] === ' ') start++;
while (this[end] === ' ') end--;
let result = '';
for (let i = 0; i < end; i++) {
result += this[i];
}
return result;
};
myIncludes()Polyfill:
String.prototype.myIncludes = function (search) {
for (let i = 0; i < this.length - search.length; i++) {
let found = true;
for (j = 0; j < search.length; j++) {
if (this[i + j] !== search[j]) {
found = false;
break;
}
}
if (found) return true;
}
return false;
};
mySlice()Polyfill:
String.prototype.mySlice = function (start, end = this.length) {
let result = '';
if (start < 0) start = this.end + start;
if (end < 0) end = this.length + end;
for (let i = 0; i < end && i < this.length; i++) {
result += this[i];
}
return result;
};
Common Interview String Problems
- Reverse a String
function reverseString(str) {
let result = '';
for (let i = str.length - 1; i >= 0; i--) {
result += str[i];
}
return result;
}
- Check For Palindrome
function isPalindrome(str) {
let reversed = str.split('').reverse().join('');
return str === reversed;
}
Optimized:
function isPalindrome(str) {
let left = 0,
right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) return false;
left++;
right--;
}
return true;
}
- Count Character Frequency
function charCount(str) {
let map = {};
for (let char of str) {
map[char] = (map[char] || 0) + 1;
}
return map;
}
- Find First Non-Repeating Character
function firstUniqueChar(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;
}
return null;
}
Importance of Understanding Built-in Behavior
Most developers use methods like trim() or includes() daily—but interviews often test:
Can you implement them manually?
Do you understand edge cases?
Can you optimize logic?
Common Edge Cases:
Empty strings
Large inputs
Case sensitivity
Unicode characters
Negative indices
Conclusion
Understanding string methods at a deeper level gives you:
Stronger problem-solving skills
Better debugging ability
Confidence in interviews





