JavaScript中的If和Else语句(香草)

Generally if statements have the following syntax:

通常, if语句具有以下语法:

    if(condition){
	    Our code;
    }

The condition, here can be anything from the mathematical expression, Boolean, relation operations etc.

条件,这里可以是数学表达式,布尔值,关系运算等中的任何一种。

In any case, the final value of any condition will either true or false. Once condition become true "Our code" will run that is in if block.

在任何情况下,任何条件的最终值都将为true或false。 条件变为真后,将运行if块中的“我们的代码”

Example 1:

范例1:

//condition: is 2 is less than 3? Yes so, condition = true
if(2 < 3) 
{
	console.log("I am in if");
}

Output

输出量

    I am in if

Example 2:

范例2:

//400 is greater than 20 thus, condition = true
if(400 > 20)  
{
	console.log("I am again in if block");
}

Output

输出量

    I am again in if block

Example 3:

范例3:

if(4-5+1 ==0) 
{
	console.log("Hey there I am back in if block");
}

Output

输出量

    Hey there I am back in if block

Note: We used == in condition instead of = because = is an assignment operator where as == is comparison operator is compares both operands and if they are equal then condition = true. Also != is a comparison operator which returns true if operands are not equal.

注意:我们在条件中使用==代替=,因为=是赋值运算符,其中==是比较运算符,它会比较两个操作数,如果它们相等,则condition = true 。 !=也是比较运算符,如果操作数不相等,则返回true。

But, what if condition will be false then if block will not execute the code within its blocks, so how can we deal with it? Then we can use else statement,

但是,如果条件为假,那么如果块将不执行其块内的代码,该怎么办,那么我们该如何处理呢? 然后我们可以使用else语句,

if(false)
{
	console.log("I am in if block");
} 
else
{
	console.log("I am  in ELSE block");
}

Output

输出量

    I am in ELSE block

Now, thing to keep in mind here is when if condition will be false then only else block will execute, and when if block will execute else block will not execute.

现在,要记住的是,如果条件为假,则只有else块会执行,而如果如果block会执行,则else块不会执行

When we need to check multiple conditions we can use multiple if statements,

当我们需要检查多个条件时,可以使用多个if语句

Example:

例:

let number = 12
if(number<20){
	console.log("condition 1 is true");
}
if(number>20){
	console.log("condition 2 is true");
}
if(number==12){
	console.log("condition 3 is true");
}

Output

输出量

    condition 1 is true
    condition 3 is true


Alternatively, above code can be written using logical operators,

另外,也可以使用逻辑运算符编写以上代码,

Logical operators:

逻辑运算符:

&& (AND) and || (OR) are two logical operators which allow us to use multiple conditions using single if statement.

&& (AND)和|| (OR)是两个逻辑运算符,可让我们使用单个if语句使用多个条件。

With && (AND) it will be true when all conditions will true while in case of || (OR) it will be true even when any condition will be true out of all conditions.

使用&& (AND),在所有条件都为真的情况下为true,而在||的情况下,则为true (OR)即使所有条件中的任何条件都成立,也将成立。

Example:

例:

let num= 5

if(num<6 && num>2){
	console.log("This will work");
}	
if(num<7 || num ==2){
	console.log("This will also work");
}

Here, both block will execute

在这里,两个块都将执行

嵌套if-else (Nested if-else)

Using nested if-else we can use check several cases, let us demonstrate nested if-else with following example:

使用嵌套的if-else我们可以使用check几种情况,让我们通过以下示例演示嵌套的if-else:

We generally use JS in Web application Development where we need to make decisions as per the situation. For example, let a user visits IncludeHelp website and that user want to comment on a discussion forum, but only registered user can comment on the forum, so How can we achieve this? Well, nested If and else can be use here.

我们通常在Web应用程序开发中使用JS,我们需要根据情况制定决策。 例如,让用户访问IncludeHelp网站,而该用户想在讨论论坛上发表评论,但只有注册用户可以在该论坛上发表评论,那么我们如何实现此目的? 好吧, 嵌套的If and else可以在这里使用。

Example:

例:

var aboutVisitor = 'abc';

if (aboutVisitor == 'user'){
	console.log("Comments Enable");
} else if (aboutVisitor == 'guest'){
	console.log("Comments Disable, view-only");
} else {
	console.log("Please Verify");
}


翻译自: https://www.includehelp.com/code-snippets/if-and-else-statement-in-javascript-vanilla.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值