Splitting a String does not really work as intended

I just started the challenge “Title Case a Sentence”. I wanted to start by splitting the String into an array.

This is the code I started with:

function titleCase(str) {

  let arr = str.split(" ");

  return arr;

}

console.log(titleCase("I'm a little tea pot"));

My problem is, that this code logs the following code on the console:


[ 'I\'m', 'a', 'little', 'tea', 'pot' ]

Where does this backslash come from? How to I prevent it?

No, because 'It\'s' is the string containing the characters It's, rather than the string 'It' followed by a syntax error caused by there not being a variable called s.

Does this mean that the backslash is supposed to be there?

Yes. If you remove the backlash escape, what does the code read as, bearing in mind that quote marks are used to surround strings?

This might be a help:

The backslash is used to escape the character after it, so that JS treats it as the actual character rather than as a bit of JS syntax

Not entirely sure why the output is showing it like that but I’m guessing the output code in the back is escaping.

If you loop the array and look at the words they look as you expected.

function titleCase(str) {
  let arr = str.split(" ");
  arr.forEach(word => console.log(word))
  return arr;
}

console.log(titleCase("I'm a little tea pot"));

/*
I'm
a
little
tea
pot
[ 'I\'m', 'a', 'little', 'tea', 'pot' ]
*/

Also, if you look at the browser console it will look as expected as well.