if语句简写_缩写JavaScript If语句

if语句简写

The JavaScript if statement performs an action based on a condition, a common scenario in all programming languages.The if statement tests a bit of data against a condition, and then specifies some code to be executed if the condition is true, like so:

JavaScript的基础条件if语句执行一个动作,在所有的编程languages.The一个常见的场景if语句测试位数据的对抗状态,然后指定如果条件为真,像这样将要执行一些代码:


if condition {
execute

The if statement is almost always paired with the else statement because usually, you want to define an alternative bit of code to execute. Let's consider an example:

if语句几乎总是与else语句配对,因为通常情况下,您需要定义另一段要执行的代码。 让我们考虑一个例子:


if ('Stephen' === name) {
message = "Welcome back Stephen";
} else {
message = "Welcome " &

This code returns "Welcome back Stephen" if name is equal to Stephen; otherwise, it returns "Welcome" and then whatever value the variable name contains.

如果名称等于Stephen,则此代码返回“ Welcome back Stephen”; 否则,它将返回“ Welcome”,然后返回变量名称包含的任何值。

简短的IF语句 ( A Shorter IF Statement )

JavaScript provides us with an alternative way of writing an if statement when both the true and false conditions just assign different values to the same variable.

当真假条件都将不同的值分配给同一变量时,JavaScript为我们提供了一种编写if语句的替代方法。

This shorter way omits the keyword if as well as the braces around the blocks (which are optional for single statements). We also move the value that we are setting in both the true and false conditions to the front of our single statement and embed this new style of if statement into the statement itself. 

如果块以及括号周围(对于单个语句是可选的)以及括号,这种较短的方法将省略关键字。 我们还将在真假条件下设置的值移到单个语句的前面,并将这种新型的if语句嵌入到语句本身中。

Here's how this looks:

外观如下:


variable = (condition) ? true-valu

So our if statement from above could be written all in one line as:

因此,我们上面的if语句可以全部写成一行:


message = ('Stephen' === name) ? "Welcome back Stephen" : "Welcome &

As far as JavaScript is concerned, this one statement is identical to the longer code from above.

就JavaScript而言,这一条语句与上面的较长代码相同。

The only difference is that writing the statement this way actually provides JavaScript with more information about what the if statement is doing. The code can run more efficiently than if we wrote it the longer and more readable way. This is also called a ternary operator.

唯一的区别是,以这种方式编写语句实际上为JavaScript提供了有关if语句正在执行的操作的更多信息。 与我们以更长且更易读的方式编写代码相比,该代码可以更有效地运行。 这也称为三元运算符

将多个值分配给单个变量 ( Assigning Multiple Values to a Single Variable )

This way of coding an if statement can help avoid verbose code, particularly in nested if statements. For example, consider this set of nested if/else statements:

这种编码if语句的方法可以帮助避免冗长的代码,尤其是在嵌套的if语句中 。 例如,考虑以下嵌套的if / else语句集:


var answer;
if (a == b) {
if (a == c) {
answer = "all are equal";
} else {
answer = "a and b are equal";
}
} else {
if (a == c) {
answer = "a and c are equal";
} else {
if (b == c) {
answer = "b and c are equal";
} else {
answer = "all are different";

This code assigns one of five possible values to a single variable. Using this alternative notation, we can considerably shorten this into just one statement that incorporates all of the conditions:

Note that this notation can be used only when all the different conditions being tested are assigning different values to the same variable.


var answer;
if (a == b) {
if (a == c) {
answer = "all are equal";
} else {
answer = "a and b are equal";
}
} else {
if (a == c) {
answer = "a and c are equal";
} else {
if (b == c) {
answer = "b and c are equal";
} else {
answer = "all are different";

This code assigns one of five possible values to a single variable. Using this alternative notation, we can considerably shorten this into just one statement that incorporates all of the conditions:

Note that this notation can be used only when all the different conditions being tested are assigning different values to the same variable.

翻译自: https://www.thoughtco.com/create-a-shorter-if-statement-in-javascript-2037428

if语句简写

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值