JavaScript If...Else statement
JavaScript If...Else Statement
Conditional statements are used to perform different actions based on different conditions.
Conditional Statements
Usually when writing code, you always need to perform different actions for different decisions. You can use conditional statements in your code to accomplish this task.
In JavaScript, we can use the following conditional statements:
if statement - Use this statement to execute code only when the specified condition is true
if... else statement - execute code when the condition is true and other code when the condition is false
if...else if....else statement - use this statement to select one of multiple blocks of code to Execute
switch statement - Use this statement to select one of multiple blocks of code to execute
If statement
The statement will only be executed if the specified condition is true code.
Syntax
if (condition)
{
Code executed when the condition is true
}
Please use lowercase if. Using uppercase letters (IF) will generate a JavaScript error!
If...else statement
Please use the if...else statement to execute the code when the condition is true, and when the condition is Execute other code when false.
Grammar
if (condition) { 當(dāng)條件為 true 時執(zhí)行的代碼 } else { 當(dāng)條件不為 true 時執(zhí)行的代碼 }
點(diǎn)擊這個按鈕,獲得基于時間的問候。點(diǎn)擊這里function myFunction(){ var x=""; var time=new Date().getHours(); if (time