6 August 2024

The simplest way, but this approach only works if you are sure that the values of test.test and test.test2 are not falsy (i.e., not 0, null, undefined, false, NaN, or an empty string ''). If you need to ensure the properties simply exist and can hold any value (including falsy values), you should use a different approach.

Source code viewer
  1. const test = {
  2. test: 'value1',
  3. test2: 'value2'
  4. };
  5.  
  6. if (test.test && test.test2) {
  7. console.log("Both 'test' and 'test2' exist and are truthy");
  8. } else {
  9. console.log("One or both parameters do not exist or are not truthy");
  10. }
Programming Language: ECMAScript