If else statement in javascript
Conditional statements are used to perform different actions based on different conditions. Show Conditional StatementsVery often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In JavaScript we have the following conditional statements:
The The if StatementUse the Syntax if
(condition) { Note that ExampleMake a "Good day" greeting if the hour is less than 18:00: if (hour < 18) { The result of greeting will be: Try it Yourself » The else StatementUse the if (condition) { ExampleIf the hour is less than 18, create a "Good day" greeting, otherwise "Good evening": if (hour < 18) { The result of greeting will be: Try it Yourself » The else if StatementUse the Syntax if (condition1) { ExampleIf time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00, create a "Good day" greeting, otherwise a "Good evening": if (time < 10) { The result of greeting will be: Try it Yourself » More ExamplesRandom link ExampleIf the hour is less than 20, output "Good day": let hour = new Date().getHours(); Try it Yourself » Output "Good day" or "Good evening": let hour = new Date().getHours(); Try it Yourself » More examples below. Definition and UsageThe if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. The if/else statement is a part of JavaScript's "Conditional" Statements, which are used to perform different actions based on different conditions. In JavaScript we have the following conditional statements:
SyntaxThe if statement specifies a block of code to be executed if a condition is true: if (condition) { The else statement specifies a block of code to be executed if the condition is false: if (condition) { The else if statement specifies a new condition if the first condition is false: if (condition1) { Parameter Values
More ExamplesIf time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00, create a "Good day" greeting, otherwise a "Good evening": var time = new Date().getHours(); Try it Yourself » If the first element in the document has an id of "myDIV", change its font-size:
var x = document.getElementsByTagName("DIV")[0]; if (x.id === "myDIV") { Try it Yourself » Change the value of the source attribute (src) of an Try it Yourself » Display a message based on user input: var letter = document.getElementById("myInput").value; // If the letter is "c" // If the letter is "b" or "d" // If the letter is anything else Try it Yourself » Validate input data: var x, text; // Get the value of the input field with id="numb" // If x is Not a Number
or less than 1 or greater than 10, output "input is not valid" if (isNaN(x) || x < 1 || x > 10) { Try it Yourself » Related PagesJavaScript Tutorial: JavaScript If...Else Statements JavaScript Tutorial: JavaScript Switch Statement Browser Support
ES1 (JavaScript 1997) is fully supported in all browsers:
What is if else statement in JavaScript?The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. The if/else statement is a part of JavaScript's "Conditional" Statements, which are used to perform different actions based on different conditions.
What is the syntax of if else?Syntax. If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else block will be executed. C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.
What is if else if used for?The if/else if statement allows you to create a chain of if statements. The if statements are evaluated in order until one of the if expressions is true or the end of the if/else if chain is reached. If the end of the if/else if chain is reached without a true expression, no code blocks are executed.
Does Elif exist in JavaScript?In JavaScript's if-then-else there is technically no elseif branch.
|