Skip to content

Commit 573a6b7

Browse files
committed
added new examples
1 parent 8646a22 commit 573a6b7

6 files changed

+126
-6
lines changed

chapter01/02-Variables.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,30 @@ var nullVar = null; //{6}
88
var und; //7
99

1010
console.log("num: "+ num);
11-
console.log("name: "+ num);
12-
console.log("trueValue: "+ num);
13-
console.log("price: "+ num);
11+
console.log("name: "+ name);
12+
console.log("trueValue: "+ trueValue);
13+
console.log("price: "+ price);
1414
console.log("nullVar: "+ nullVar);
15-
console.log("und: "+ und);
15+
console.log("und: "+ und);
16+
17+
//******* Variable Scope
18+
19+
var myVariable = 'global';
20+
myOtherVariable = 'global';
21+
22+
function myFunction(){
23+
var myVariable = 'local';
24+
return myVariable;
25+
}
26+
27+
function myOtherFunction(){
28+
myOtherVariable = 'local';
29+
return myOtherVariable;
30+
}
31+
32+
console.log(myVariable); //{1}
33+
console.log(myFunction()); //{2}
34+
35+
console.log(myOtherVariable); //{3}
36+
console.log(myOtherFunction()); //{4}
37+
console.log(myOtherVariable); //{5}

chapter01/03-Operators.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,27 @@ console.log('num < 1 : ' + (num < 1));
4242
console.log('num >= 1 : ' + (num >= 1));
4343
console.log('num <= 1 : ' + (num <= 1));
4444

45-
4645
/* Logical operators */
4746
console.log('true && false : ' + (true && false));
4847
console.log('true || false : ' + (true || false));
49-
console.log('!true : ' + (!true));
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

+10
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

+23
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

+10
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

+36
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

0 commit comments

Comments
 (0)