String Polyfills and Common Interview Questions in JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
This blog series is designed for professionals who want to learn JavaScript step by step. Starting from the basics like variables and data types, we gradually move to functions, DOM manipulation, and real-world mini projects.
Javascript is a very powerful language not just because if what is can do, but how it actually does the things. One of the core and fundamental concept of Javascript is callbacks which is widely used
What routing means in mobile applications
Most of the react native tutorials teaches you how to build application or new screens but very few talks about the systems. That is why the developers can easily build attractive screens but failed t
As a Javascript developer, you have seen the application become more interactive and started handling more asynchronous operations like API calls, file readings, and timers. Initially, developers rely
While writing a Javascript code, no matter how carefully you have written the code but once the user input a invalid data and the API fails, the application might crash at runtime due to some edge cas

JavaScript gives developers a lot of flexibility, but one thing that confuses almost everyone at first is this. In this article, we’ll understand: What this means in JavaScript How this behaves in f

In Javascript, strings are used everywhere, but many developers do not know how they actually work behind the scenes.
When you call a method like .toUpperCase() on a string primitive, JavaScript creates a temporary wrapper object (using the String constructor) so the method can be executed. Once the operation is done, that object is immediately discarded, and the result is returned as a primitive.
String methods are the built-in functions that are used to manipulate and update the string.
Here are some of the string methods:
toUpperCase()
toLowerCase()
slice()
substring()
includes()
split()
trim()
These methods don’t modify the original string (because strings are immutable). Instead, they return a new string.
Let's take an example toUpperCase and understand how it works.
When you call string.toUpperCase()
const str = 'Hello';
str.toUpperCase();
Javascript iterate over each character
Convert that character into uppercase
Build a new string
Think of execution like this
Input String → Process Each Character → Apply Logic → Return New String
Polyfills are a custom implementation of the built-in functions. It is mostly used to create the same functionality that built-in functions offer.
Provide support for new methods in older browsers
Understanding internal logic
To practice the built-in methods logic by creating them of their own
Let's implement some polyfills for string methods
string.toLowerCase()
Here I will be writing a custom polyfill for toLowerCase.
if(!String.prototype.toLowerCase){
String.prototype.toLowerCase = function () {
let result = "";
// this refer to string on which method is called
for (let char of this) {
const code = char.charCodeAt(0);
// a-z → A-Z
if (code >= 97 && code <= 122) {
result += String.fromCharCode(code - 32);
} else {
result += char;
}
}
return result;
};
}
This will only work if the browser doesn't support string.toLowerCase, it will use the polyfill toLowerCase
string.includes
if(!String.prototype.includes){
String.prototype.myIncludes = function(searchString) {
return this.indexOf(searchString) !== -1;
};
}
If you're preparing for interviews, these are must-practice:
Reverse a String function without using built-in methods
Logic: Iterate over the string and append the new string with each character in an order so that the last character comes first.
Check Palindrome
Logic: Reverse and create a new string. Check if both strings are equal or not.
Count Characters
Logic: Use a "Map" or an object to store counts as you iterate through the string.
Anagram Detection: Do two strings contain the same characters?
Logic: Sort both strings and compare them.
Most developers stop at using methods. But in an interview, the interviewer tests:
How things work internally
Your problem-solving ability
Your understanding of edge cases
Understanding internals helps you in writing better code, debugging faster, and performing well in interviews.
Example questions:
How does slice() differ from substring()?
Why are strings immutable?
How would you optimize a string search?
If you want to stand out as a developer:
Don’t just use methods — understand them, practice writing polyfills, and solve real interview problems.
That’s where real growth happens.
Follow the series to learn more about Javascript.