Fixed code formatting (#23068)
Added curly brackets where they had been left out of the code snippets. Also fixed two grammatical errors on line 52
This commit is contained in:
@ -8,24 +8,26 @@ The `if` statement executes a statement if a specified condition is `true`. If t
|
|||||||
**Note:** The `else` statement is optional.
|
**Note:** The `else` statement is optional.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
if (condition)
|
if (condition) {
|
||||||
/* do something */
|
/* do something */
|
||||||
else
|
} else {
|
||||||
/* do something else */
|
/* do something else */
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Multiple `if...else` statements can be chained to create an `else if` clause. This specifies a new condition to test and can be repeated to test multiple conditions, checking until a true statement is presented to execute.
|
Multiple `if...else` statements can be chained to create an `else if` clause. This specifies a new condition to test and can be repeated to test multiple conditions, checking until a true statement is presented to execute.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
if (condition1)
|
if (condition1) {
|
||||||
/* do something */
|
/* do something */
|
||||||
else if (condition2)
|
} else if (condition2) {
|
||||||
/* do something else */
|
/* do something else */
|
||||||
else if (condition3)
|
} else if (condition3) {
|
||||||
/* do something else */
|
/* do something else */
|
||||||
else
|
} else {
|
||||||
/* final statement */
|
/* final statement */
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Note:** If you want to execute more than one statement in the `if`, `else` or `else if` part, curly braces are required around the statements:
|
**Note:** If you want to execute more than one statement in the `if`, `else` or `else if` part, curly braces are required around the statements:
|
||||||
@ -63,28 +65,28 @@ const num = someCondition ? 1 : 2;
|
|||||||
|
|
||||||
**Using** `if...else`:
|
**Using** `if...else`:
|
||||||
```javascript
|
```javascript
|
||||||
// If x=5 then z=7 and q=42 else z=19.
|
// If x = 5, then z = 7 and q = 42.
|
||||||
if (x === 5) {
|
// If x is not 5, then z = 19.
|
||||||
|
if (x == 5) {
|
||||||
z = 7;
|
z = 7;
|
||||||
q = 42;
|
q = 42
|
||||||
}else{
|
} else {
|
||||||
z = 19;
|
z = 19;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Using** `else if`:
|
**Using** `else if`:
|
||||||
```javascript
|
```javascript
|
||||||
// Categorising any number x as a small, medium, or large number
|
if (x < 10) {
|
||||||
if (x < 10)
|
|
||||||
return "Small number";
|
return "Small number";
|
||||||
else if (x < 50)
|
} else if (x < 50) {
|
||||||
return "Medium number";
|
return "Medium number";
|
||||||
else if (x < 100)
|
} else if (x < 100) {
|
||||||
return "Large number";
|
return "Large number";
|
||||||
else {
|
} else {
|
||||||
flag = 1;
|
flag = 1;
|
||||||
return "Invalid number";
|
return "Invalid number";
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Using** `if` **alone**:
|
**Using** `if` **alone**:
|
||||||
|
Reference in New Issue
Block a user