Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

582
Views
what is difference between using delete keyword , assign undefined value to object literal in javascript?

I met some needs for deep copying original object literals, excepts some keys. I know spread operator doesn't copy deeply whole nested objects, however it's not main points for this question, so let's pass that issues.

so, back in original question, I see no difference between using delete keyword and assign undefined to target property which I want to remove.

const original = {
   a: 1,
   b: 2,
}

const copied = {
  ...original,
  b: undefined
}

const explicitDelete = {
 ...original
}
delete explicitDelete["b"]

seems copied way is less verbose, but it's totally okay with that way?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I see no difference between using delete keyword and assign undefined to target property

The difference is that the property still exists with the value undefined:

"b" in copied // true
"b" in explicitDelete // false

If you don't like the verbosity of delete, you can use a helper function

function omit(obj, ...keys) {
  const res = {};
  for (const p in obj)
    if (!key.includes(p))
      res[p] = obj[p];
  return res;
}
omit(orignal, "b")

or object destructuring with rest syntax:

const {b:_, ...withoutB} = original;

See also How to omit specific properties from an object in JavaScript, How do I remove a property from a JavaScript object?, How can I clone a JavaScript object except for one key?.

about 4 years ago · Juan Pablo Isaza Report

0

There is definitely a difference. When you use delete, the object will no longer have the property name at all. The in operator will indicate that. Setting the property value to undefined will not have that effect.

about 4 years ago · Juan Pablo Isaza Report

0

delete removes the entry, that's the difference

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!