0

Why do I get numbers (0 through 9) for trying to print each character in the string? The string's length is 10 so am I getting the index of each letter? How can I get the characters instead? I would like to get the output that Python would provide in similar code (code for JS and Python below together with its own output). I tried this on Google Chrome's console. Code and output below:

strng = 'This is me';
for (var charac in strng)
{
     console.log(charac);
}

Output is

0
1
2
3
4
5
6
7
8
9

Python which also has the for-in format would print each character per line. And this is what I want. Code and output below from terminals iPython:

strng = 'This is me'

for charac in strng:
   print(charac)

Python output is:

T
h
i
s

i
s

m
e
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33

1 Answers1

2

From MDN:

The for...in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.

Try to use the of statement:

strng = 'This is me';
for (var charac of strng)
{
     console.log(charac);
}