GeezerVison is showing github.com — Add base for if condition · guptag-dev/javascript@6aedea1
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: SUMMARY.md +5-1Lines changed: 5 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
11 | 11 | * [Strings](strings/README.md) |
12 | 12 | * [Creation](strings/create.md) |
13 | 13 | * [Concatenation](strings/concat.md) |
14 | | - * [Length](strings/length.md) |
| 14 | + * [Length](strings/length.md) |
| 15 | +* [Conditional Logic](conditional/README.md) |
| 16 | + * [If](conditional/if.md) |
| 17 | + * [Else](conditional/else.md) |
| 18 | + * [Comparators](conditional/comparators.md) |
Collapse file
Copy file name to clipboardExpand all lines: comments/README.md Collapse file
Copy file name to clipboard +9Lines changed: 9 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +# Conditional Logic |
| 2 | + |
| 3 | +A condition is a test for something. Conditions are very important for programming, in several ways: |
| 4 | + |
| 5 | +First of all conditions can be used to ensure that your program works, regardless of what data you throw at it for processing. If you blindly trust data, you’ll get into trouble and your programs will fail. If you test that the thing you want to do is possible and has all the required information in the right format, that won’t happen, and your program will be a lot more stable. Taking such precautions is also known as programming defensively. |
| 6 | + |
| 7 | +The other thing conditions can do for you is allow for branching. You might have encountered branching diagrams before, for example when filling out a form. Basically, this refers to executing different “branches” (parts) of code, depending on if the condition is met or not. |
| 8 | + |
| 9 | +In this chapter, we'll learn the base of conditional logic in Javascript. |
Collapse file
Copy file name to clipboard +58Lines changed: 58 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +# Condition If |
| 2 | + |
| 3 | +The easiest condition is an if statement and its syntax is `if(condition){ do this … }`. The condition has to be true for the code inside the curly braces to be executed. You can for example test a string and set the value of another string dependent on its value: |
| 4 | + |
| 5 | +```javascript |
| 6 | +var country = 'France'; |
| 7 | +var weather; |
| 8 | +var food; |
| 9 | +var currency; |
| 10 | + |
| 11 | +if(country === 'England'){ |
| 12 | + weather = 'horrible'; |
| 13 | + food = 'filling'; |
| 14 | + currency = 'pound sterling'; |
| 15 | +} |
| 16 | + |
| 17 | +if(country === 'France'){ |
| 18 | + weather = 'nice'; |
| 19 | + food = 'stunning, but hardly ever vegetarian'; |
| 20 | + currency = 'funny, small and colourful'; |
| 21 | +} |
| 22 | + |
| 23 | +if(country === 'Germany'){ |
| 24 | + weather = 'average'; |
| 25 | + food = 'wurst thing ever'; |
| 26 | + currency = 'funny, small and colourful'; |
| 27 | +} |
| 28 | + |
| 29 | +var message = 'this is ' + country + ', the weather is ' + |
| 30 | + weather + ', the food is ' + food + ' and the ' + |
| 31 | + 'currency is ' + currency; |
| 32 | +``` |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +Fill up the value of `name` to validate the condition. |
| 37 | + |
| 38 | +```js |
| 39 | +var name = |
| 40 | + |
| 41 | +if (name == "John") { |
| 42 | + |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +```js |
| 47 | +var name = "John"; |
| 48 | + |
| 49 | +if (name == "John") { |
| 50 | + |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +```js |
| 55 | +assert(name == "John"); |
| 56 | +``` |
| 57 | + |
| 58 | +--- |