Skip to content

Commit a52992a

Browse files
committed
add session 2 notes
1 parent 4612ff0 commit a52992a

File tree

7 files changed

+274
-0
lines changed

7 files changed

+274
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# comparison
2+
3+
[Refer this tutorial](https://javascript.info/comparison)
4+
5+
- Equals: a == b, please note the double equality sign == means the equality test, while a single one a = b means an assignment.
6+
- All comparison operators return a boolean value:
7+
8+
A comparison result can be assigned to a variable, just like any value:
9+
10+
```javascript
11+
let result = 5 > 4; // assign the result of the comparison
12+
alert(result); // true
13+
```
14+
15+
in JavaScript, a non-empty string is always true.
16+
17+
```javascript
18+
alert(Boolean('0')); // true
19+
alert(Boolean(' ')); // spaces, also true (any non-empty string is true)
20+
```
21+
22+
# String comparison
23+
24+
strings are compared letter-by-letter.
25+
26+
at any index (which is character) in a string, if character from first string and a character from second string at same index, is greater than the other one then first string is grater.
27+
28+
```javascript
29+
console.log('Z' > 'A'); // true
30+
console.log('Glow' > 'Glee'); // true
31+
console.log('Bee' > 'Be'); // true
32+
```
33+
34+
![strcmp](../images/string%20comparison%20explanation.jpg)
35+
36+
# Comparison of different types
37+
38+
When comparing values of different types, JavaScript converts the values to numbers.
39+
40+
```javascript
41+
console.log('2' > 1); // true, string '2' becomes a number 2
42+
console.log('01' == 1); // true, string '01' becomes a number 1
43+
```
44+
45+
For boolean values, true becomes 1 and false becomes 0.
46+
47+
```javascript
48+
console.log(true == 1); // true
49+
console.log(false == 0); // true
50+
```
51+
52+
![intersting case](../images/boolean%20string%20comparison.jpg)
53+
54+
# Strict equality
55+
56+
A regular equality check == has a problem. It cannot differentiate 0 from false:
57+
The same thing happens with an empty string:
58+
59+
```javascript
60+
console.log(0 == false); // true
61+
console.log('' == false); // true
62+
```
63+
64+
**An empty string, becomes a zero while comparing.**
65+
66+
**strict equality operator === checks the equality without type conversion**
67+
68+
There is also a “strict non-equality” operator !== analogous to !=.
69+
70+
```javascript
71+
console.log(0 === false); // false, because the types are different
72+
```
73+
74+
# Comparison with null and undefined
75+
76+
**For a strict equality check ===**
77+
78+
```javascript
79+
console.log(null === undefined); // false, because each of them is a different type
80+
```
81+
82+
**For a non-strict check ==**
83+
84+
```javascript
85+
console.log(null == undefined); // true,
86+
```
87+
88+
**For maths and other comparisons < > <= >=**
89+
90+
null/undefined are converted to numbers: null becomes 0, while undefined becomes NaN.
91+
92+
**Rules with equality operator**
93+
94+
the equality check == for `undefined` and `null` is defined such that, without any conversions, they equal each other and don’t equal anything else.
95+
96+
```javascript
97+
null == undefined; // true
98+
null == 0; // (2) false
99+
```
100+
101+
**An incomparable undefined**
102+
103+
`undefined` gets converted to `NaN` and `NaN` is a special numeric value which returns false for all comparisons even with NaN comparison itself.
104+
105+
```javascript
106+
NaN != NaN; // true
107+
```
108+
109+
`undefined` only equals `null`, `undefined`
110+
111+
```javascript
112+
alert(undefined > 0); // false
113+
alert(undefined < 0); // false (2)
114+
alert(undefined == 0); // false (3)
115+
```
116+
117+
# What will be the result for these expressions?
118+
119+
```javascript
120+
5 > 4;
121+
'apple' > 'pineapple';
122+
'2' > '12';
123+
undefined == null;
124+
undefined === null;
125+
null == '\n0\n';
126+
null === +'\n0\n';
127+
```
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Type Conversions
2+
3+
Most of the time, operators and functions automatically convert the values given to them to the right type.
4+
5+
For example,
6+
7+
- **alert automatically converts any value to a string to show it**.
8+
- **Mathematical operations convert values to numbers.**
9+
10+
# String Conversion
11+
12+
call the String(value) function to convert a value to a string:
13+
14+
```javascript
15+
let value = true;
16+
alert(typeof value); // boolean
17+
18+
value = String(value); // now value is a string "true"
19+
alert(typeof value); // string
20+
21+
String(false); // "false"
22+
String(null); // "null"
23+
```
24+
25+
# Numeric Conversion
26+
27+
- Numeric conversion happens in **mathematical functions** and **expressions** automatically.
28+
- We can use the Number(value) function to explicitly convert a value to a number:
29+
30+
```javascript
31+
Number(''); //0
32+
Number(' '); //0
33+
```
34+
35+
![numconversion rules](../images/numconversionrule.jpg)
36+
37+
# Boolean conversion rules
38+
39+
Please note: the string with zero "0" is true
40+
41+
```javascript
42+
alert(Boolean('0')); // true
43+
alert(Boolean(' ')); // spaces, also true (any non-empty string is true)
44+
```
45+
46+
![bool conversions](../images/boolean%20string%20comparison.jpg)
47+
48+
# Precedence of operator
49+
50+
if you use same type of operators in an expression (i.e. expression contains numbers) then it will perform left to right operation
51+
52+
```javascript
53+
alert(5 + 5 + 'px'); // 10px
54+
alert('px' + 5 + 5); // px55
55+
```
56+
57+
if you have higher Precedence operators in expression the evaluation will be different
58+
59+
```javascript
60+
alert('px' + 5 * 5); // px25
61+
```
62+
63+
For more on this [read here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)
64+
65+
# Some special functions
66+
67+
## parseFloat() vs parseInt vs Number vs +"(string value)"
68+
69+
parseFloat/parseInt is for parsing a string, while Number/+ is for coercing a value to a number. They behave differently. But first let's look at where they behave the same:
70+
71+
```javascript
72+
parseFloat('3'); // => 3
73+
Number('3'); // => 3
74+
parseFloat('1.501'); // => 1.501
75+
Number('1.501'); // => 1.501
76+
parseFloat('1e10'); // => 10000000000
77+
Number('1e10'); // => 10000000000
78+
```
79+
80+
parseInt will trucate decimal part
81+
82+
```javascript
83+
parseInt('12.2'); // 12
84+
```
85+
86+
However, if your input starts with a number and then contains other characters, parseFloat truncates the number out of the string, while Number gives NaN (not a number):
87+
88+
```javascript
89+
parseFloat('1x'); // => 1
90+
parseInt('1x'); // => 1
91+
parseFloat('3px'); // => 3
92+
parseInt('3px'); // => 3
93+
Number('1x'); // => NaN
94+
```
95+
96+
Number understands hexadecimal input while parseFloat does not:
97+
98+
```javascript
99+
parseFloat('0x10'); // => 0
100+
parseInt('0x10'); // => 16
101+
Number('0x10'); // => 16
102+
```
103+
104+
Note that using the unary + operator is exactly the same as using Number as a function:
105+
106+
```javascript
107+
Number('0x10'); // => 16
108+
+'0x10'; // => 16
109+
Number('10x'); // => NaN
110+
+'10x'; // => NaN
111+
Number('40'); // => 40
112+
+'40'; // => 40
113+
```
114+
115+
For empty string, they are different.
116+
117+
- **+"" and Number("") returns 0**
118+
- **parseFloat("") and parseInt("") returns NaN**
119+
120+
```javascript
121+
parseFloat(''); // => NaN
122+
Number(''); // => 0
123+
parseFloat(' \r\n\t'); // => NaN
124+
Number(' \r\n\t'); // => 0
125+
```
126+
127+
## toString() vs String()
128+
129+
value.toString() will cause an error if value is `null` or `undefined`. String(value) should not.
130+
131+
```javascript
132+
var value = null;
133+
alert(value.toString()); // TypeError exception
134+
135+
var value = null;
136+
alert(String(value)); // "null"
137+
```
138+
139+
# object to primitive
140+
141+
![](../images/object-to-primitive.jpg)
142+
143+
for more [read here](https://javascript.info/object-toprimitive#tostring-valueof)
144+
145+
// TODO Remaining notes here

session 2/chapters/resources.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[javascript.info](https://javascript.info/)
2+
13
[Array Methods](https://www.digitalocean.com/community/tutorial_series/working-with-arrays-in-javascript)
24

35
[string methods - 1](https://www.digitalocean.com/community/tutorials/how-to-work-with-strings-in-javascript)
75.1 KB
Loading
81.7 KB
Loading
122 KB
Loading
37.2 KB
Loading

0 commit comments

Comments
 (0)