How to count elements inside all nested arrays

  arrays, count, javascript, nested

I recently started learning javascript and ran into a problem.

I wrote a little code that counts elements inside a nested array, but when adding an element to the first nested array, the code breaks. I don’t understand what the problem is.

var clothes = [
    ['cap', 'scarf'],                 //-- When adding a new element, the counter breaks !!!!!!!!!!
    ['T-shirt', 'shirt', 'trousers'], //-- When adding a new item, the counter works fine
    ['boots', 'sneakers']             //-- When adding a new item, the counter works fine

var totalItems = function () {
    for (var i = 0; i < clothes.length; i++) {
        var total = 0;
        for (var k = 0; k <= clothes[i].length; k++) {
            total = total + clothes[k].length;
        return total

console.log('all clothes: ' + totalItems());


*Uncaught TypeError: Cannot read property ‘length’ of undefined
> > at totalItems (test2.js:13)

at test2.js:31*

Please help and explain why the error occurs only when adding to the first nested array?

P.S. Thanks a lot in advance for any help
P.P.S. Sorry for my english ^^

Source: Ask Javascript Questions