-1
var s= {type: "beetle", price : "xxx", popular: "yes" ,.... };

If I have to remove all the properties of s how should I do it?

  • Should it be delete s.type; delete s.price; delete s.popular .. so on.

  • Or s = null;?

  • Or would s going out of scope delete the properties?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    Possible duplicate of [When should I use delete vs setting elements to null in JavaScript?](https://stackoverflow.com/questions/1947995/when-should-i-use-delete-vs-setting-elements-to-null-in-javascript) – Rafael Herscovici Oct 05 '18 at 08:56
  • 1
    `delete s` doesn't do anything - the `delete` operator is used to delete a _property_ of an object, not the object itself. See [help](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete) – Dunc Oct 05 '18 at 08:58
  • no. but instead you can just do `s = undefined;` this way you its good as deleting the variable. – John Michael Villegas Oct 05 '18 at 09:01
  • Yes I mean delete s.car; forgot to put it as s.car. Thanks – vishnu prasad.v Oct 05 '18 at 09:03
  • @JohnMichaelVillegas Yes makingit undefined is correct. But How is s= null different from delete s.car ? we are removing s's reference to car. – vishnu prasad.v Oct 05 '18 at 09:04
  • delete is for the property. so if you do `delete s.car` it will return a empty object since `car` is the only property of `s` variable. but `s = null` is setting the variable `s` to value which is `null`. if you have another property in `s` variable but in a child property of `car` if you execute `delete s.car` the proter `car` will be gone and the remaining property will remain. – John Michael Villegas Oct 05 '18 at 09:12
  • 1
    final note: `delete s.car` is not same as `s = null` `delete s.car;` console.log(s) // print {} `s = null;` console.log(s) //print null – John Michael Villegas Oct 05 '18 at 09:16
  • Not same for s but both handles property car in the same way or different ways? – vishnu prasad.v Oct 05 '18 at 09:22
  • @JohnMichaelVillegas PLease look into my question now as I have stated it clearly. Thanks. – vishnu prasad.v Oct 05 '18 at 09:38
  • I'm still waiting for the perfect solution. Thanks ! – vishnu prasad.v Oct 06 '18 at 07:00
  • don't set it as `null` since null is still a value. doing `s = undefined` will delete the variable `s`. if you `delete s.` the value of `s` will become `{}` – John Michael Villegas Oct 09 '18 at 10:07
  • in a nutshell 1. `delete` will remove the property of a object. if all property will remove the remaining value will be `{}` 2. `s = null` will *SET* a new value of the variable `s` to `null` 3. `s = undefined` will *SET* the variable in to `undefined` state. – John Michael Villegas Oct 09 '18 at 10:09
  • bottom line `delete`, `s = null, `s = undefined` are all **NOT THE SAME** – John Michael Villegas Oct 09 '18 at 10:11

3 Answers3

0

In MDN it said, "The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically."

So that totally different one is set an object to be a null object and another won't work because s is not and property of any object.

Loic
  • 160
  • 2
  • 13
0

When you set a property equals to null, it still exists but hasn't any value but if you delete it, it will no longer exist.

Pouya Jabbarisani
  • 1,084
  • 3
  • 16
  • 29
0

They're not the same at all. Deleting the car property of s still leaves it as an object - admittedly it is an "empty" object with no properties of its own, but that is a very different value from null. For one thing, {} is still an object, and delegates to Object.prototype, so can access methods like s.toString() (which is used internally for coercion to a string value) and s.hasOwnProperty(propname) - null is just a primitive value which you can't call any methods on at all. For another, {} becomes true if coerced to Boolean (eg if you do if (s)), while null coerces to false.

So the values actually behave very differently.

Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34
  • Agreed but my concern is not about the s, the main thing is the deletion. Is the property deleted in both ways?. – vishnu prasad.v Oct 05 '18 at 09:10
  • @vishnuprasad.v I'm not entirely sure I understand the question. In both cases the property is deleted, in the sense that `s.car` will no longer work. But note that if `s` is `null`, then `s.car` raises a `TypeError`, while if `s` is `{}` (as it is after `delete s.car`), `s.car` raises no error and returns `undefined`. Some additional context behind the question would help to give a useful answer. – Robin Zigmond Oct 05 '18 at 09:21
  • Thanks Robin ! I have updated the question please let me know if you find the question convincing. Thanks – vishnu prasad.v Oct 05 '18 at 09:24
  • Zignmond - Thanks actually you have given me the answer - s.car is deleted in both the cases so instead of deleting all the properties one by one i can just use s = null to remove them. THis is just for the deletion of properties which i dont need anymore. – vishnu prasad.v Oct 05 '18 at 09:29