Skip to content

Commit c63f7c8

Browse files
committed
merged 2nd ed
1 parent 7e2e647 commit c63f7c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1355
-0
lines changed

chapter01/01-HelloWorld.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-HelloWorld.js"></script>
9+
<script type="text/javascript">
10+
/*alert('Hello, World!');
11+
console.log('Hello, World!');*/
12+
</script>
13+
</body>
14+
</html>

chapter01/01-HelloWorld.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function output(t) {
2+
document.write('<p>' + t + '</p>');
3+
return;
4+
}
5+
6+
alert('Hello, World!');
7+
console.log('Hello, World!');
8+
output('Hello, World!');

chapter01/02-Variables.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="02-Variables.js"></script>
9+
</body>
10+
</html>

chapter01/03-Operators.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="03-Operators.js"></script>
9+
</body>
10+
</html>

chapter01/03-Operators.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Arithmetic operators */
2+
var num = 0;
3+
4+
console.log('num value is ' + num);
5+
6+
num = num + 2;
7+
8+
console.log('New num value is ' + num);
9+
10+
num = num * 3;
11+
12+
console.log('New num value is ' + num);
13+
14+
num = num / 2;
15+
16+
console.log('New num value is ' + num);
17+
18+
num++;
19+
20+
num--;
21+
22+
console.log('New num value is ' + num);
23+
24+
console.log('num mod 2 value is ' + (num % 2));
25+
26+
27+
/* Assignment operators */
28+
num += 1;
29+
num -= 2;
30+
num *= 3;
31+
num /= 2;
32+
num %= 3;
33+
34+
console.log('New num value is ' + num);
35+
36+
/* Assignment operators */
37+
console.log('num == 1 : ' + (num == 1));
38+
console.log('num === 1 : ' + (num === 1));
39+
console.log('num != 1 : ' + (num != 1));
40+
console.log('num > 1 : ' + (num > 1));
41+
console.log('num < 1 : ' + (num < 1));
42+
console.log('num >= 1 : ' + (num >= 1));
43+
console.log('num <= 1 : ' + (num <= 1));
44+
45+
/* Logical operators */
46+
console.log('true && false : ' + (true && false));
47+
console.log('true || false : ' + (true || false));
48+
console.log('!true : ' + (!true));
49+
50+
/* Bitwise operators */
51+
console.log('5 & 1:', (5 & 1)); //same as 0101 & 0001 (result 0001 / 1)
52+
console.log('5 | 1:', (5 | 1)); //same as 0101 | 0001 (result 0101 / 5)
53+
console.log('~ 5:', (~5)); //same as ~0101 (result 1010 / 10)
54+
console.log('5 ^ 1:', (5 ^ 1)); //same as 0101 ^ 0001 (result 0100 / 4)
55+
console.log('5 << 1:', (5 << 1)); //same as 0101 << 1 (result 1010 / 10)
56+
console.log('5 >> 1:', (5 >> 1)); //same as 0101 >> 1 (result 0010 / 2)
57+
58+
/* typeOf */
59+
console.log('typeof num:', typeof num);
60+
console.log('typeof Packt:', typeof 'Packt');
61+
console.log('typeof true:', typeof true);
62+
console.log('typeof [1,2,3]:', typeof [1,2,3]);
63+
console.log('typeof {name:John}:', typeof {name:'John'});
64+
65+
/* delete */
66+
var myObj = {name: 'John', age: 21};
67+
delete myObj.age;
68+
console.log(myObj); //Object {name: "John"}

chapter01/04-TruthyFalsy.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="04-TruthyFalsy.js"></script>
9+
</body>
10+
</html>

chapter01/04-TruthyFalsy.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function testTruthy(val){
2+
return val ? console.log('truthy') : console.log('falsy');
3+
}
4+
5+
testTruthy(true); //true
6+
testTruthy(false); //false
7+
testTruthy(new Boolean(false)); //true (object is always true)
8+
9+
testTruthy(''); //false
10+
testTruthy('Packt'); //true
11+
testTruthy(new String('')); //true (object is always true)
12+
13+
testTruthy(1); //true
14+
testTruthy(-1); //true
15+
testTruthy(NaN); //false
16+
testTruthy(new Number(NaN)); //true (object is always true)
17+
18+
testTruthy({}); //true (object is always true)
19+
20+
var obj = {name:'John'};
21+
testTruthy(obj); //true
22+
testTruthy(obj.name); //true
23+
testTruthy(obj.age); //age (prop does not exist)

chapter01/05-EqualsOperators.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="05-EqualsOperators.js"></script>
9+
</body>
10+
</html>

chapter01/05-EqualsOperators.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//Packt == true
2+
3+
console.log('packt' ? true : false);
4+
//outputs true
5+
6+
console.log('packt' == true);
7+
//1 - converts Boolean using toNumber
8+
//'packt' == 1
9+
//2 - converts String using toNumber
10+
//NaN == 1
11+
//outputs false
12+
13+
console.log('packt' == false);
14+
//1 - converts Boolean using toNumber
15+
//'packt' == 0
16+
//2 - converts String using toNumber
17+
//NaN == 0
18+
//outputs false
19+
20+
console.log([0] == true);
21+
//1 - converts Boolean using toNumber
22+
//[0] == 1
23+
//2 - converts Object using toPrimitive
24+
//2.1 - [0].valueOf() is not primitive
25+
//2.2 - [0].toString is 0
26+
//0 == 1
27+
//outputs false
28+
29+
//******************************* ===
30+
console.log('packt' === true); //false
31+
32+
console.log('packt' === 'packt'); //true
33+
34+
var person1 = {name:'John'};
35+
var person2 = {name:'John'};
36+
console.log(person1 === person2); //false, different objects
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="06-ConditionalStatements.js"></script>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)