Syntax
Object.hasOwn(record, "key");
Object.fromEntries([["key", "value"]]);Examples
Own Properties and Object.fromEntries: working example
Run this standalone example in a modern browser console unless another environment is specified. Expected result: The console prints true false, then true 3.
const record = Object.create({ inherited: 1 });
record.count = 0;
console.log("inherited" in record, Object.hasOwn(record, "inherited"));
const prices = Object.fromEntries([["tea", 3], ["coffee", 4]]);
console.log(Object.hasOwn(record, "count"), prices.tea);- Expected result: The console prints true false, then true 3.
- The in operator sees the prototype property; hasOwn does not.
- An own property can exist even when its value is falsy, so truthiness is not an existence check.
Best practices
- Use Map when keys are not naturally object property names.
- Remember that repeated keys in fromEntries overwrite earlier values.
Runtime & versions
Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
ECMAScript specification