JavaScript: 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 JavaScript: Object Literal Expression

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

〔►see JavaScript: 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 JavaScript: Reflect.set

Object.defineProperty(…) → Create or modify a property and its attributes. 〔►see JavaScript: Object.defineProperty

Object.defineProperties(…) → Create or modify multiple properties at once. 〔►see JavaScript: Object.defineProperties

// adding a new property

// creating object with property "k1" and "k2"
var 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 JavaScript: Delete operator

Reflect.deleteProperty (obj,key)

It returns true if succesful, else false.

〔►see JavaScript: Reflect.deleteProperty

JS Object Property Topic

  1. JavaScript: Property Overview
  2. JavaScript: Property Key
  3. JavaScript: Property Dot Notation / Bracket Notation
  4. JavaScript: Create/Delete Property
  5. JavaScript: Get Property, Set Property
  6. JavaScript: Check Property Existence
  7. JavaScript: Access Property, List Properties
  8. JavaScript: Property Attributes
  9. JavaScript: Getter/Setter Properties
  10. JavaScript: Property Descriptor
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.