Skip to content

Commit 21ba717

Browse files
authored
Merge pull request Swap76#1 from Swap76/master
take latest
2 parents bcd116f + c91d0ec commit 21ba717

File tree

3 files changed

+184
-4
lines changed

3 files changed

+184
-4
lines changed
Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,74 @@
1-
# IF, ELSE IF, ELSE CONDITION
1+
# IF, ELSE IF, ELSE CONDITION
2+
Conditional statements run specific blocks of code based on certain parameters that you set. Like other code it reads top to bottom so the order that you put your conditions in matter.
3+
4+
Example of an if/else if/else:
5+
```js
6+
if(this statement is true){
7+
do this
8+
} else if (this statement is true){
9+
do this
10+
} else{
11+
do this
12+
}
13+
```
14+
`If` always comes first and have a set of parenthesis with a statement in them that you are checking to see if it's true. If it is, the code in the curly brackets is run, otherwise it would move on to the `else if`, and see if that statement is true. If `else if` is true then it runs that block of code, if not then it would run the code in the `else` statement. Once a statement is deemed true and the code is run it doesn't continue to read through the code even if there is a more accurate statement lower down. A good example of where this matters would be the common coding challenge fizzbuzz. The idea of fizzbuzz is that you want to log all the numbers from 1-100 however if it's divisible by 3 print "fizz", 5 print "buzz", and if it's divisible by both 3 and 5 print "fizzbuzz".
15+
If we write our if's the way the problem is stated we would would write:
16+
```
17+
if(num % 3 == 0){
18+
console.log("fizz")
19+
} else if(num % 5 == 0){
20+
console.log("buzz")
21+
} else if(num % 3 == 0 && num % 5 == 0){
22+
console.log("fizzbuzz")
23+
} else {
24+
console.log(num)
25+
}
26+
```
27+
In this case every time we'd hit a number that should print fizzbuzz we'd get fizz because that is a truthful statement and it's not looking for a more correct answer.
28+
To account for that we'd want to adjust our statement order to:
29+
```js
30+
if(num % 3 == 0 && num % 5 == 0){
31+
console.log("fizzbuzz")
32+
} else if(num % 5 == 0){
33+
console.log("buzz")
34+
} else if(num % 3 == 0){
35+
console.log("fizz")
36+
} else {
37+
console.log(num)
38+
}
39+
```
40+
41+
## What if we don't write else if but just another if?
42+
Sometimes you see code that has a number of `if`'s in a row but no `else if`'s or `else`'s in this case it's checking each condition in that case each `if`
43+
44+
This code will only print "it's a number":
45+
```js
46+
var number = 2
47+
if(typeof number === "number"){
48+
console.log("it's a number")
49+
} else if(number % 2 === 0){
50+
console.log("it's even")
51+
}
52+
```
53+
However this code will print both "it's a number" and "it's even"
54+
```js
55+
var number = 2
56+
if(typeof number === "number"){
57+
console.log("it's a number")
58+
}
59+
if(number % 2 === 0){
60+
console.log("it's even")
61+
}
62+
```
63+
64+
## What if we only have an if?
65+
If there is only one `if` then it only does that code `if` the condition is true and nothing if it's false.
66+
67+
For example:
68+
```js
69+
if(number % 2 === 0){
70+
console.log("it's even")
71+
}
72+
console.log(number)
73+
```
74+
In this case if `number` is even it will print it's even and it will always print the number.

docs/JavaScript_Basics/objects.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ Objects are variables too. But objects can contain many values.
2626
The values are written as name : value pairs (name and value separated by a colon).
2727

2828
Example:
29-
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
30-
29+
```js
30+
var person = {
31+
firstName:"John",
32+
lastName:"Doe",
33+
age:50,
34+
eyeColor:"blue"
35+
};
36+
```

docs/JavaScript_Basics/this.md

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,102 @@
1-
# This
1+
2+
# 'this' keyword
3+
4+
JavaScript's 'this' keyword refers to the object it belongs to. It's values depends on where it is used.
5+
6+
7+
## 'this' as global variable
8+
If `this` is used alone, it refers to a global object. In browsers it refers to Window Object as Global Object.
9+
10+
```javascript
11+
var k = this;
12+
console..log(k)
13+
14+
/* Output
15+
16+
Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}
17+
18+
*/
19+
20+
```
21+
22+
## 'this' in a function
23+
24+
In JavaScript, by default the owner of the function is `this`. The value of this in a function refers to the object it belongs to. In below example, `this` refers to window object, so instead of printing '5' it refers to global value '10'.
25+
26+
```javascript
27+
var x =10;
28+
function printX() {
29+
var x = 5;
30+
console.log(this.x)
31+
}
32+
33+
printX(); /* its like this.printX() or window.printX() */
34+
35+
36+
/* Output
37+
10 /* this refers to global object so prints 10 */
38+
*/
39+
```
40+
41+
## 'this' in function - strict mode
42+
43+
if `use strict` mode is enabled, `this` don't refer to the global object by default. It gives `undefined` when a function is called without explicitly using `this`
44+
45+
```javascript
46+
// setInterval This runs a code after specific interval of time till we stop the timer.
47+
`use strict'
48+
var x =10;
49+
function printX() {
50+
var x = 5;
51+
console.log(this.x)
52+
}
53+
this.printX();
54+
printX();
55+
56+
/* Output
57+
10 // printX() called with this
58+
Uncaught TypeError: Cannot read property 'x' of undefined // printX() called without this
59+
*/
60+
```
61+
62+
## 'this' in a method
63+
64+
In JavaScript, `this` in a method refers to the object of which the method belongs to. In below example, the `printX` method belongs to person object. so `this` refers to person object.
65+
66+
```javascript
67+
var firstName = "John";
68+
var person = {
69+
firstName: "Jenny",
70+
printX : function() {
71+
console.log(this.firstName)
72+
}
73+
}
74+
person.printX();
75+
76+
/* Output
77+
Jenny
78+
*/
79+
```
80+
81+
## Explicit function binding
82+
Calling `object A` method, using values of `object B` is called Explicit function binding. JavaScript provides two method `call()` and `apply()`
83+
84+
For more details read on [call()](/JavaScript_Basics/call) and [apply()](/JavaScript_Basics/apply) .
85+
86+
87+
```javascript
88+
var personA = {
89+
firstName: "Jenny",
90+
printX : function() {
91+
console.log(this.firstName)
92+
}
93+
}
94+
var personB = {
95+
firstName: "John"
96+
}
97+
personA.printX.call(personB);
98+
99+
/* Output
100+
John
101+
*/
102+
```

0 commit comments

Comments
 (0)