【发布时间】:2012-01-09 19:40:46
【问题描述】:
如何在 JavaScript 的 IF 语句中使用具有布尔值的变量作为条件?
patt1 = new RegExp ("time");
var searchResult = (patt1.test("what time is it" )); // search for the word time in the string
// and return true or false
If (searchResult = true) // what is the right syntax for the condition?
{
document.write("Word is in the statement");
document.write("<br />");
}
【问题讨论】:
-
您只能使用变量作为“条件”:
if (searchResult),如果您想使用该语法,使用一个 = 将不起作用,因为 = 运算符用于 java 和 jscript 中的赋值你应该使用if (searchResult == true)
标签: javascript if-statement boolean