String Polyfills and Common Interview Methods in JavaScript
Introduction
Strings are one of the most commonly used data types in JavaScript. Whether you're working on frontend UI, backend APIs, or solving coding interview problems, string manipulation is everywhere.
In this blog, weโll cover:
What string methods are Why developers write polyfills How to implement string utilities manually Common interview problems Why understanding built-in behavior matters What Are String Methods?
String methods are built-in functions provided by JavaScript to perform operations on strings.
Examples: let str = "Hello World";
str.toUpperCase(); // "HELLO WORLD" str.includes("World"); // true str.slice(0, 5); // "Hello"
These methods help us:
Modify strings Search within strings Extract parts of strings
๐ Important: Strings in JavaScript are immutable, meaning methods return a new string instead of modifying the original one.
Why Developers Write Polyfills?
A polyfill is a custom implementation of a built-in function.
Why needed? Older browsers may not support modern methods Helps understand how JS works internally Important for interviews (logic clarity) Example:
If .includes() didnโt exist, we can write our own version.
Implementing Simple String Polyfills
- 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; };
console.log("hello".myIncludes("ell")); // true 2. Polyfill for toUpperCase() String.prototype.myToUpperCase = function() { let result = "";
for (let char of this) { let code = char.charCodeAt(0);
// convert lowercase to uppercase
if (code >= 97 && code <= 122) {
result += String.fromCharCode(code - 32);
} else {
result += char;
}
}
return result; };
console.log("hello".myToUpperCase());
// HELLO 3. Polyfill for reverse() String.prototype.myReverse = function() { let result = "";
for (let i = this.length - 1; i >= 0; i--)
{
result += this[i];
}
return result;
}
console.log("hello".myReverse()); // "olleh" Common Interview String Problems
- Reverse a String function reverseString(str) { return str.split("").reverse().join(""); }
๐ Interview twist: Solve without using built-in methods
Check Palindrome function isPalindrome(str) { let reversed = str.split("").reverse().join(""); return str === reversed; }
Count Characters function charCount(str) { let map = {};
for (let char of str) { map[char] = (map[char] || 0) + 1; }
return map; } 4. 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; }
return null; } 5. Anagram Check function isAnagram(s1, s2) { return s1.split("").sort().join("") === s2.split("").sort().join(""); } How Built-in Methods Work (Conceptually)
Understanding internals is ๐ for interviews.
Example: slice() Takes start and end index Loops through string Builds a new string
๐ Internally:
function mySlice(str, start, end)
{
let result = "";
for (let i = start; i < end; i++) {
result += str[i];
}
return result;
}
String Processing Flow (Conceptual)
Input String โ Loop/Traversal โ Condition Check โ Build Result โ Return Output