Why do I need an else statement to check for keys in an object in Javascript?

I’m doing Leetcode Question Number 49..

I entered this but it did not work.

var groupAnagrams = function(strs) {
    if (strs.length <= 1) {
        return [strs]
    }

    const hashMap = {}

    for(let i = 0; i < strs.length; i++) {
        const currentKey = strs[i].split('').sort().join('')

        if (hashMap[currentKey]) {
            hashMap[currentKey].push(strs[i])
        }

        hashMap[currentKey] = [strs[i]]
    }

    return Object.values(hashMap)
};

Then I asked ChatGPT and it asked me to put it in an else statement

var groupAnagrams = function(strs) {
    if (strs.length <= 1) {
        return [strs]
    }

    const hashMap = {}

    for(let i = 0; i < strs.length; i++) {
        const currentKey = strs[i].split('').sort().join('')

        if (hashMap[currentKey]) {
            hashMap[currentKey].push(strs[i])
        } else {
            hashMap[currentKey] = [strs[i]]
        }
    }

    return Object.values(hashMap)
};

It works, but I don’t get why/how it works. Why does it need to be in an else statement? Is it because I’m not returning anything? I thought the event loop will skip the command inside the if statement if the condition is not met?