String methods(slice, touppercase, and charAt)

let name = "angel";

name = name.charAt(0).toUpperCase(0) + name.slice(0)

When I add 0, it adds another a at the beginning of the string(“Aangel” like that), but when I add 1, it doesn’t add another A and it capitalizes it? can someone explain why?

Like an array characters in a string are indexed

a n g e l
0 1 2 3 4

Also like an array they have a length property

const name = 'angel'
console.log(name.length) // 5

String.slice(begin, end) copies a section of a string. If no end argument is given it will slice to the last character of the string. So slice(1) will slice from and including index 1 to the last character index.

e.g. 'angel'.slice(1) // -> 'ngel'

I would recommend having a read of the MDN link for String.slice

  • name.charAt(0) is “a”
  • “a”.toUpperCase() is “A”
  • name.slice(0) is “angel”
  • name.slice(1) is “ngel”

note: toUpperCase() doesn’t use a function argument, so just ignores the 0 that you used.

You are asking about these two string combinations:

“A” + “angel”, and “A” + “ngel”

Hopefully that breakdown will help you to understand a bit more about things there.

1 Like

thank you

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.