Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

string iterate in js

var text = 'uololooo';

// With simple for loop
for(let i=0; i<text.length; i++){
  console.log(text[i])
}

// With ES6
[...text].forEach(c => console.log(c))

// With the `of` operator
for (const c of text) {
    console.log(c)
}

// With ES5
for (var x = 0, c=''; c = text.charAt(x); x++) { 
    console.log(c); 
}

// ES5 without the for loop:
text.split('').forEach(function(c) {
    console.log(c);
});
Comment

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];
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery animate transform 
Javascript :: copy on clip board 
Javascript :: js do while loop 
Javascript :: Setting darkmode using Tailwind 
Javascript :: wheel 
Javascript :: simple user agent parse js 
Javascript :: add svg in react 
Javascript :: create a promise in javascript 
Javascript :: sort array of numbers js 
Javascript :: javascript compare number 
Javascript :: react pass object as props 
Javascript :: javascript floating point addition 
Javascript :: how to add an event listener to a function javascript 
Javascript :: Reactjs function exemple useEffect 
Javascript :: how to slice array in angular 6 
Javascript :: javascript dynamic variable name 
Javascript :: for loop on array in javascript 
Javascript :: check if string Array javascript 
Javascript :: vs code jsconfig 
Javascript :: bresenham algorithm 
Javascript :: fix slow loading images in react 
Javascript :: chrome extension catch shortcut 
Javascript :: javascript scrollby div 
Javascript :: How to pass json format data on ajax call 
Javascript :: vuejs pass all events to child 
Javascript :: how to mark a icon with vector icons in mapview 
Javascript :: apollo uselazyquery oncompleted 
Javascript :: interpolation in js 
Javascript :: post request enabled in express js 
Javascript :: browser support 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =