Today I have learned condition by using if condition in javascript
Condition by using if condition in javascript:
Conditional statements in JavaScript allow you to execute specific blocks of code based on conditions. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition.
There are several methods that can be used to perform Conditional Statements in JavaScript.
- if Statement
- if-else Statement
- else if Statement
- switch Statement
- Ternary Operato
- Syntax:
if ( condition ) {
// If the condition is met,
//code will get executed.
}JavaScript if-else Statement
The if-else statement will perform some action for a specific condition. Here we are using the else statement in which the else statement is written after the if statement and it has no condition in their code block.
Syntax:
if (condition1) {
// Executes when condition1 is true
if (condition2) {
// Executes when condition2 is true
}
}- if (this.state.isLoggedIn) {// Render content for logged-in usersreturn (<div><h1>Welcome, User!</h1><button class="btn btn-danger" onClick={this.handleLogout}>Logout</button></div>);} else {// Render content for users who are not logged inreturn (<div><h1>Please log in</h1><form><input class="form-control me-2" type="text" placeholder="email"/><input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pswd"/><input class="form-control me-2" type="number" placeholder="phone number"/><h5>Gender</h5><input type="radio" id="male"></input><lable for="male">Male</lable><br></br><input type="radio" id="female"></input><lable for="female">Female</lable><br></br><br></br><button class="btn btn-primary" onClick={this.handleLogin}>Login</button></form></div>);}}handleLogin = () => {// Logic to handle loginthis.setState({ isLoggedIn: true });};handleLogout = () => {// Logic to handle logoutthis.setState({ isLoggedIn: false });};}
.png)
.png)
Comments
Post a Comment