Strings 3/5
Accessing Character:
We can also access different characters from a string variable, and for that, we use the square brackets []. The syntaxes for accessing characters are str.charAt(pos) and variableName(pos) where pos is the position or index of the character that we want to access and variableName is the variable to be sliced. Let’s look at an example.
123let str = 'JavaScript'; console.log(str[0] ); console.log( str.charAt(4));
Swipe to start coding
A string value, England, is stored in a variable named country. The task is to display the letter a of the variable England.
Løsning
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain the difference between using square brackets and the charAt() method?
What will happen if I try to access an index that is out of range?
Can you show more examples of accessing characters in a string?
Fantastisk!
Completion rate forbedret til 2
Strings 3/5
Stryg for at vise menuen
Accessing Character:
We can also access different characters from a string variable, and for that, we use the square brackets []. The syntaxes for accessing characters are str.charAt(pos) and variableName(pos) where pos is the position or index of the character that we want to access and variableName is the variable to be sliced. Let’s look at an example.
123let str = 'JavaScript'; console.log(str[0] ); console.log( str.charAt(4));
Swipe to start coding
A string value, England, is stored in a variable named country. The task is to display the letter a of the variable England.
Løsning
Tak for dine kommentarer!
single