javascript
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 ifExample 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 blockExample 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 blockNote: 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 blockNow, 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 truecondition 3 is true .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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= 5if(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
總結
以上是生活随笔為你收集整理的JavaScript中的If和Else语句(香草)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java SecurityManager
- 下一篇: java 方法 示例_Java集合的la