Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Arrays Intro
Making Arrays
Array Builder Kata
Array Elements
Human Names Kata
Array Length
Longest Array Kata
Looping Over Arrays
Highest Number Kata
Word Hunter Kata
Array Average Kata
Min-Max Kata
Adding Elements
Number Range Kata
Removing Elements
Arrays of Objects
Biggest Animal Kata
Arrays of Arrays
Taboo Words Kata
Going Further
Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Arrays Intro
Making Arrays
Array Builder Kata
Array Elements
Human Names Kata
Array Length
Longest Array Kata
Looping Over Arrays
Highest Number Kata
Word Hunter Kata
Array Average Kata
Min-Max Kata
Adding Elements
Number Range Kata
Removing Elements
Arrays of Objects
Biggest Animal Kata
Arrays of Arrays
Taboo Words Kata
Going Further
Looping Over Strings
Loops give us a way to act on each individual character in a string.
We can loop over a string by keeping track of the index
of the current character we are working with.
Each time the loop runs, we bump the index up by one. We stop once index
reaches the string's length
value, which means we hit the end of the string.
const myString = 'surprise'; let index = 0; while (index < myString.length) { let letter = myString[index]; console.log(letter); index++; }
We could also work backwards through a string if we wanted:
const myString = 'surprise'; let index = myString.length - 1; while (index >= 0) { let letter = myString[index]; console.log(letter); index--; }
Learning Goals
I can iterate over the characters in a string
Code Editor
Click "Run Code" to execute your JavaScript in a secure sandbox and see the output below.
Console Output
// Console output will appear here...
0 output lines