admin管理员组

文章数量:1429514

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.

本文标签: JavaScript When the condition portion of a ternary or if statement does not includeor gtStack Overflow