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:

〔►see JavaScript: Creating 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) → 〔►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

Two ways to delete a property.

Delete Operator

delete obj[key] → delete a own property key of obj.

The delete operator never goes up the prototype chain.

delete returns true even if property does not exist.

〔►see JavaScript: Delete operator

Reflect.deleteProperty

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: Dot Notation vs Bracket Notation for Properties
  4. JavaScript: Create/Delete Property
  5. JavaScript: Read/Write to Property and Prototype Chain
  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.