Embarking on Search & Replace Functions

Hello, code adventurer! Today, we're navigating the complexities of JavaScript's Search and Replace functionality. This functionality enables us to find and replace words within sentences. Let's set sail!

Introduction to Search Methods: 'indexOf()'

Our journey begins with three primary tools that we can use to locate words within a string: indexOf(), lastIndexOf(), and includes(). Let's start with indexOf first!

The indexOf() function marks the first instance of a provided word within a provided string. Remember, like a puzzle, string positions start at 0. It's the finding function within our string universe. When there is no occurrence of the provided word, indexOf returns -1.

The code above searches for the word "Planet" within the spaceMessage string and reports its first location — the seventh position. When we search for , which doesn't exist, it returns .

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal