# String Polyfills and Common Interview Methods in JavaScript

# What are we gonna Study?

Hey there folks, Hope you are enjoying your dev life, and having fun with your code!

Today we are going to study about these topics

1.  What string methods are
    
2.  Why developers write polyfills
    
3.  Implementing simple string utilities
    
4.  Common interview string problems
    
5.  Importance of understanding built-in behavior
    

These concepts are interview make or break, so gather all of your focus here and bear with me till the very end of it.

* * *

# What are String Methods?

*   String methods are built-in functions used for manipulating the string.
    
*   Since, Strings are primitives (immutable), they don't change the string, instead they create a new one.
    
*   Some common methods and their use case are listed in this table
    

* * *

### Extracting Methods

| Method | Use |
| --- | --- |
| `.slice(start, end)` | Used to extract a part of string from start to one character before end. (Supports negative indexing). |
| `substring(start, end)` | Same as `.slice()` but does not support negative indexing. |
| `substr(start, length)` | Extracts from start to a particular length, but considered depricated. |

* * *

### Searching

| Method | Use |
| --- | --- |
| `.indexOf(value)` | Returns the first index of a value in a string. |
| `.lastIndexOf(value)` | Returns the last index of a value in a string. |
| `.includes(value)` | Checks whether a string contains a value or not. |
| `.startsWith(value)` | Checks whether a string starts with a particular value. |
| `.endsWith(value)` | Checks whether a string ends with a particular value. |
| `.length` | Returns the length of the string. |

* * *

### Manipulating / Transforming

| Method | Use |
| --- | --- |
| `.toUpperCase()` | Transforms the string to upper case. |
| `.toLowerCase()` | Transforms the string to lower case. |
| `.trim()` | Removes whitespaces from both start and end. |
| `.trimStart()` | Removes whitespaces from start. |
| `.trimEnd()` | Removes whitespaces from end. |
| `.replace(old, new)` | Replaces old value from new value in a string (only first occurance). |
| `.replaceAll(old, new)` | Replaces old value from new value in a string (all occurances). |
| `.concat(str)` | Merges two or more strings. |
| `split(separator)` | Splits the string from a particular value (separator) and makes an array of the splited substrings. |
| `.repeat(count)` | Repeats the string. |

These all are basically your string methods, there are more methods but come on, i am not gonna cover each one of them.

* * *

# Why Developers Make Polyfills?

**Note:** Polyfills are basically nothing more than your own methods, not in just strings but almost every data type.

Now, the reason why developers make their own polyfills is quite obvious,

Because there are some kind of operation that you might perform often with strings, but JavaScript doesn't provides you a built-in methods for that, so instead of repeating one code everywhere you can write a Polyfill of that and use it whenever you need.

* * *

# Implementing Simple String Utilities

Let's make a few simple utilities for ourselves

```javascript
// Make a string utility that capitalizes the first letter of a string

const capitalize = (str) => {
  if (!str) return "";
  return str.charAt(0).toUpperCase().concat(str.slice(1).toLowerCase());
};
```

*   We can even transform it into a polyfill instead of a function, here's how
    

```javascript
if (!String.prototype.capitalize) {
  Object.defineProperty(String.prototype, 'capitalize', {
    value: function() {
      if (!this) return "";
      return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
    },
    enumerable: false
  });
}

console.log("hello world".capitalize());
```

*   Here, we check if the string already contains this method.
    
*   If not, we'll define a property called capitalize, where our we'll pass a function which contains all of our logic.
    
*   And then we said `enumerable: false` that means it won't show up in `for...of` loop.
    

So, that is how you make your own polyfills!

Let's make one more, for title case, I'll write the function, your assignment is to make it a polyfill ok?

```javascript
// Create a utility for making string in Title Case

const titleCase = (str) => {
  if (!str) return "";
  return str
    .split(" ")
    .map((s) => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase())
    .join(" ");
};

console.log(titleCase("hello MY NamE is Ved"));
```

* * *

# Common Interview Questions

Interviewers typically ask for manual implementations of ES6+ methods to ensure you don't just rely on built-ins.

*   `String.prototype.trim()`: Implement logic to remove leading and trailing whitespace. Interviewers often look for a manual loop approach (walking from both ends) or a regex-based solution.
    
*   `String.prototype.includes()`: Write a function that returns a boolean if a substring exists. A key detail candidates often miss is the second `startIndex` parameter.
    
*   `String.prototype.repeat(count)`: Replicate the string `count` times. Crucial edge cases include handling a negative count (which should throw a `RangeError`) and treating decimal counts as integers.
    
*   `String.prototype.startsWith()` **and** `endsWith()`: Check if a string begins or ends with a specific prefix/suffix. A common follow-up is asking how to handle cases where the prefix is longer than the string itself.
    
*   `String.prototype.replaceAll()`: Replace all occurrences of a substring. This is considered "tricky" because a naive `while` loop with `indexOf` can lead to an infinite loop if the replacement contains the search string.
    

* * *

# Importance of understanding built-in behavior

*   See, most of the interview questions are asked on your understanding of a built-in method.
    
*   They focus about do you know how this method works internally? Do you know what edge cases it handles?
    
*   Becuase see, in my opinion making these polyfills is very easy if you know how it works? What edge cases it handles?
    
*   And that is why industry veterans say that focus on foundations.
    
*   I could have solved all the interview questions but i left them for you, so that you can practice them. Please make them without the help of AI, this will help you understand behind the scenes of these built-in methods.
    

* * *

# Wrap Up!

So, that was it for this blog, Hope you enjoyed it, if yes, then please share it on X or LinkedIn, i would love to see that, if you are disappointed please share your valueable feedback.

I'll catch you up in the next blog, until then keep coding and enjoying your dev life
