Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

iterating string js

// Every one single answer

//1
var value = "alma";
var new_value = [...value].map((x) => x+"E").join("")
//2
let str = 'This is my string';
for(let character of str)  // secondary performance
  console.log(character)

//3
for (var i = 0; i < str.length; i++) {  // most performance
  alert(str.charAt(i));
}
//4
 for (var i = 0; i < str.length; i++) {
   alert(str[i]);
 }
//5
var i = str.length;
while (i--) {
  alert(str.charAt(i));
}
//6
 var i = str.length;
while (i--) {
  alert(str[i]);
}
//7
// ES6 version.
let result = ''; 
str.split('').forEach(letter => {  // almost third at performance
  result += letter;
});

//8
var result = '';
for (var letterIndex in str) {  
  result += str[letterIndex];
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #iterating #string #js
ADD COMMENT
Topic
Name
7+1 =