یک سوال درباره "if"
اهمیت: 5
کدام یک از alert
ها اجرا خواهد شد؟
نتیجه عبارت های داخل (...)if
چه خواهد بود؟
if
(
-
1
||
0
)
alert
(
'first'
)
;
if
(
-
1
&&
0
)
alert
(
'second'
)
;
if
(
null
||
-
1
&&
1
)
alert
(
'third'
)
;
جواب: اولی و سومی اجرا خواهند شد.
جزییات:
// Runs.
// The result of -1 || 0 = -1, truthy
if
(
-
1
||
0
)
alert
(
'first'
)
;
// Doesn't run
// -1 && 0 = 0, falsy
if
(
-
1
&&
0
)
alert
(
'second'
)
;
// Executes
// Operator && has a higher precedence than ||
// so -1 && 1 executes first, giving us the chain:
// null || -1 && 1 -> null || 1 -> 1
if
(
null
||
-
1
&&
1
)
alert
(
'third'
)
;