下面给出的这段代码应该评估一个值是否强制为真或假。这是如何运作的?例如,如果我想知道一个字符串“我的字符串”是否会被强制为真或假,我该如何使用来确定呢?
我试图用“我的字符串”替换下面代码中的 val 出现 val
function logTruthiness (val) {
if (val) {
console.log("Truthy!");
} else {
console.log("Falsy.");
}
}
function logTruthiness ("my string") {
if ("my string") {
console.log("Truthy!");
} else {
console.log("Falsy.");
}
}
错误函数 logTruthiness ("my string") { SyntaxError: Unexpected string