JS: Create/Delete Property

By Xah Lee. Date: . Last updated: .

Properties can be created at the same time when you create a object. There are 2 ways:

{k1:v1, k2:v2, …}

[see JS: Object Literal Expression]

Object.create(Object.prototype,{k1:{value:v1},k2:{value:v2},…})

[see JS: Create Object]

Add Property

New properties can be added to a existing object by:

o[k] = v;. This will create or modify a property for the object.

Reflect.set(o, k) → Create or modify a property, function form. [see JS: Reflect.set]

Object.defineProperty(…) → Create or modify a property and its attributes. [see JS: Object.defineProperty]

Object.defineProperties(…) → Create or modify multiple properties at once. [see JS: Object.defineProperties]

// adding a new property

// creating object with property "k1" and "k2"
const obj = {"k1":1, "k2":2};

// adding a property
obj["p3"] = 3;

console.log( obj );             // { k1: 1, k2: 2, p3: 3 }

Delete Property

delete obj[key]

Delete a own property key of obj. Returns true even if property does not exist.

[see JS: Delete operator]

Reflect.deleteProperty (obj,key)

It returns true if succesful, else false.

[see JS: Reflect.deleteProperty]

JS Object Property Topic

  1. JS: Property Overview
  2. JS: Property Key
  3. JS: Property Dot Notation / Bracket Notation
  4. JS: Create/Delete Property
  5. JS: Get Property, Set Property
  6. JS: Check Property Existence
  7. JS: Access Property, List Properties
  8. JS: Property Attributes
  9. JS: Getter/Setter Properties
  10. JS: Property Descriptor
Liket it? Put $1 at patreon.

Or, Buy JavaScript in Depth