I dont understanding. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I dont understanding.

var txt = "I can eat bananas all day"; var x = txt.slice(10,17);

28th Feb 2019, 7:14 PM
Ariful
Ariful - avatar
1 Answer
+ 3
So characters in strings all have indexes for the first one to the last beginning from 0. The slice method cuts out part of a string from the desired indexes (includes the first, excludes the last), so in this case it will be from the character with an index of 10 to the character before the character with an index of 17. Start counting from 0 on "I" (including spaces) and you will find the character with an index of 10 is b. Next the character with an index of 17 is the space after "s". So from "b" to before that space will give you the string "bananas" and that will be the value of x.
28th Feb 2019, 7:23 PM
LynTon
LynTon - avatar