delete obj.property vs obj.property = undefined
Written byPhuoc Nguyen
Category
JavaScript
Created
10 Jun, 2020
Assume that
`obj`
is an object, and `property`
is the name of its property.#Differences
-
Calling
`obj.property = undefined`
sets the value of property to`undefined`
. The property is still there and appears if we iterate the properties of the object.let person = { name: 'John' };person.name = undefined;person.hasOwnProperty('name'); // truename in person; // trueObject.keys(person); // ['name']for (let p in person) {console.log(p);} // 'name'`delete obj.property`
will remove the property from the object. Let's revisit the sample code above, now with`delete person.name`
:let person = { name: 'John' };delete person.name;person.hasOwnProperty('name'); // falsename in person; // falseObject.keys(person); // An empty array// Nothing is shown up in the Consolefor (let p in person) {console.log(p);} -
`delete`
can't delete an inherited property.const car = { branch: 'Audi' };const a4 = Object.create(car);console.log(a4.branch); // 'Audi'delete a4.branch;console.log(a4.branch); // 'Audi'In this case, we have to set the property to`undefined`
:a4.branch = undefined;console.log(a4.branch); // undefined
#Good to know
-
`delete`
doesn't work with array:const array = [1, 2, 3, 4, 5];delete array[1];console.log(array); // [1, empty, 3, 4, 5]If you want to remove an item from an array, use the`splice`
method.const array = [1, 2, 3, 4, 5];array.splice(2, 1);console.log(array); // [1, 2, 4, 5]There's an alternative method to remove the last element from array:const array = [1, 2, 3, 4, 5];array.pop();console.log(array); // [1, 2, 3, 4] -
We can use the ES6 spread operator to remove a property from an object:const { name, ...rest } = { name: 'Foo', age: 20 };console.log(name); // 'Foo'console.log(rest); // { age: '20' }It's also possible to remove a dynamic property:const property = 'name';const { [property]: value, ...rest } = { name: 'Foo', age: 20 };console.log(value); // 'Foo'console.log(rest); // { age: '20' }
Questions? 🙋
Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn