Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Arrays Methods: Part 3

How come a set of string is equal to one slice?

When I wanted to slice from the word quick to fox, how come I am slicing the words and not the letters?

 <script>
      var first =  ["The", "quick", "brown", "fox", "jumps"]
      var second =  ["over", "the", "lazy"];
      var saying = first.concat("over", "the", "lazy","dog"); 
      var shortSaying = saying.slice(1,4);
          saying.slice()
      var sayingString = saying;
    </script>

4 Answers

Since saying is an array of strings, it is the array that you're slicing when you retrieve ["quick", "brown", "fox"].

It can be confusing because arrays of strings and strings can both be sliced. Compare:

['a','b','c','d'].slice(1, 3) // equals ["b", "c"]
'abcd'.slice(1,3) // equals "bc"
Laura Cressman
Laura Cressman
12,548 Points

The data stored in the variables first, second, and saying is an array, not a string. Therefore, when you use the slice method on saying, you are taking the values in positions 1, 2, and 3 of the array (so the words "quick", "brown", and "fox", since the position numbers in an array begin with the number 0). If instead you were using the slice method on a string, like "quick", then you would be slicing the characters. Does that make sense? Smile more:) Laura

The concat method (used on an array) will return an array. Which means saying is an array - and the slice method too will result in an array...

I understand now. Thanks everybody!