How to iterate over a string with index in Node.js

2 Answers

0 votes
const str = 'node.js';
 
for (let i = 0; i < str.length; i++) {
    console.log(str[i], i);
}
 
 
 
  
/*
run:
  
n 0
o 1
d 2
e 3
. 4
j 5
s 6
  
*/

 



answered Feb 27, 2022 by avibootz
0 votes
const str = 'node.js';
 
const arr = [...str];
 
arr.forEach((ch, index) => {
    console.log(ch, index);
});
 
 
 
  
/*
run:
  
n 0
o 1
d 2
e 3
. 4
j 5
s 6
  
*/

 



answered Feb 27, 2022 by avibootz

Related questions

1 answer 210 views
2 answers 127 views
3 answers 138 views
2 answers 174 views
1 answer 181 views
2 answers 208 views
208 views asked Feb 28, 2022 by avibootz
3 answers 357 views
357 views asked Jan 20, 2022 by avibootz
...