JavaScript中的if语句是什么?
if语句是基本的控制语句,它使JavaScript可以有条件地进行决策和执行语句。
语法
基本if语句的语法如下-
if(expression){
Statement(s)to be executed if expression is true
}在此评估JavaScript表达式。如果结果值为true,则执行给定的语句。如果表达式为假,则不会执行任何语句。大多数时候,您将在进行决策时使用比较运算符。
示例
您可以尝试运行以下命令以了解如何在JavaScript中使用if语句-
<html>
<body>
<script>
var age = 25;
if( age > 18 ){
document.write("有资格投票!");
}
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>