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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545429.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

不创建 sequence 自增字段

drop table t_log;create table t_log (log_id number primary key,user_name varchar2(100),log_date date);--新建一个表&#xff0c;有id 用户名 和 日期字段create or replace trigger tri_logonafter logon on databasebegininsert into t_log values(nvl((select max(…

完整案例:实现延迟队列的两种方法

延迟队列是指把当前要做的事情,往后推迟一段时间再做。 延迟队列在实际工作中和面试中都比较常见,它的实现方式有很多种,然而每种实现方式也都有它的优缺点,接下来我们来看。 延迟队列的使用场景 延迟队列的常见使用场景有以下几种: 超过 30 分钟未支付的订单,将会被取…

Java API概述及应用

Java API概述及应用5.1_Scanner和Random的使用&#xff08;1&#xff09;Scanner&#xff08;2&#xff09;Random生成随机数5.2_ArrayList集合的使用&#xff08;1&#xff09;ArrayList的定义及限制&#xff08;2&#xff09;函数调用&#xff08;3&#xff09;字符串字符串加…

定位position详解:relative与absolute

定位标签&#xff1a;position 包含属性&#xff1a;relative&#xff08;相对&#xff09; absolute&#xff08;绝对&#xff09; 1.position:relative; 如果对一个元素进行相对定位&#xff0c;首先它将出现在它所在的位置上。然后通过设置垂直或水平位置&#xff0c;让这个…

实战:布隆过滤器安装与使用及原理分析

我们前面有讲到过 HyperLogLog 可以用来做基数统计,但它没提供判断一个值是否存在的查询方法,那我们如何才能查询一个值是否存在于海量数据之中呢? 如果使用传统的方式,例如 SQL 中的传统查询,因为数据量太多,查询效率又低有占用系统的资源,因此我们需要一个优秀的算法…

转:RMAN 备份与恢复 实例

转载自&#xff1a;http://blog.csdn.net/tianlesoftware/article/details/4699320 1. 检查数据库模式&#xff1a; sqlplus /nolog conn /as sysdba archive log list (查看数据库是否处于归档模式中) 若为非归档,则修改数据库归档模式。 startup mount alter…

有重复数字的组合问题_带数字重复的组合和问题

有重复数字的组合问题Description: 描述&#xff1a; This is a standard interview problem to make some combination of the numbers whose sum equals to a given number using backtracking. 这是一个标准的面试问题&#xff0c;它使用回溯功能将总和等于给定数字的数字进…

第四章语法分析和语法分析程序

第四章语法分析和语法分析程序4.1_自顶向下的语法分析4.1.1_自顶向下分析过程的基本特点①消除文法直接左递归②回溯的消除及LL(1)文法4.1.2_递归下降法4.1.3_预测分析法&#xff08;也叫LL1法&#xff0c;注意分析过程中非终结符号逆序入栈&#xff09;4.2_自底向上的语法分析…

实战:RediSearch 高性能的全文搜索引擎

RediSearch 是一个高性能的全文搜索引擎,它可以作为一个 Redis Module(扩展模块)运行在 Redis 服务器上。 RediSearch 主要特性如下: 基于文档的多个字段全文索引高性能增量索引文档排序(由用户在索引时手动提供)在子查询之间使用 AND 或 NOT 操作符的复杂布尔查询可选的…

智能优化算法应用:基于法医调查算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于法医调查算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于法医调查算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.法医调查算法4.实验参数设定5.算法结果6.…

转:Oracle物理文件

转载&#xff1a;http://www.worlduc.com/blog2012.aspx?bid19969111 -------------------初始化参数文件--------------------------------------------- 在9i之前&#xff0c;参数文件只有一种&#xff0c;它是文本格式的&#xff0c;称为pfile&#xff0c;在9i及以后的版本…

c语言两个浮点数相加_C语言中两个浮点数或双精度数的模数

c语言两个浮点数相加As we know that modules also known as the remainder of the two numbers can be found using the modulus (%) operator which is an arithmetic operator in C/C. The modules operator works with integer values i.e. modulus operator is used to fi…

java线程的5个使用技巧

Java线程的5个使用技巧 Published: 21 Jan 2015 Category: java Java线程有哪些不太为人所知的技巧与用法&#xff1f; 萝卜白菜各有所爱。像我就喜欢Java。学无止境&#xff0c;这也是我喜欢它的一个原因。日常工作中你所用到的工具&#xff0c;通常都有些你从来没有了解过的东…

算法复习第五章贪心法

算法复习第五章贪心法概述TSP最近邻点策略最短连接策略图着色问题最小生成树&#xff08;Prim算法、Kruskal&#xff09;0-1bag问题活动安排问题多机调度概述 TSP 最近邻点策略 最短连接策略 图着色问题 最小生成树&#xff08;Prim算法、Kruskal&#xff09; 0-1bag问题 活动…

实战:定时任务案例

我在开发的时候曾经遇到了这样一个问题,产品要求给每个在线预约看病的患者,距离预约时间的前一天发送一条提醒推送,以防止患者错过看病的时间。这个时候就要求我们给每个人设置一个定时任务,用前面文章说的延迟队列也可以实现,但延迟队列的实现方式需要开启一个无限循环任…

c语言putchar函数_C语言中的putchar()函数与示例

c语言putchar函数C语言中的putchar()函数 (putchar() function in C) The putchar() function is defined in the <stdio.h> header file. putchar()函数在<stdio.h>头文件中定义。 Prototype: 原型&#xff1a; int putchar(const char *string);Parameters: co…

算法复习第六章第七章

算法复习第六章第七章第六章回溯法TSP问题0-1bag问题图着色问题八皇后问题第七章分支限界法0-1bag问题TSP问题第六章回溯法 TSP问题 0-1bag问题 图着色问题 八皇后问题 第七章分支限界法 0-1bag问题 TSP问题

扫描识别系统

扫描识别系统&#xff0c;是指能够利用扫描仪进行扫描的相关文件&#xff0c;比方普通文档&#xff0c;政府公文&#xff0c;二代身份证&#xff0c;条码……等等。通过扫描仪扫描后不单单生成常见的JPG&#xff0c;PDF等格式的图像。而是利用先进的OCR技术&#xff0c;进行相关…

转:ORA-01126: 数据库必须已装载到此实例并且不在任何实例中打开

转载&#xff1a;http://www.worlduc.com/blog2012.aspx?bid19973952 alter database archivelog 2 ; alter database archivelog * 第 1 行出现错误: ORA-01126: 数据库必须已装载到此实例并且不在任何实例中打开 ------最佳解决方案-------------------- 修改归档模式的…

Java SimpleTimeZone setStartRule()方法与示例

SimpleTimeZone类setStartRule()方法 (SimpleTimeZone Class setStartRule() method) Syntax: 句法&#xff1a; public void setStartRule(int st_mm, int st_dd, int st_time);public void setStartRule(int st_mm, int st_dd, int st_dow, int st_time);public void setSta…