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 :: react native detect production 
Javascript :: dom get all tags 
Javascript :: discord.js get attachment url 
Javascript :: javascript string get last two character 
Javascript :: javascript check if array is empty 
Javascript :: favicon in next js not working 
Javascript :: jquery nth child 
Javascript :: get unique values from array of objects javascript 
Javascript :: ScrollController not attached to any scroll views 
Javascript :: a JavaScript function to multiply a set of numbers 
Javascript :: sqlite3 multithreading nodejs 
Javascript :: dotenv jest 
Javascript :: how to ask input in javascript 
Javascript :: node list files in directory 
Javascript :: js get transition duration 
Javascript :: jquery mouseup javascript 
Javascript :: javascript subtract days from date 
Javascript :: window.open javascript auto close 
Javascript :: removing duplicates in array javascript 
Javascript :: Laravel csrf token mismatch for ajax POST Request 
Javascript :: js is letter 
Javascript :: requirenativecomponent rnsscreen was not found in the uimanager 
Javascript :: If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project. That will permanently disable this message but you might encounter other issues. 
Javascript :: how to copy text on clipboard in react 
Javascript :: modify root in javascript 
Javascript :: string to JSONobject android 
Javascript :: hello world expressjs 
Javascript :: moment js add day 
Javascript :: Angular detecting escape key press 
Javascript :: js map value in range 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =