Articles in this section

hasOwn helper

The hasOwn helper checks whether a specified key exists as a direct property of an object and returns true if it does. Use it to verify whether optional configuration fields or settings are present before referencing them.

Syntax

{{hasOwn object "key"}}

object -- The object to check. Required.

key -- The property name to test. Required. Wrap in double quotes.

Examples

Check for existing and missing properties

{{hasOwn record.config "theme"}}
{{hasOwn record.config "version"}}

Input:

{
  "record": {
    "config": {
      "theme": "dark",
      "debug": true
    }
  }
}

Output:

true
false

"theme" exists directly on record.config, so the first expression returns true. "version" is not a property of record.config, so the second returns false.

Use in a conditional block

{{#if (hasOwn record.config "debug")}}
  Debug mode enabled
{{else}}
  Debug property not found
{{/if}}

Input:

{
  "record": {
    "config": {
      "theme": "light",
      "debug": true
    }
  }
}

Output: Debug mode enabled

Wrap hasOwn in parentheses to use it as an inline expression inside #if.

Usage notes

  • hasOwn checks only direct (own) properties -- it does not check inherited properties from prototypes.
  • Wrap the key argument in double quotes.
  • Wrap hasOwn in parentheses when using it as an inline expression: {{#if (hasOwn object "key")}}.
  • To check whether a field has a non-empty value (rather than just whether it exists), use #if directly on the field instead.