JAVASCRIPT
- Understanding Hoisting in JavaScript
- Understanding Closures in JavaScript: A Complete Guide for Beginners
- Understanding Anonymous Functions in JavaScript
- JavaScript Functions: A Complete Guide
- Var vs let vs const in JavaScript
- Understanding Temporal Dead Zone (TDZ) in JavaScript
- what is surrounding state and lexical environment
- Understanding the Scope Chain in JavaScript
- How Javascript Works And Code is executed
- What is undefined and null in JavaScript?
- What are template literals in JavaScript?
- What is the difference between typeof and instanceof?
- What is a callback function?
- Explain IIFE (Immediately Invoked Function Expression).
- What is the difference between function declarations and function expressions?
- What is the arguments object in JavaScript?
- What are rest parameters and default parameters?
- What are first-class functions?
- What is the difference between call, apply, and bind?
- Explain private variables in JavaScript.
- What is an object in JavaScript?
- What are object properties and methods?
- What are getters and setters in JavaScript?
- Difference between object literal and constructor function.
- What is the difference between Object.freeze and Object.seal?
- How does Object.create() work?
- What is the DOM?
- How do you prevent default behavior of events?
- Difference between innerHTML, innerText, and textContent.
- Understanding Events in JavaScript: A Complete Guide
- How to Chain Promises in JavaScript? (Complete Guide)
- What is the difference between function scope and block scope?
- What are data attributes?
- Understanding the JavaScript Event Loop: A Complete Guide
- Explain this keyword in JavaScript.
- What is prototype and prototypal inheritance?
- How do you add or remove properties from an object?
- Difference between array and object.
- What is event delegation?
- What are array methods like map, filter, reduce?
- How do you clone an array?
- Difference between forEach and map.
- What is destructuring?
- What are template literals?
- What are modules in JavaScript?
- What is spread and rest operator?
- Difference between import and require.
- What is a promise?
- How do you remove an event listener?
- Explain async/await in JavaScript.
- Difference between HTMLCollection and NodeList.
- How do you select elements in DOM?
- Explain event delegation.
- What is a generator function?
- What is the difference between bubbling and capturing?
- How to create and remove DOM elements dynamically?
- How to traverse the DOM?
- Explain event bubbling and capturing.
- Difference between onclick and addEventListener.
- What is preventDefault and stopPropagation?
- How to handle keyboard events?
- How to handle mouse events?
- How to handle form submission using JS?
- Difference between synchronous and asynchronous code.
- Explain memoization in JavaScript.
- Explain debounce and throttle.
- Difference between shallow copy and deep copy.
- What is Proxy and Reflect in JavaScript?
- Explain Symbol in JavaScript.
How do you add or remove properties from an object?
1. Adding Properties
You can add new properties to an object in multiple ways:
✅ Dot notation
let user = {};
user.name = "John";
user.age = 25;
console.log(user); // { name: "John", age: 25 }
✅ Bracket notation (useful for dynamic keys or keys with spaces/special chars)
let user = {};
user["email"] = "john@example.com";
let key = "phoneNumber";
user[key] = "1234567890";
console.log(user); // { email: "john@example.com", phoneNumber: "1234567890" }
✅ Object.assign() (merge properties into an object)
let user = { name: "John" };
Object.assign(user, { age: 25, country: "India" });
console.log(user); // { name: "John", age: 25, country: "India" }
✅ Spread operator (...)
let user = { name: "John" };
user = { ...user, age: 25, country: "India" };
console.log(user); // { name: "John", age: 25, country: "India" }
2. Removing Properties
✅ delete operator
let user = { name: "John", age: 25 };
delete user.age;
console.log(user); // { name: "John" }
⚠️ Note: delete removes a property from the object but does not affect the prototype.
✅ Destructuring + rest (...)
let user = { name: "John", age: 25, country: "India" };
let { age, ...rest } = user;
console.log(rest); // { name: "John", country: "India" }
(This doesn’t mutate the original object; instead, it creates a new one without age.)
✅ Summary:
- Add:
obj.prop = value,obj["key"] = value,Object.assign(), spread operator. - Remove:
delete obj.prop, or create a new object without that property using destructuring.
No comments yet! You be the first to comment.
