Check arguments in JavaScript Object Literal Argument
19:47 22 Apr 2026

Question: What is a method to organize the code to check arguments in Object Literal Argument?

This question concerns code clarity, code textual minimization, and effectiveness.

For example, take this function call:

f({ obj1:{} });

//In plain argument list, arguments are easy to check:
function ff( toString, obj1 ){
    var existing = toString || obj1;
}
//In Object Literal Argument is not:
function f({ toString, obj1 }){
    //this gives false positive:
    var existing = toString || obj1;
};
function f(arg){
    //this fails also:
    let { toString, x } = {...arg};
    var existing = toString || obj1;
};
//fails
function f(arg){ let { toString=null, obj1=null }={...arg};
    console.log( toString || obj1 );
};
//fails
function f({ toString=null, obj1=null }){
    console.log( toString || obj1 );
};

//Only this works, but consumes too much text resources:
function f(arg){
    if( arg.hasOwnProperty( 'toString' ) ){
        var existing = arg.toString;
    } else if( arg.hasOwnProperty( 'obj1' ) ){
       var existing = arg.obj1;
    }
};

The argument name "toString' was intentionally taken to emphasize non-reliance on preemptively known default prototype.

Thank you.

javascript function-call