Characters of a Text
Text is not an array, but in some ways it works like one. Each character in a text has an index, counted from 0, and you can read it with square brackets:
const language = "JavaScript";
console.log(language[0]);
console.log(language[4]);This prints:
J
SSpaces and punctuation marks are characters too, so "Hi, Tom" has 7 characters. A text also has the length property: the number of its characters. The last character is therefore at index .length - 1, just like the last element of an array:
console.log(language.length);
console.log(language[language.length - 1]);This prints:
10
tThere is one big difference, though: you can't change a character in a text. The code language[0] = "L"; does nothing. It doesn't cause an error, but the text stays "JavaScript". Once a text has been created, it can't be changed. You can only create a new text, for example by joining texts with +, or with one of the methods that texts have. The method toUpperCase() returns a new text in capital letters and leaves the original as it was:
language[0] = "L";
console.log(language);
const loud = language.toUpperCase();
console.log(loud);
console.log(language);This prints:
JavaScript
JAVASCRIPT
JavaScriptInstructions
Create a constant firstChar with the first character of firstName.
Create a constant lastChar with the last character of firstName. As with arrays, use firstName.length - 1 as the index.
Print both characters and the length of the name: console.log(firstChar, lastChar, firstName.length);. The console should show S t 8.