1

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
aikind
  • 98
  • 1
  • 2
  • 9
  • 1
    `printHello` is a [method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions) of the object . It is similar to `{ printHello: function() { console.log('Hello'); }` (There are a few differences though) – adiga Jul 28 '22 at 10:53

0 Answers0