JavaScript: When the `condition` portion of a `ternary` or `if` statement does not include `===` or `>=` - Stack Overflow

In the condition portion of the following ternary statement, does playlist.length equal playlist.length

In the condition portion of the following ternary statement, does playlist.length equal playlist.length >= 1 ?

var playlist = ["video1", "video2", "video3", "video4", "video5"];

// some code here

alert ((playlist.length) ?
     playlist.length + "video(s) remain in the playlist: " + playlist.join (", ") + "." 
    : "No videos remain in the playlist");

Likewise, in the following snippet of code, does ! playlist.length equal playlist.length === 0 ?

alert ((! playlist.length) ?
     "No videos in the playlist."
    : playlist.length + " video(s) remain in the playlist: " + playlist.join(", ") + ".");

This is the first time I've seen an example where the condition portion of a ternary or if statement does not include such a notation as === or >=. So I'm not sure how to interpret this.

In the condition portion of the following ternary statement, does playlist.length equal playlist.length >= 1 ?

var playlist = ["video1", "video2", "video3", "video4", "video5"];

// some code here

alert ((playlist.length) ?
     playlist.length + "video(s) remain in the playlist: " + playlist.join (", ") + "." 
    : "No videos remain in the playlist");

Likewise, in the following snippet of code, does ! playlist.length equal playlist.length === 0 ?

alert ((! playlist.length) ?
     "No videos in the playlist."
    : playlist.length + " video(s) remain in the playlist: " + playlist.join(", ") + ".");

This is the first time I've seen an example where the condition portion of a ternary or if statement does not include such a notation as === or >=. So I'm not sure how to interpret this.

Share Improve this question edited Dec 14, 2015 at 22:00 pourrait Peut-être asked Dec 14, 2015 at 21:12 pourrait Peut-êtrepourrait Peut-être 2911 gold badge3 silver badges9 bronze badges 5
  • playlist.length as a condition will check if that property is undefined or not – Lucas Rodriguez Commented Dec 14, 2015 at 21:14
  • This is very idiomatic. Any boolean value can be used in a ternary/conditional. Since 0 is falsy, array.length is false when used in such a context – Brennan Commented Dec 14, 2015 at 21:14
  • in your examples it will check for playlist.length being zero or not. in general it look for a truthy statement. – Sirko Commented Dec 14, 2015 at 21:14
  • It tests if the object it self has a length, i.e data inside of it. – gh9 Commented Dec 14, 2015 at 21:15
  • A zero value will make the condition false, but it reduces readability and consistency of the code. I prefer to use conditions more explicitly. – Reyraa Commented Dec 14, 2015 at 22:43
Add a ment  | 

4 Answers 4

Reset to default 4

0 is implicitly converted to false in boolean parisons in JavaScript. So when the length is 0, that is false. Conversely, any other number is implicitly converted to true in boolean parisons in JavaScript. So when the length is anything but 0 it is true.

An easy test is to use !! to see the "truthy" value

!!1 === true
!!0 === false
!!6 === true

The part to the left of the ? is simply evaluated for "truthiness". The value 0 is "falsy" so evaluates as false for the purposes of the test. Numeric values other than 0 are "truish" and therefore for this purpose evaluate to true.

All other behaviors of the ternary are the same.

The === and !== simply add the additional constraint that the L-value and R-value must also be the same type.

The two are very similar: !playlist.length and playlist.length === 0.

However, they are not exacty the same. In fact, here:

var playlist1 = [];
var playlist2 = {};
!playlist1.length // true
!playlist2.length // true
playlist1.length === 0 // true
playlist1.length === 0 // false

In that sense !playlist.length also can be used on all kinds of objects, not just arrays.

In any case, when using this on an array, it is a way to check if the array is empty, and works as you have suggested, the same as playlist.length === 0.

In javascript 0 equals false, and any other number value equals true, but if you use === it pare value types too.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745527525a4631539.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信