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:

  1. {k1:v1, k2:v2 etc} [see Object Literal Expression]
  2. Object.create(…) [see Create Object]

Add Property

New properties can be added to a existing object by:

obj.k = v

or

obj[k] = v

This will create or modify a property for the object.

[see Property Dot Notation / Bracket Notation]

// 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 }

Other ways to add/modify property are:

Delete Property

  1. delete obj[key] [see Delete operator]
  2. Reflect.deleteProperty( obj, key) [see Reflect.deleteProperty]

JavaScript Property

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc