String Polyfills and Common Interview Methods in JavaScript
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:
Receives input
Processes characters
Returns new string/result
String Processing Flow
Common String Methods
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
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
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
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
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
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!