Return an object of functions without specifying key-value pair in JavaScript

Viewed 30

Could I please have an explanation on why returning an object of functions without specifying a key-value pair is valid? From my understanding of objects in JavaScript, objects must have key-value pairs, but the object being returned below does not.

function returnFunc() {
    return {
        printHello() {
            console.log('Hello');
        },
        printBye() {
            console.log('Bye');
        }
    }
}

returnFunc().printHello();

And if I try to return a literal without specifying a key-value pair, I get an error as expected.

function returnLiteral() { 
    return {
        'Hello',
        'Bye'
    }
} // THIS FUNCTION IS NOT VALID
0 Answers
Related