Weensy
Weensy Code

Weensy Code

How to Access, Add, Modify and Delete a property from JavaScript object

Weensy's photo
Weensy
·May 31, 2022·

1 min read

Access

1. Dot notation

const obj = {
  notation: 'dot',
  application: {
    name: 'instagram',
    type: 'social'
}

obj.notation // = dot
obj.application.name // = instagram
obj.application.type // = social

2. Bracket notation

const obj = {
  notation: 'bracket',
  application: {
    name: 'calculator',
    type: 'utility'
}

obj['notation'] // = bracket
obj['application'].name // = calculator
obj.application['type'] // = utility

Add, Modify

1. Dot notation

const obj = {
    name: 'amex'
}

obj.type = 'finance' // Add type
obj.name = 'binance' // Change name

obj.name // = binance
obj.type // = finance

2. Bracket notation

const obj = {
  name: 'nike',
}

obj['type'] = 'shopping' //Add type
obj['name'] = 'ikea' // Change name

obj['name'] // = ikea
obj['type'] // = shopping

Delete

const obj = {
  name: 'gosegu'
  height: 300
}

delete obj.name // Delete name
delete obj[height] //Delete height

obj.name // = undefined
obj[height] // = undefined
 
Share this