You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Learn/3. Control Structures (condition, loop and etc.).md
+38-38Lines changed: 38 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,16 +11,16 @@ In this course, you will learn about:
11
11
12
12
|Operator|Description|Example|
13
13
|:-:|:-:|:-:|
14
-
|`==`|Check for value equality|`a == b`|
15
-
|`!=`|Check for value inequality|`a != b`|
16
-
|`===` and `!==`|Check for value **and** type (in)equality|`a === b` / `a !== b`|
17
-
|`>` and `<`|Check for value being greater / smaller|`a > b` / `a < b`|
18
-
|`>=` and `<=`|Check for value being greater or equal / smaller or equal|`a >= b` / `a <= b`|
19
-
|`!`|Check if **note** true|`!a`|
14
+
|`==`|Checks value equality|`a == b`|
15
+
|`!=`|Checks value inequality|`a != b`|
16
+
|`===` and `!==`|Checks value **and** type (in)equality|`a === b` / `a !== b`|
17
+
|`>` and `<`|Checks value being greater / smaller|`a > b` / `a < b`|
18
+
|`>=` and `<=`|Checks value being greater or equal / smaller or equal|`a >= b` / `a <= b`|
19
+
|`!`|Checks if value is **not** true|`!a`|
20
20
21
21
One of most using statement for conditions, is `if` statement. Always keep in mind that condition in `if (condition) { .. }` simply has to be a **boolean value**.
22
22
23
-
Often, you'll **generate** such a boolean value with the help of `===,``>`, `<` etc. **All these operators yield boolean values** (without changing the variables / values you're using them on).
23
+
Often, you'll **generate** such a boolean value with the help of `===`,`>`, `<` and etc. **All these operators yield boolean values** (without changing the variables / values you're using them on).
24
24
25
25
Since `if` only wants a boolean, you of course don't have to use such an operator. If you already got a variable that holds a boolean, you can use it without any extra operator. For example:
26
26
@@ -47,9 +47,9 @@ You can use the `!` operator to negate (**invert**) the value:
47
47
```js
48
48
constisTrue=true;
49
49
if (!isTrue) {
50
-
// This code will NOT execute because isTrue is true but ! inverts it (in this check)
50
+
// This code will NOT execute because `isTrue` is true but `!` inverts it (in this check)
51
51
} else {
52
-
// This would execute because !isTrue yields false => else block executes
52
+
// This would execute because `!isTrue` yields false => else block executes
For more information of operator precedence, see this [link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence).
81
+
For more information of operator precedence, see [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence) link.
What is `if`? `if` is a statement. We can use `if` for create some conditions for our code. For example, we want tell `"Stop!"` when number reach`10`:
139
+
What is `if`? `if` is a statement. We can use `if` for create some conditions for our code. For example, we want tell `"Stop!"` when number reaches`10`:
140
140
141
141
```js
142
142
let number =0;
@@ -150,7 +150,7 @@ while(true){
150
150
}
151
151
```
152
152
153
-
In above snippet, we wanted if `number` more than or equal to `10`, execution will stop. `while` is a **loop** and cover it in later... while loop is `true` (iterate for ever), number will increase by `1`. After number reach`10`, execution will stop by `if` condition.
153
+
In snippet above, we wanted if `number` more than or equal to `10`, execution then stop. `while` is a **loop** and we cover it in later. while loop is `true` (iterate for ever), number then increases by `1`. After number reaches`10`, execution will stop by `if` condition.
154
154
155
155
We also have optional statement called `else`. If `if` condition not reach, `else` block will execute. For example:
156
156
@@ -164,15 +164,15 @@ if (name === "Hamid") {
164
164
}
165
165
```
166
166
167
-
In above snippet, if `name` equal to `"Hamid"`, output will `"True!"`, else if `name` not equal to `"Hamid"`, output will `"False"`. We called this `if..else` condition (statement).
167
+
In snippet above, if `name` equal to `"Hamid"`, output will be `"True!"`, else if `name` not equal to `"Hamid"`, output will be`"False"`. We called this `if..else` condition (statement).
168
168
169
169
## Truethy and Falsy
170
170
171
171
Some values are called **falsy**, like `0`, `0n`, `-0`, `null`, `undefined`, `false`, `NaN` and empty string `""`.
172
172
173
173
Some values are called **truthy**, like `23`, `"hi"`, `true`, `[]`, `{}`, `object = {a:1}`, `function hamid() {..}` and `[23, 24]`.
174
174
175
-
See below snippets:
175
+
See these following snippets:
176
176
177
177
```js
178
178
let name ="Hamid";
@@ -199,12 +199,12 @@ if (isTrue) {
199
199
200
200
Since if just wants a condition that returns `true` or `false`, it makes sense that you can just provide a **boolean** variable or value and it works - without the extra comparison (`if (isTrue === true`) - that would also work but is redundant).
201
201
202
-
Whilst the above example makes sense, it can be confusing when you encounter code like this for the first time:
202
+
Whilst the example above makes sense, it can be confusing when you encounter code like this for the first time:
203
203
204
204
```js
205
205
let userInput ='Hamid';
206
206
if (userInput) {
207
-
// this code here will execute because 'Hamid' is "truthy" (all strings but empty strings are)
207
+
// this code here will execute because 'Hamid' is "truthy" (all strings but empty strings are falsy)
208
208
}
209
209
```
210
210
@@ -220,7 +220,7 @@ if (userInput) { ... }
220
220
221
221
## Ternary Operator
222
222
223
-
We can use shorthand `? :` operator for `if` statements. For example, we can write code like below:
223
+
We can use shorthand `? :` operator for `if` statements. For example, we can write code like:
224
224
225
225
```js
226
226
constname=if (isTrue) {
@@ -236,7 +236,7 @@ But with ternary operator, we can do this:
236
236
constname= isTrue ?"Hamid":null;
237
237
```
238
238
239
-
How above snippet work? It's very simple. First, we declare a constant that called `name`. Then, we check if `isTrue` is `true`, `"Hamid"` will execute, else the condition returns `null`.
239
+
How this snippet work? It's very simple. First, we declared a constant that called `name`. Then, we checked if `isTrue` is `true`, `"Hamid"` will be executed, else the condition returns `null`.
240
240
241
241
**Note**: `if` is statement, but **not expression**. Because it **can't** be after right-hand-side of a variable or a constant (after `=` assignment). But we can use **Ternary Operator** for this purpose. For example:
242
242
@@ -270,7 +270,7 @@ We can use `||` (or) operator for variable's value. For example:
270
270
let firstName =""||"Hamid";
271
271
console.log(firstName); // "Hamid"
272
272
273
-
let lastName ="Alavi"||"Willson"
273
+
let lastName ="Alavi"||"Goertzel"
274
274
console.log(lastName); // "Alavi"
275
275
```
276
276
@@ -292,7 +292,7 @@ And for better understanding with example, consider this table:
292
292
|Value 1|Value 2|Value 3|Output|
293
293
|:-:|:-:|:-:|:-:|
294
294
|`false`|`false`|`true`|`true`|
295
-
|`""`|`"Hamed"`|`"Ali"`|`"Ali"`|
295
+
|`""`|`"Hamed"`|`"Ali"`|`"Hamed"`|
296
296
|`false`|`NaN`|`"Hamid"`|`"Hamid"`|
297
297
|`true`|`"Reza"`|`"Mehrdad"`|`true`|
298
298
@@ -304,8 +304,8 @@ We can use `&&` (and) operator for variable's value. For example:
304
304
let firstName =""&&"Hamid";
305
305
console.log(firstName); // ""
306
306
307
-
let lastName ="Alavi"&&"Willson"
308
-
console.log(lastName); // "Willson"
307
+
let lastName ="Alavi"&&"Goertzel";
308
+
console.log(lastName); // "Goertzel"
309
309
```
310
310
311
311
In `firstName`, output is `""`, because first value is **falsy**.
@@ -338,7 +338,7 @@ And for better understanding with example, consider this table:
338
338
let day =4;
339
339
340
340
if (day ===1) {
341
-
console.log("Monday");
341
+
console.log("Monday");// this line executed
342
342
} elseif (day ===2) {
343
343
console.log("Tuesday");
344
344
} elseif (day ===3) {
@@ -356,7 +356,7 @@ if (day === 1) {
356
356
}
357
357
```
358
358
359
-
See? If we want use complex conditiion, multiple combined conditions or greater than checks and so on. We definitely want to use `if/else` blocks and statements instead of the `switch-case` statement; because that really only shines for **strict equality** checks.
359
+
See? If we want touse complex condition, multiple combined conditions or greater than checks and so on, we definitely want to use `if/else` blocks and statements instead of the `switch-case` statement; because that really only shines for **strict equality** checks.
360
360
361
361
Let's look `switch-case` example:
362
362
@@ -374,7 +374,7 @@ switch (day) {
374
374
console.log("Wednesday");
375
375
break;
376
376
case4:
377
-
console.log("Thursday");
377
+
console.log("Thursday");// this line executed
378
378
break;
379
379
case5:
380
380
console.log("Friday");
@@ -392,7 +392,7 @@ switch (day) {
392
392
393
393
It's very easy to understand. We just pass the variable into the `()`. It means that our target is `day`. We can choose or add many conditions for that variable with `case` keyword. For example, if `day` is `4`, output will be `"Thursday"`. After each `case`, we use `break` keyword, because `break` keyword will stop rest execution after executes the matched condition.
394
394
395
-
**Note**: The colon (`:`) after case, is exactly like `{ .. }` for `if..else` statement .
395
+
**Note**: The `:` colon after `case`, is exactly like `{ .. }` for `if..else` statement .
396
396
397
397
For more information, go to [this](https://stackoverflow.com/questions/32576618/switch-statement-to-compare-values-greater-or-less-than-a-number/32576647) link.
398
398
@@ -433,9 +433,9 @@ for (;;) {
433
433
}
434
434
```
435
435
436
-
It looks like a infinite loop (with no conditions).
436
+
It looks like a infinite loop (without conditions).
437
437
438
-
We also have a strage type of this loop, but we don't recommend you to this:
438
+
We also have a strage type of this loop, but we don't recommend you to do this:
@@ -541,7 +541,7 @@ This loop executes code as long as a condition is true. For example:
541
541
let i =0;
542
542
while (true) {
543
543
console.log(i);
544
-
i++; //--infinite loop
544
+
i++; // infinite loop
545
545
}
546
546
```
547
547
@@ -567,7 +567,7 @@ do {
567
567
568
568
That means: **do** something, **while** that condition is `true` (or reach).
569
569
570
-
**Note**: In `do..while` loops, we (you) need put semicolon (`;`) after `while` loop. Consider:
570
+
**Note**: In `do..while` loops, we (you) need put `;` semicolon after `while` loop. Consider:
571
571
572
572
```js
573
573
do {
@@ -577,7 +577,7 @@ do {
577
577
578
578
## `break` and `continue` Keywords
579
579
580
-
**JavaScript** (and other programming languages) have two important keyword.
580
+
**JavaScript** (and other programming languages) have two important keywords.
581
581
582
582
> `break`
583
583
@@ -624,7 +624,7 @@ whileLoop: do {
624
624
} while (number <10);
625
625
```
626
626
627
-
We defined `whileLoop` label (you can choose another name for it), and we created an `if` statement (condition) to stop executing code after `number` equal to `4`. This technique rarely use in **JavaScript**.
627
+
We defined `whileLoop` label (you can choose whatever you want for it), and we created an `if` statement (condition) to stop executing code after `number` equal to `4`. This technique rarely use in **JavaScript**.
628
628
629
629
## Nested Loops and Conditions
630
630
@@ -785,7 +785,7 @@ function getMaxLifeValue() {
785
785
786
786
// check for invalid entered number
787
787
if (isNaN(parsedValue) || parsedValue <=0) {
788
-
throw"Invalid user input, nnot a number!";
788
+
throw"Invalid user input, not a number!";
789
789
}
790
790
return parsedValue;
791
791
}
@@ -797,7 +797,7 @@ try {
797
797
} catch (error) {
798
798
console.log(error);
799
799
maxLife =100;
800
-
alert("You enterd something wrong, deafult value of 100 was used.");
800
+
alert("You entered something wrong, deafult value of 100 was used.");
0 commit comments