Uppercase Array

Hello :),

so… I was testing around and searching some nice functions to practise and I found this:

function uppercase(str)
{
  var array1 = str.split(' ');
  var newarray1 = [];
    
  for(var x = 0; x < array1.length; x++){
      newarray1.push(array1[x].charAt(0).toUpperCase()+array1[x].slice(1));
  }
  return newarray1.join(' ');
}
console.log(uppercase("the quick brown fox"));

This Function makes the first letter from a String to Uppercase.
I understand all functions and how they work but What I didnt understand is this +
on this line :

 newarray1.push(array1[x].charAt(0).toUpperCase()+array1[x].slice(1));

.toUpperCase() + etc…

what does this + do and why does it work?

+ is string concatenation. charAt will give the first letter of each word, while slice gives you the remaining letters.

you can always add a .log() to inspect your code:

console.log(array1[x].charAt(0).toUpperCase(), array1[x].slice(1));
newarray1.push(array1[x].charAt(0).toUpperCase()+array1[x].slice(1));

So, you can concatonate methods?

no, but these methods return strings. And you can concatenate strings.

1 Like