DEV Community

Laxman Nemane
Laxman Nemane

Posted on

πŸš€ Day of Strengthening My DSA Fundamentals – JavaScript String Methods

Today, I focused on building strong fundamentals by learning and revising basic string operations in JavaScript.

Mastering string manipulation is extremely important for coding interviews, especially for product-based companies and FAANG-level roles.

Here’s what I learned today πŸ‘‡

πŸ“Œ 1️⃣ Basic Properties
βœ… length
Returns the total number of characters in a string (including spaces).

"Hello".length // 5

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 2️⃣ Accessing Characters
βœ… at(index)

Returns the character at a given index.
Supports negative indexing.


"Hello".at(1)  // "e"
"Hello".at(-1) // "o"
Enter fullscreen mode Exit fullscreen mode

βœ… charAt(index)

Returns the character at a given index.
❌ Does NOT support negative indexing.

"Hello".charAt(1) // "e"

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 3️⃣ Searching in Strings
βœ… includes(searchString, position)

Returns true or false.


"banana".includes("a")     // true
"banana".includes("b", 1)  // false
Enter fullscreen mode Exit fullscreen mode

βœ… indexOf(searchString, position)

Returns the first occurrence index or -1.

"banana".indexOf("a") // 1

Enter fullscreen mode Exit fullscreen mode

βœ… lastIndexOf(searchString)

Returns the last occurrence index.

"banana".lastIndexOf("a") // 5

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 4️⃣ Modifying Strings
βœ… concat()

Combines strings and returns a new string.

"Hello".concat(" ", "World")

Enter fullscreen mode Exit fullscreen mode

βœ… replace(pattern, replacement)

Replaces only the first occurrence.


"banana".replace("a", "x")
// "bxnana"
Enter fullscreen mode Exit fullscreen mode

βœ… replaceAll(pattern, replacement)

Replaces all occurrences.

"banana".replaceAll("a", "x")
// "bxnxnx"
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 5️⃣ Padding Methods
βœ… padStart(targetLength, padString)

Adds characters at the beginning.


"7".padStart(3, "0")
// "007"
Enter fullscreen mode Exit fullscreen mode

βœ… padEnd(targetLength, padString)

Adds characters at the end.


"34".padEnd(5, "*")
// "34***"
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 6️⃣ Pattern Matching
βœ… match()

Returns matched results (array) or null.
Often used with regular expressions.


"banana".match(/a/g)
// ["a", "a", "a"]
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Key Learnings

  • Strings are immutable in JavaScript.
  • Most string methods return a new string.
  • Searching methods are case-sensitive.
  • Understanding return types is very important for interviews.

Consistency > Motivation πŸš€

DSA #JavaScript #LearningInPublic #FrontendDeveloper #FAANGJourney

Top comments (0)