Cannot read property 'charAt' of undefined

Can anyone please help me in understanding what am i doing wrong here. I am new to JavaScript programming.I am getting this error:

Cannot read property ‘charAt’ of undefined.

//Return the first letter of given string. 
function titleCase(str) {
  var newStr = str.split(' ');
  for (var i = 0; i <= newStr.length; i++) {
    console.log(newStr[i].charAt(0));
  }
}
titleCase("Hi there.");

<!-- end snippet -->

62 thoughts on “Cannot read property 'charAt' of undefined”

Leave a Comment