You should test explicitly for `null` and `undefined` here, otherwise replacing the first element (index = 0) doesn't work.