Javascript slice (X,Y) comprehension

Hey there,
I have watched some Youtube videos but still can´t understand why the following

 function forecast(arr) {
  // change code below this line
  return arr.slice(2, 4);

// do not change code below this line
  forecast(["cold", "rainy", "warm", "sunny", "cool", "thunderstorms"])

results in [sunny, warm] I understand that from this: return arr.slice(2, 4)
the index position is 2 hence sunny and 4 I thought was the number of parameters to be sliced. In a Youtube video I understood that the last parameter is not included. I am confused…

The example is correct. The last element is not included, the first element represents the index from where it starts.
You can read more here.
IMO MDN Web Docs should be your main information gathering place when it comes to js.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Ok,thanks. Duly noted!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.