Check if a property exists in the object or array: `in`
Check if a property exists in the object or array: in
If you have an object and you want to check if a property is present you can use the in
operator.
const fruits = { strawberry: 'Strawberry', kiwi: 'Kiwi', lemon: 'Lemon'}// for checking the presence of an property you can use in operatorif ('kiwi' in fruits) { console.log('Kiwi is present')}// you can use in the short formconsole.log('kiwi' in fruits ? 'Yes! Kiwi' : 'ther is no kiwi here')
I think the in
operator is one of the best ways to check the presence of a property because, for example, if you want to check if the value is undefined
, you canβt distinguish the cases if the property doesnβt exist or the property exists and has an undefined value:
const fruits = { strawberry: 'Strawberry', kiwi: 'Kiwi', lemon: 'Lemon'}
fruits.something = undefinedif (fruits.somethingelse === undefined) { console.log('Somethingelse property does not exist')}if (fruits.something === undefined) { console.log('Something property exists but the value is undefined')}
The in
operator works also with the arrays. With the arrays, you can check the presence of the numeric index:
const fruitsArray = []fruitsArray.push('Strawberry')fruitsArray.push('Kiwi')fruitsArray.push('Lemon')console.dir(fruitsArray)console.log(2 in fruitsArray) // trueconsole.log(3 in fruitsArray) // false