If ... else command in JavaScript
While writing a program, there may be a situation when you need to follow one of a set of choices. In this case, you need to use conditional commands that allow your program to control the flow correctly and perform the right actions.
JavaScript supports conditional commands used to perform various actions on the basis of different conditions. Below we will explain the if . else command.
The diagram performs the if . else command
The diagram below shows the if . else command to work:
JavaScript supports the following if.else order forms:
- If statement
- If . else statement
- If . else if . command
If statement
The if statement is a basic control command that allows JavaScript to control the flow and execute commands according to conditions.
Syntax
The syntax for a basic if statement is:
if ( expression ){ Statement ( s ) to be executed if expression is true }
Here, a JavaScript expression is evaluated. If the result is true, the provided command is executed. If the expression is false, no command will be executed. Most of the time, you will use comparison operators while controlling flow.
For example
Try the following example to understand how to work if :
Set the variable to different value and then try.
Result:
Qualifies for driving
Đặt biến vào giá trị khác nhau và thử thử .
If . else statement
The 'if . else ' command is another control command pattern that allows JavaScript to execute commands in a more controlled manner.
Syntax
if ( expression ){ Statement ( s ) to be executed if expression is true } else { Statement ( s ) to be executed if expression is false }
Here, JavaScript expressions are evaluated. If the result value is true, the command provided in the if block is executed. If the expression is false, then the given command in the else block is executed.
For example
Try the following example to understand how if . else commands work in JavaScript.
Set the variable to different value and then try.
Result:
Does not qualify for driving
Đặt biến vào giá trị khác nhau và Then try . If . else if .
If . else if . command is an advanced if . else control command that allows JavaScript to make an accurate decision in some conditions.
Syntax
Syntax of if-else-if statement is as follows:
if ( expression 1 ){ Statement ( s ) to be executed if expression 1 is true } else if ( expression 2 ){ Statement ( s ) to be executed if expression 2 is true } else if ( expression 3 ){ Statement ( s ) to be executed if expression 3 is true } else { Statement ( s ) to be executed if no expression is true }
There is nothing special about this code. It is a series of if statements, each if is part of the else clause of the previous command. The commands are executed on a true condition, if there is no true condition, then the else block is executed.
For example
Try the following example to learn how to execute an if-else-if statement in JavaScript.
Set the variable to different value and then try.
Result:
Maths Book Set the variable to different value and then try.
Follow tutorialspoint
Previous article: Operator in JavaScript
Next lesson: Switch Case command in JavaScript
You should read it
May be interested
- Learn about ES6 in Javascriptes6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
- Summary of JavaScript exercises with sample codein order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.
- JavaScript location in HTML Filethere is flexibility in providing javascript code anywhere in an html document. however, the most preferred ways to include javascript in an html file.
- How to Turn on JavaScriptjavascript is a language used in many websites to provide additional functionality and access to the user. developers use javascript to deliver a large part of their site's functionality to you, the user. your browser must have javascript...
- What is the difference between Java and JavaScript?although the names of java and javascript seem to be related (javascript seems to be a script in java?), but that's the only similarity. these two languages are not technically related to each other.
- Void keywords in JavaScriptvoid is an important keyword in javascript that can be used as a unary operator before its single operand, which can be in any type. this operator defines an expression to be evaluated without returning a value.
- JavaScript code to generate error charts & graphsthe example below illustrates a sample variance chart created with javascript that incorporates a column chart. you will also get the source code for reference and editing as you wish.
- Introduction to 2D Array - 2-dimensional array in JavaScriptin the following article, we will introduce and show you some basic operations to create and access 2-dimensional arrays - 2d array in javascript. essentially, a 2-dimensional array is a concept of a matrix of matrices - a matrix, used to store information. each 1 element contains 2 separate indexes: row (y) - column and column (x) - column.
- Operator in JavaScriptwe see the following simple expression: 4 + 5 is equal to 9. here 4 and 5 are operands and '+' called operator - operator.
- How to Disable JavaScriptthis wikihow teaches you how to turn off your web browser's javascript support. javascript is responsible for loading dynamic content in webpages, so turning it off can help websites load faster than usual. most web browsers and their...