JavaScript For Cats

Functions

Now that we have our sentence stored in a variable let's give that variable to something that will replace words! We call things that perform actions like this functions because, well, they serve a specific function (AKA purpose or action) for us. Calling them "actions" sounded weird I guess so they went with the word "function" instead.

JavaScript has a function called replace that does exactly what we want! Functions take in any number of values (zero, one or many) and return either nothing (undefined) or exactly one value (functions can't return two or more values at a time — only a single value). The replace function is available to use on any strings and takes in two values: the characters to take out and the characters to swap in. It gets confusing to describe these things so here is a visual example:

Console

Notice how the value of dogSentence is the same even after we run replace on it? This is because replace (and most JavaScript functions for that matter) takes the variable or value that we give it and returns a new value instead of modifying the thing we passed in. Since we didn't store the new variable (there is no = on the left side of the replace function) it just printed out the return value in our console.