# String Polyfills and Common Interview Methods in JavaScript

## What Are String Methods?

Think of a string as a train made of small compartments (characters). String methods are like tools that let us:

*   Count compartments (`length`)
    
*   Cut the train (`slice`)
    
*   Paint compartments (`toUpperCase`)
    
*   Search for a passenger (`includes`)
    

### Diagram: String Processing Flow

```plaintext
Input String → Apply Method → Output Result
```

Example: "hello" → `.toUpperCase()` → "HELLO"

## Why Do Developers Write Polyfills?

Polyfills are like teaching old toys new tricks. If a browser doesn’t understand a new method, developers write a custom version so everyone can still play.

### Diagram: Polyfill Behavior

```plaintext
Browser doesn’t know → Developer writes polyfill → Browser learns
```

## Implementing Simple String Utilities

Here are some fun utilities:

*   **Reverse a string**
    

```plaintext
function reverseString(str) {
  return str.split('').reverse().join('');
}
```

*   **Check for palindrome**
    

```plaintext
function isPalindrome(str) {
  const reversed = str.split('').reverse().join('');
  return str === reversed;
}
```

*   **Polyfill for includes**
    

```plaintext
if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    return this.indexOf(search, start) !== -1;
  };
}
```

## Common Interview String Problems

Interviewers love string puzzles because they test your thinking:

*   Reverse words in a sentence
    
*   Find the first non-repeating character
    
*   Count vowels and consonants
    
*   Check if two strings are anagrams
    

### Diagram: Interview Problem Flow

```plaintext
Problem → Break into steps → Apply logic → Solution
```

## Importance of Understanding Built-in Behavior

Built-in methods are like rules of a game. If you know them, you can invent new strategies. For example:

*   `.slice()` creates a new string, doesn’t change the original.
    
*   `.replace()` swaps parts but only the first match unless told otherwise.
    

## Interview Preparation Tips

*   Practice writing utilities without built-in methods.
    
*   Focus on explaining your logic.
    
*   Show how you break problems into steps.
    
*   Remember: interviews test your thought process, not just memorization.
