How do you count the index of a string in JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

How do you count the index of a string in JavaScript?

For instance, the code below outputs 8 but I really don't understand why it gives 8 as the output var str = "This is SoloLearn"; var pos = str.indexOf("Solo"); alert(pos++);

5th Apr 2019, 6:24 AM
eMBee
eMBee - avatar
4 Answers
+ 14
Mofey Solo start position at 8. 8th position is S. javascript start count 0.
5th Apr 2019, 6:35 AM
Ahad
Ahad - avatar
+ 18
It gives 8 as the output since the index of any string starts with 0. The index of "Solo" is 8 so the pos variable will store 8 and display it and after that it will increment the pos value since pos++ is a post increment operation , firstly the value will be assigned then it will be incremented
5th Apr 2019, 7:50 AM
Roma Mandal
Roma Mandal - avatar
+ 10
Welcome Mofey
5th Apr 2019, 6:39 AM
Ahad
Ahad - avatar
+ 2
Oh, thank you so much <rabby> </ahad> At least I can say I've learnt something new today
5th Apr 2019, 6:37 AM
eMBee
eMBee - avatar