gpt4 book ai didi

JavaScript:当 `condition` 或 `ternary` 语句的 `if` 部分不包括 `===` 或 `>=`

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:42 24 4
gpt4 key购买 nike

在以下三元 语句的条件 部分,playlist.length 是否等于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");

同样,在下面的代码片段中,! playlist.length 等于 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.

最佳答案

0 在 JavaScript 中的 bool 比较中隐式转换为 false。所以当长度为0时,即为假。相反,在 JavaScript 的 bool 比较中,任何其他数字都会隐式转换为 true。因此,当长度不为 0 时为真。

一个简单的测试是使用 !! 来查看“truthy”值

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

关于JavaScript:当 `condition` 或 `ternary` 语句的 `if` 部分不包括 `===` 或 `>=`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com