-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathchapter1.js
156 lines (128 loc) · 3.29 KB
/
chapter1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* JavaScript fundamentals includes comments, variable and operator
* JavaScript is a duck type language.
* @author Mahmud Ahsan
* {@link https://github.com/mahmudahsan/javascript}
*/
// Single line comment
/**
* Multiline
* comment
*/
/**
* Variable and Data Type
* 1. Number
* 2. String
*/
var number1 = 200; // number: integer
let number2 = 300.50; // number: floating-point
const name = "Aladin"; // string
const boolean = true; // true | false
const nothing = null; // absent of object
const undef = undefined; // absent of value
// const means constant. It can not be modifed once defined
console.log(number1, typeof number1);
console.log(number2, typeof number2);
console.log(name, typeof name);
console.log(boolean, typeof boolean);
console.log(nothing, typeof nothing);
console.log(undef, typeof undef);
console.log();
// var exist in function scope
{
var str = "Hello World";
console.log(str);
}
console.log(str); // Okay no error
console.log();
// let and const is in block scope
{
let localStr1 = "Life is Cool";
const localStr2 = "Nothing is impossible";
console.log(localStr1);
console.log(localStr2);
}
//console.log(localStr1); // ReferenceError: localStr1 is not defined
//console.log(localStr2); // ReferenceError: localStr2 is not defined
console.log();
/**
* null vs undefined
* you can use any to describe absent of value
*/
const absent1 = null;
const absent2 = undefined;
console.log(absent1 == absent2); // true
console.log(absent1 === absent2); // value and identity check false
console.log();
/**
* Spcial Numbers
*/
const sp1 = Infinity;
const sp2 = -Infinity;
const sp3 = NaN; // Not a number
console.log(sp1, typeof sp1);
console.log(sp2, typeof sp2);
console.log(sp3, typeof sp3);
console.log((0/0)); // output: Nan
console.log();
/**
* String
* Single Quote, Double Quote and Backtick
*/
const strType1 = "Life is good";
const strType2 = 'Let\'s do it';
const strType3 = `Sum of 2+2=${2+2}`; // template literal
// Template literal ${} inside value computed and converted to string
console.log(strType1);
console.log(strType2);
console.log(strType3);
/**
* Expression
* In computer programming expression part of code that generates value
*/
console.log(2+2); // 2+2 is expression
console.log();
/**
* Operators
*/
// Unary Operator
let uVal = 10;
console.log(-uVal);
++uVal; // --uVal => pre-increment, pre-decrement
console.log(uVal); // 11
uVal++; // uVal-- => post-increment, post-decrement
console.log(uVal); // 12
// Difference between ++uVal and uVal++
uVal = 100;
console.log(++uVal);
console.log(uVal);
console.log(uVal++);
console.log(uVal);
console.log();
// Binary Operator
let bNum1 = 20;
let bNum2 = 40;
let resultBN = bNum1 + bNum2; // bNum1 - bNum2
console.log(`${bNum1}+${bNum2}=${bNum1 + bNum2}`);
resultBN = resultBN + 10;
console.log(resultBN);
resultBN += 1; // ++resultBN or resultBN++
console.log(resultBN);
console.log();
resultBN = 100;
console.log(resultBN);
resultBN = resultBN - 10;
console.log(resultBN);
resultBN = resultBN * 2;
console.log(resultBN);
resultBN = resultBN / 7;
console.log(resultBN); // 25.714285714285715
console.log(Math.floor(resultBN)); // 25
console.log();
// Remainder operator
let x = 10;
let y = 3;
console.log(x % y);
// Exponential function
let aNum = 2;
console.log("Exponential: " + Math.pow(aNum, 8));