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

270
Views
Can Optional Chaining be alternative of 'in' operator in Javascript

I often saw in operator in some library of Javascript. but I think in operator has risk of causing human errors. because we have to write property name as string type.

I think optional chaining could be alternative of in operator and It looks more safe. we can get help of intelligence of IDE during writing code.

what is the benefit of using in keywords instead of Optional Chaining?

enter image description here

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

0

AFAIK, in is used to get to know whether the property exists in an object or its prototype or not.

const address = {
  zipCode: 1234,
  country: 'usa',
  city: 'new York',
};

if ('zipCode' in address) {
  console.log('This is address from in checking');
}

and if you are using optional chaining then you are checking the value to be truthy or not

const address = {
    zipCode: 1234,
    country: 'usa',
    city: 'new York',
};

if (address?.zipCode) {
    console.log('This is address from optional chaining');
}


But think of a situation where the value of that property is a falsy value then the in operator will give you the result of property existence. If the value is a falsy value then you have to make additional checks to know whether the property exists or not.

Value can be anything so you have to make necessary checks in optional chaining

const address = {
  zipCode: 0,
  country: 'usa',
  city: 'new York',
};

if ('zipCode' in address) {
  console.log('This is address from in checking');
}

if (address?.zipCode) {  // Won't work
  console.log('This is address from optional chaining');
}

if (address?.zipCode || address?.zipCode === 0) {  // Will work with extra checks
  console.log('This is address from optional chaining');
}

about 4 years ago · Juan Pablo Isaza Report

0

You're comparing 2 different things

In your context, in operator is similar to Object.hasOwnProperty, and its returned value is true/false.

const address = {
   zipCode: 123
}

console.log('zipCode' in address) //true

console.log(address.hasOwnProperty('zipCode')) //true

Optional chaining is to check undefined values optionally

//the short form of `address && address.zipCode`
console.log(address?.zipCode) //false - because no `address` variable

You can consider this case that zipCode property is there but its value is undefined

const address = {
   zipCode: undefined
}

console.log('zipCode' in address) //true

console.log(address?.zipCode) //undefined!

From the above example, you can see that zipCode is in address, but optional chaining does not help you to check it but returns its value instead.

what is the benefit of using in keywords instead of Optional Chaining?

In the conclusion, you should use in when you want to check properties in that object exist or not. In other cases, you can use optional chaining to not only verify property availability but also its values (0, null, and undefined).

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!