Skip to content

Commit b419027

Browse files
committed
add number and object types
1 parent 2b06e97 commit b419027

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

07-types-number.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
## Number
3+
4+
Only **one** type of number in JavaScript whether it has decimal point or not.
5+
6+
```javascript
7+
const age = 100;
8+
const money = 1000.50
9+
console.log(typeof age); // number
10+
console.log(typeof money); // number
11+
12+
```
13+
14+
- `typeof` is used to find out the 'type' of a variable.
15+
16+
- Various operations: addition, subtraction, multiplication, division can be done with nos.
17+
18+
- Example
19+
20+
`"10" * "10" // 100 (number) - converts the strings to number`
21+
22+
23+
The above works with _multiplication, division and subtraction and not addition,_ because the + sign is also used for concatenation.
24+
25+
- **Math helper methods:**
26+
27+
- **Math.round, Math.floor, Math.ceil, Math.random** and many others
28+
29+
```javascript
30+
Math.round(2.5); // 3
31+
Math.floor(2.4); // 2
32+
Math.ceil(2.4); // 3
33+
Math.random(); // 0.565262543048269 - random no. between 0 and 1
34+
35+
```
36+
37+
- **Modulo and Power operators:**
38+
39+
```javascript
40+
const smarties = 20;
41+
const kids = 3;
42+
const eachKidGets = Math.floor(smarties/kids); // 6
43+
const leftSmarties = smarties % kids; // 2 - modulo operation
44+
45+
const x = 2 ** 3; // 8 - power operation using power operator (**)
46+
// or
47+
const x = Math.pow(2,3); // 8 - power operation using Math.pow
48+
49+
```
50+
51+
- Example
52+
53+
0.1 + 0.2 // 0.30000000000000004
54+
55+
56+
Why? [Explanation](http://0.30000000000000004.com/)
57+
58+
So, when working with money, don't store them as dollars and cents. Store all of the money in cents as you won't have to deal with fractions only whole nos. When need to display to user, just convert them back.
59+
60+
- **Infinity and Negative Infinity:**
61+
62+
`typeof Infinity; // number`
63+
64+
`typeof -Infinity; // number`
65+
66+
- **Not a Number (NaN):**
67+
68+
`10 / 'dog' // NaN`
69+
70+
`typeof NaN // number`

08-types-object.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
## Object
3+
4+
- Everything in JavaScript is an Object.
5+
6+
- Objects are used for collection of data, collection of functionality.
7+
8+
- Example:
9+
10+
```javascript
11+
const person = {
12+
name: 'Soumya', // property: value
13+
age: 100
14+
};
15+
16+
typeof person // object
17+
18+
person.age = 101;
19+
console.log(person.age); // 101
20+
21+
```
22+
23+
24+
.
25+
26+
- Order of properties doesn't matter in an object.
27+
- **Accessing properties:**
28+
- `person.name // Soumya` (dot notation)

0 commit comments

Comments
 (0)