.length
, when used in a string (in this case, 'mozzilad'
), returns the number of characters of this string. 'mozzilad'
has 8 characters, carrying browserType.length - 1
is equal to 8 - 1 = 7.
By accessing browserType as an array, using the index browserType.length-1
, browserType[browserType.length-1]
will return the 8th character, which is at index 7 of the array.
0 1 2 3 4 5 6 7 // Índices da array
1º 2º 3º 4º 5º 6º 7º 8º // browserType possui 8 caracteres
browserType: [m, o, z, z, i, l, a, d]
^ // Caractere a ser retornado
Remember that the array indexes start counting from 0 (index 0, index 1, etc.), and the number of characters starts from 1 (1st character, 2nd character, etc.), so -1 is placed in the .length
.
Leandro, if one of the answers solved your problem, you can choose the one that best solved it and accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. You can only accept one of them, but don’t forget that you can also vote in all the answers you found useful.
– hkotsubo