Back to blog
JavaScript

String Polyfills and Common Interview Methods in JavaScript

May 10, 2026 2 min read

What Are String Methods?

String methods are built-in functions used to process text.

Examples:

  • toUpperCase()

  • trim()

  • split()

  • includes()


Example

const name = "javascript";

console.log(name.toUpperCase());


Output

JAVASCRIPT


How String Methods Work Conceptually

A method:

  1. Receives input

  2. Processes characters

  3. Returns new string/result


String Processing Flow

Diagram: flowchart LR

Common String Methods

MethodPurpose
toUpperCase()Uppercase text
toLowerCase()Lowercase text
trim()Remove spaces
split()Convert to array
includes()Check substring
replace()Replace text

Example: trim()

const text = "   hello   ";

console.log(text.trim());


Output

hello


What Is a Polyfill?

A polyfill is custom code that recreates built-in functionality.

Used when:

  • Feature doesn't exist

  • Older browsers lack support

  • Interview practice


Why Developers Write Polyfills

ReasonExplanation
Learn internalsUnderstand built-in behavior
Browser compatibilitySupport older environments
Interview preparationCommon frontend interviews

Example Polyfill: Custom trim()


Built-In Version

const text = "  hello  ";

console.log(text.trim());


Simple Custom Version

function customTrim(str) {
  return str.replace(/^\\s+|\\s+$/g, "");
}

console.log(customTrim("  hello  "));


Polyfill Behavior Representation

Diagram: flowchart LR

Example: includes()


Built-In Method

const text = "JavaScript";

console.log(text.includes("Script"));


Output

true


Simple Custom includes()

function customIncludes(str, word) {
  return str.indexOf(word) !== -1;
}

console.log(customIncludes("JavaScript", "Script"));


Common Interview String Problems

1. Reverse a String


Example

function reverseString(str) {
  return str.split("").reverse().join("");
}

console.log(reverseString("hello"));


Output

olleh


2. Check Palindrome

A palindrome reads same forward and backward.


Example

function isPalindrome(str) {
  const reversed = str.split("").reverse().join("");

  return str === reversed;
}

console.log(isPalindrome("madam"));


Output

true


3. Count Characters

function countCharacters(str) {
  return str.length;
}

console.log(countCharacters("JavaScript"));


Output

10


4. Capitalize First Letter

function capitalize(str) {
  return str[0].toUpperCase() + str.slice(1);
}

console.log(capitalize("javascript"));


Output

Javascript


Why Interviews Ask String Problems

String questions test:

  • Logic building

  • Loops

  • Array methods

  • Problem-solving

  • Understanding of built-ins


Important Built-In Behaviors

MethodReturns New Value?
trim()Yes
toUpperCase()Yes
replace()Yes

Strings are immutable.

Original string does not change.


Example

const text = "hello";

text.toUpperCase();

console.log(text);


Output

hello


Correct Way

const text = "hello";

const updated = text.toUpperCase();

console.log(updated);


Output

HELLO


Simple Interview Tips

TipReason
Practice loopsMany problems use iteration
Understand strings as arrays of charactersCore concept
Learn built-in methodsFaster solutions
Write custom logic tooInterview expectation

Practice Exercise

Reverse String

function reverse(str) {
  return str.split("").reverse().join("");
}

console.log(reverse("node"));


Check Substring

function containsWord(str, word) {
  return str.includes(word);
}

console.log(containsWord("JavaScript", "Script"));


Key Takeaways

ConceptSummary
String MethodsBuilt-in text utilities
PolyfillCustom implementation
Interview FocusLogic + built-in understanding
StringsImmutable values
Common ProblemsReverse, palindrome, search

Final Notes

String problems are extremely common in JavaScript interviews because they test:

  • Problem-solving

  • Data manipulation

  • Understanding of core language behavior

A good developer should understand both:

  • Using built-in methods

  • Implementing logic manually


0 Comments

Sign in to join the conversation

No comments yet. Be the first to comment!