Concepts

Known-shape object construction

Try the free JavaScript compiler

Construct only expected fields from validated input instead of copying arbitrary keys.

Formal Syntax & Structure

javascript syntaxjavascript
const result = { allowedField: validatedValue }

Working Example

Example Snippetjavascript
(async () => {function prefs(x){return {theme:Object.hasOwn(x,"theme")&&["light","dark"].includes(x.theme)?x.theme:"light"};}const y=prefs({theme:"dark",extra:1});console.log(y.theme,Object.hasOwn(y,"extra"));})()
JavaScript Reference