Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ?. and ?? operator in javascript do? [duplicate]

I recently noticed usage like this in a java script code base what does it do. I was unable to find any relevant documentation regarding that. Though intuitively operators seem to checking whether property is present. Is there any official documentation regarding this.

Ex:

args?.propertyName !== 'someValue'
const value = props.someProp ?? props.defaultProp;
like image 897
Sai Teja Talasila Avatar asked Jun 18 '26 04:06

Sai Teja Talasila


1 Answers

They are for optionals:

val ?? other is called nullish coalescing operator and is equivalent to val == null ? other : val

and optionalThing?.property is refered as optional chaining and is the same as optionalThing == null ? null : optionalThing.property

This optional chaining expressions result in shorter and simpler expressions when accessing chained properties when the possibility exists that a reference may be missing ( allows you to do things like optionalThing?.optionalProperty?.anotherOptionalProperty?.property ).

like image 75
jeprubio Avatar answered Jun 20 '26 17:06

jeprubio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!