What is the obj?.prop syntax in javascript? [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
Null-safe property access (and conditional assignment) in ES6/2015
9 answers
I was looking through a code and I came across this:
{{abc?.xvy=== tyu?abc?.xz:abc?.xz}}
I am unable to understand meaning of this expression. I know that it is Null-safe property access but I am bit confused about the chaining.
Any help is much appreciated
javascript ecmascript-5
marked as duplicate by adiga, Samuel Liew♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 5 at 7:54
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Null-safe property access (and conditional assignment) in ES6/2015
9 answers
I was looking through a code and I came across this:
{{abc?.xvy=== tyu?abc?.xz:abc?.xz}}
I am unable to understand meaning of this expression. I know that it is Null-safe property access but I am bit confused about the chaining.
Any help is much appreciated
javascript ecmascript-5
marked as duplicate by adiga, Samuel Liew♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 5 at 7:54
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I hope this code isn't used anywhere.
– Eric Duminil
Feb 5 at 7:39
unfortunately I saw it is used. Why is it not supposed to be used ?
– Apurva Pathak
Feb 5 at 8:56
It really isn't readable at all,abc
,xvy
tyu
are too generic to be useful and the ternay operator is useless because both outcomes are the same, as mentioned here
– Eric Duminil
Feb 5 at 8:57
add a comment |
This question already has an answer here:
Null-safe property access (and conditional assignment) in ES6/2015
9 answers
I was looking through a code and I came across this:
{{abc?.xvy=== tyu?abc?.xz:abc?.xz}}
I am unable to understand meaning of this expression. I know that it is Null-safe property access but I am bit confused about the chaining.
Any help is much appreciated
javascript ecmascript-5
This question already has an answer here:
Null-safe property access (and conditional assignment) in ES6/2015
9 answers
I was looking through a code and I came across this:
{{abc?.xvy=== tyu?abc?.xz:abc?.xz}}
I am unable to understand meaning of this expression. I know that it is Null-safe property access but I am bit confused about the chaining.
Any help is much appreciated
This question already has an answer here:
Null-safe property access (and conditional assignment) in ES6/2015
9 answers
javascript ecmascript-5
javascript ecmascript-5
edited Feb 5 at 7:14
Apurva Pathak
asked Feb 5 at 6:26
Apurva PathakApurva Pathak
1369
1369
marked as duplicate by adiga, Samuel Liew♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 5 at 7:54
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by adiga, Samuel Liew♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 5 at 7:54
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I hope this code isn't used anywhere.
– Eric Duminil
Feb 5 at 7:39
unfortunately I saw it is used. Why is it not supposed to be used ?
– Apurva Pathak
Feb 5 at 8:56
It really isn't readable at all,abc
,xvy
tyu
are too generic to be useful and the ternay operator is useless because both outcomes are the same, as mentioned here
– Eric Duminil
Feb 5 at 8:57
add a comment |
I hope this code isn't used anywhere.
– Eric Duminil
Feb 5 at 7:39
unfortunately I saw it is used. Why is it not supposed to be used ?
– Apurva Pathak
Feb 5 at 8:56
It really isn't readable at all,abc
,xvy
tyu
are too generic to be useful and the ternay operator is useless because both outcomes are the same, as mentioned here
– Eric Duminil
Feb 5 at 8:57
I hope this code isn't used anywhere.
– Eric Duminil
Feb 5 at 7:39
I hope this code isn't used anywhere.
– Eric Duminil
Feb 5 at 7:39
unfortunately I saw it is used. Why is it not supposed to be used ?
– Apurva Pathak
Feb 5 at 8:56
unfortunately I saw it is used. Why is it not supposed to be used ?
– Apurva Pathak
Feb 5 at 8:56
It really isn't readable at all,
abc
, xvy
tyu
are too generic to be useful and the ternay operator is useless because both outcomes are the same, as mentioned here– Eric Duminil
Feb 5 at 8:57
It really isn't readable at all,
abc
, xvy
tyu
are too generic to be useful and the ternay operator is useless because both outcomes are the same, as mentioned here– Eric Duminil
Feb 5 at 8:57
add a comment |
3 Answers
3
active
oldest
votes
Its new ES proposal called "optionals" for safe check reading for object properties. Above expression is equivalent to:
(abc && abc.xvy) === (tyu) ? (abc && abc.xz) : (abc && abc.xz)
You can find more details here: https://github.com/davidyaha/ecmascript-optionals-proposal
2
OP's code doesn't have any standalone reference to anxz
variable, though.
– CertainPerformance
Feb 5 at 7:17
(tyu) ? (abc && abc.xz) : (abc && abc.xz)
is justabc && abc.xz
– Eric Duminil
Feb 5 at 12:34
add a comment |
This looks to be an example of the optional chaining proposal, which is still very much a work in progress (only at Stage 1). It's not actually implemented in vanilla JS environments yet. Using
obj?.prop
means: if obj
is undefined or null, the expression evaluates to undefined
. But otherwise, it will evaluate to the prop
property on the object. This is syntax sugar for
obj && obj.prop
(using just obj.prop
alone will throw if obj
is undefined or null)
So, your
abc?.xvy=== tyu?abc?.xz:abc?.xz
will evaluate to true
if the nested value abc?.xvy
is equal to the nested value abc?.xz
- or, it will evaluate to true
if at least one of the nested values doesn't exist, and the other is undefined
.
Spaced out for easier reading:
abc?.xvy === tyu
? abc?.xz
: abc?.xz
As you can see, both ?
and :
expressions are the same, making the conditional operator unnecessary here. An equivalent test (assuming that referencing tyu
doesn't throw) would be
abc?.xvy === abc?.xz
add a comment |
It's called Null Propagation Operator.
We can think of each ?. operator as a short circuit where "if the expression up until this point is null or undefined, then the whole expression evaluates to undefined".
We could also optionally call functions.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Its new ES proposal called "optionals" for safe check reading for object properties. Above expression is equivalent to:
(abc && abc.xvy) === (tyu) ? (abc && abc.xz) : (abc && abc.xz)
You can find more details here: https://github.com/davidyaha/ecmascript-optionals-proposal
2
OP's code doesn't have any standalone reference to anxz
variable, though.
– CertainPerformance
Feb 5 at 7:17
(tyu) ? (abc && abc.xz) : (abc && abc.xz)
is justabc && abc.xz
– Eric Duminil
Feb 5 at 12:34
add a comment |
Its new ES proposal called "optionals" for safe check reading for object properties. Above expression is equivalent to:
(abc && abc.xvy) === (tyu) ? (abc && abc.xz) : (abc && abc.xz)
You can find more details here: https://github.com/davidyaha/ecmascript-optionals-proposal
2
OP's code doesn't have any standalone reference to anxz
variable, though.
– CertainPerformance
Feb 5 at 7:17
(tyu) ? (abc && abc.xz) : (abc && abc.xz)
is justabc && abc.xz
– Eric Duminil
Feb 5 at 12:34
add a comment |
Its new ES proposal called "optionals" for safe check reading for object properties. Above expression is equivalent to:
(abc && abc.xvy) === (tyu) ? (abc && abc.xz) : (abc && abc.xz)
You can find more details here: https://github.com/davidyaha/ecmascript-optionals-proposal
Its new ES proposal called "optionals" for safe check reading for object properties. Above expression is equivalent to:
(abc && abc.xvy) === (tyu) ? (abc && abc.xz) : (abc && abc.xz)
You can find more details here: https://github.com/davidyaha/ecmascript-optionals-proposal
edited Feb 5 at 7:19
answered Feb 5 at 6:33
Vishal RajoleVishal Rajole
844816
844816
2
OP's code doesn't have any standalone reference to anxz
variable, though.
– CertainPerformance
Feb 5 at 7:17
(tyu) ? (abc && abc.xz) : (abc && abc.xz)
is justabc && abc.xz
– Eric Duminil
Feb 5 at 12:34
add a comment |
2
OP's code doesn't have any standalone reference to anxz
variable, though.
– CertainPerformance
Feb 5 at 7:17
(tyu) ? (abc && abc.xz) : (abc && abc.xz)
is justabc && abc.xz
– Eric Duminil
Feb 5 at 12:34
2
2
OP's code doesn't have any standalone reference to an
xz
variable, though.– CertainPerformance
Feb 5 at 7:17
OP's code doesn't have any standalone reference to an
xz
variable, though.– CertainPerformance
Feb 5 at 7:17
(tyu) ? (abc && abc.xz) : (abc && abc.xz)
is just abc && abc.xz
– Eric Duminil
Feb 5 at 12:34
(tyu) ? (abc && abc.xz) : (abc && abc.xz)
is just abc && abc.xz
– Eric Duminil
Feb 5 at 12:34
add a comment |
This looks to be an example of the optional chaining proposal, which is still very much a work in progress (only at Stage 1). It's not actually implemented in vanilla JS environments yet. Using
obj?.prop
means: if obj
is undefined or null, the expression evaluates to undefined
. But otherwise, it will evaluate to the prop
property on the object. This is syntax sugar for
obj && obj.prop
(using just obj.prop
alone will throw if obj
is undefined or null)
So, your
abc?.xvy=== tyu?abc?.xz:abc?.xz
will evaluate to true
if the nested value abc?.xvy
is equal to the nested value abc?.xz
- or, it will evaluate to true
if at least one of the nested values doesn't exist, and the other is undefined
.
Spaced out for easier reading:
abc?.xvy === tyu
? abc?.xz
: abc?.xz
As you can see, both ?
and :
expressions are the same, making the conditional operator unnecessary here. An equivalent test (assuming that referencing tyu
doesn't throw) would be
abc?.xvy === abc?.xz
add a comment |
This looks to be an example of the optional chaining proposal, which is still very much a work in progress (only at Stage 1). It's not actually implemented in vanilla JS environments yet. Using
obj?.prop
means: if obj
is undefined or null, the expression evaluates to undefined
. But otherwise, it will evaluate to the prop
property on the object. This is syntax sugar for
obj && obj.prop
(using just obj.prop
alone will throw if obj
is undefined or null)
So, your
abc?.xvy=== tyu?abc?.xz:abc?.xz
will evaluate to true
if the nested value abc?.xvy
is equal to the nested value abc?.xz
- or, it will evaluate to true
if at least one of the nested values doesn't exist, and the other is undefined
.
Spaced out for easier reading:
abc?.xvy === tyu
? abc?.xz
: abc?.xz
As you can see, both ?
and :
expressions are the same, making the conditional operator unnecessary here. An equivalent test (assuming that referencing tyu
doesn't throw) would be
abc?.xvy === abc?.xz
add a comment |
This looks to be an example of the optional chaining proposal, which is still very much a work in progress (only at Stage 1). It's not actually implemented in vanilla JS environments yet. Using
obj?.prop
means: if obj
is undefined or null, the expression evaluates to undefined
. But otherwise, it will evaluate to the prop
property on the object. This is syntax sugar for
obj && obj.prop
(using just obj.prop
alone will throw if obj
is undefined or null)
So, your
abc?.xvy=== tyu?abc?.xz:abc?.xz
will evaluate to true
if the nested value abc?.xvy
is equal to the nested value abc?.xz
- or, it will evaluate to true
if at least one of the nested values doesn't exist, and the other is undefined
.
Spaced out for easier reading:
abc?.xvy === tyu
? abc?.xz
: abc?.xz
As you can see, both ?
and :
expressions are the same, making the conditional operator unnecessary here. An equivalent test (assuming that referencing tyu
doesn't throw) would be
abc?.xvy === abc?.xz
This looks to be an example of the optional chaining proposal, which is still very much a work in progress (only at Stage 1). It's not actually implemented in vanilla JS environments yet. Using
obj?.prop
means: if obj
is undefined or null, the expression evaluates to undefined
. But otherwise, it will evaluate to the prop
property on the object. This is syntax sugar for
obj && obj.prop
(using just obj.prop
alone will throw if obj
is undefined or null)
So, your
abc?.xvy=== tyu?abc?.xz:abc?.xz
will evaluate to true
if the nested value abc?.xvy
is equal to the nested value abc?.xz
- or, it will evaluate to true
if at least one of the nested values doesn't exist, and the other is undefined
.
Spaced out for easier reading:
abc?.xvy === tyu
? abc?.xz
: abc?.xz
As you can see, both ?
and :
expressions are the same, making the conditional operator unnecessary here. An equivalent test (assuming that referencing tyu
doesn't throw) would be
abc?.xvy === abc?.xz
answered Feb 5 at 6:35
CertainPerformanceCertainPerformance
97.5k165887
97.5k165887
add a comment |
add a comment |
It's called Null Propagation Operator.
We can think of each ?. operator as a short circuit where "if the expression up until this point is null or undefined, then the whole expression evaluates to undefined".
We could also optionally call functions.
add a comment |
It's called Null Propagation Operator.
We can think of each ?. operator as a short circuit where "if the expression up until this point is null or undefined, then the whole expression evaluates to undefined".
We could also optionally call functions.
add a comment |
It's called Null Propagation Operator.
We can think of each ?. operator as a short circuit where "if the expression up until this point is null or undefined, then the whole expression evaluates to undefined".
We could also optionally call functions.
It's called Null Propagation Operator.
We can think of each ?. operator as a short circuit where "if the expression up until this point is null or undefined, then the whole expression evaluates to undefined".
We could also optionally call functions.
answered Feb 5 at 6:34
Alex ParkAlex Park
907
907
add a comment |
add a comment |
I hope this code isn't used anywhere.
– Eric Duminil
Feb 5 at 7:39
unfortunately I saw it is used. Why is it not supposed to be used ?
– Apurva Pathak
Feb 5 at 8:56
It really isn't readable at all,
abc
,xvy
tyu
are too generic to be useful and the ternay operator is useless because both outcomes are the same, as mentioned here– Eric Duminil
Feb 5 at 8:57