Tell me how to change the function:

function isTrueObject(val) { let bool; if (val instanceof Array || val === null) { bool = false; } else if (typeof val === 'object') { bool = true; } else bool = false; return bool; } console.log(isTrueObject({ x: 1 })); console.log(isTrueObject(1)); console.log(isTrueObject(0)); console.log(isTrueObject('jfkjv')); console.log(isTrueObject(false)); 

what would be this view:

 function isTrueObject(val) { return expression; } 

  • Essentially instanceof and instanceof returns true / false and I need to use those values ​​in order to find out if I pass an object or something else to the function - Anna
  • It is better to use Array.isArray . The current check with instanceof can be instanceof in certain cases. - user207618

1 answer 1

Like this

 function isTrueObject(val) { return val instanceof Array || val === null ? false : typeof val === 'object' ? true : false; } console.log(isTrueObject({ x: 1 })); console.log(isTrueObject(1)); console.log(isTrueObject(0)); console.log(isTrueObject('jfkjv')); console.log(isTrueObject(false)); 

  • in fact, this is the same as mine, only a note through a ternary operator - Anna
  • @ Anna Well, yes, the question was how to convert the function code to a return expression ? - tilin
  • essentially instanceof and instanceof returns true / false and I need to use those values ​​in order to find out if I am passing an object or something else to the function - Anna