JavaScript Part 2
JavaScript Part 2
JavaScript Part 2
Director/CTO Panacloud
PIAIC
github.com/zeeshanhanif
JavaScript linkedin.com/in/zeeshanhanif
Part 2
Book we will
follow
Below link provide you
online exercises
Online http://asmarterwaytolearn.c
Exercises om/js/index-of-exercises.ht
ml
Installation
<html>
<head>
<script src="./index.js"></script>
</head>
<body>
Hello World
</body>
</html>
index.js file
alert("Hello World");
Initial Code and Setup
alert("Hello World");
3. alert is a keyword
4. The quoted text "Thanks for your input!" is called a text
string or simply a string.
Alerts
console.log("Hello World");
console.log(2+8);
document.write
document.write("Hello Word");
document.write(2+8);
Variable name/identifier
Variables
1. Complex Types
a. Objects
b. Functions
var name;
var age;
Undefined
1. This is another special data type that can have only one
value-the null value.
2. A null value means that there is no value.
3. It is not equivalent to an empty string ("") or 0, it is simply
nothing
var name = null;
var nationality = "Mark";
nationality = null;
Difference between null and undefined
1. You can use the JavaScript typeof operator to find the type
of a JavaScript variable.
2. The typeof operator returns the type of a variable or an
expression
var a = 4; // Statement 1
var b = 2; // Statement 2
var c = 0; // Statement 3
c = a + b; // Statement 4
alert(a); // Statement 4
console.log(c); // Statement 4
End of Statement with semicolon ;
1. Legal names:
1. Illegal names:
var userResponse
var userResponseTime
var userResponseTimeLimit
var response
var a = 5;
var b = 3;
var c = a + b; // Addition, result 8
var d = a - b; // Subtraction, result 2
var e = a * b; // Multiplication, result 15
var f = a / b; // Division, result 1.66
var g = a % b; // Modulus, result 2
var h = a ** b; // Exponentiation, result 125
Assignment Operators
Example Same as
a += 5; a = a + 5;
a -= 5; a = a - 5;
a *= 5; a = a * 5;
a /= 5; a = a / 5;
a %= 5; a = a % 5;
a **= 5; a = a ** 5;
Eliminating ambiguity -- BODMAS
var a = 5 + 2 * 3 - 2 / 2; // result 10
B Brackets first
var a = 5 + 2 * (3 - 2) / 2; // result 6
var a = 3 + 5 * 2; // result 13
var b = 8 / 2 - 1; // result 3
var c = 3 % 2 + 4 - 1; // result 4
var d = a + 5 * c - b / (3 + b);// result 32.5
Operator Precedence -- Few of them
Increment and Decrement Operator
Prefix Increment
Postfix Increment
Prefix Decrement
Postfix Decrement
Example 1
var a = 4;
var b = 2;
var c = a++ - b; // first value of a placed here
which is 4 then increase 1 in a so will become 5
alert(a); // 5
alert(b); // 2
alert(c); // 2
Increment and Decrement Operator
Example 2
var a = 4;
var b = 2;
var c = ++a + b; // first value of a increased so
will become 5 then value of a will be placed here, 5
alert(a); // 5
alert(b); // 2
alert(c); // 7
Increment and Decrement Operator
Example 3
var a = 4;
var b = 3;
var c = a++ + --b - --a;
alert(a); // 4
alert(b); // 2
alert(c); // 2
Increment and Decrement Operator
Example 4
var a = 4;
var b = 3;
var c = ++a + b++ - a + --b;
alert(a); // 5
alert(b); // 3
alert(c); // 6
String Concatenation
Concatenating Text strings
1. If you are sure that string has number, then you have to
convert it into number to perform addition
2. We need to use parseInt function for conversion
!== not equal value or not equal type <= less than and equal to
Comparison operators
var a = 3;
console.log(a == 6); //return false
console.log(a === 6); //return false
console.log(a != 6); //return true
console.log(a !== 6); //return true
console.log(a > 6); //return false
console.log(a < 6); //return true
console.log(a >= 6); //return false
console.log(a <= 6); //return true
Comparison operators
var a = 3;
console.log(a == 3); //return true
console.log(a == "3"); //return true
console.log(a == 6); //return false
var a = 60;
var b = a > 50 && a < 70;
alert(b); // return true
var c = 80;
var d = c > 50 && c < 70;
alert(d); // return false
|| Logical Operator
var a = 60;
var b = a < 50 || a > 70;
alert(b); // return false
var c = 80;
var d = c < 50 || c > 70;
alert(d); // return true
! Logical NOT
var a = 60;
var b = !(a < 50);
alert(b); // return true
var c = 80;
var d = !(c > 50);
alert(d); // return false
! Logical NOT
alert( !1 ); // false
alert( !0); // true
2. Here 1 will be converted to boolean first and it will be true
and because of ! operator opposite will return
3. Same for 0, it will be converted to false and ! will negate it to
true
!! Double NOT
if (condition) {
// block of code to be executed if the condition
is true
}
Conditions: if
if (condition1) {
//block of code to be executed if condition1 is true
} else if (condition2) {
//block of code to be executed if the condition1 is false
and condition2 is true
} else {
//block of code to be executed if the condition1 is false
and condition2 is false
}
Conditions: else if
for ( ; ; ){
console.log("Hello");
}
For Loop
console.log("3 x 1 = 3");
console.log("3 x 2 = 6");
console.log("3 x 3 = 9");
console.log("3 x 4 = 12");
console.log("3 x 5 = 15");
For Loop
Task **
***
****
*****
******
*******
Arrays
Arrays
1. With Arrays you can create single variable and hold all
temprature in it.
2. With Array you will be able to find and sort temperature
easily
1. Array can be created for all datatypes or you can mix them
in single array
var arr1 = ["Hello", "World", "Bye"];
var arr2 = [29, 38, 16, 22];
var arr3 = [true,false,true,false,false];
var arr4 = [23.2, 45.8, 98.12];
var arr5 = [{name: "John"}, {name: "Jason"}];
var arr6 = [74, "Hello", true, {name: "John"}];
Accessing Array Elements
1. You can access array elements from any index and update
them
var foods = ["Pizza", "Burger", "Snacks"];
console.log(foods[1]); // Burger
foods[1] = "Sandwich"; // Updating existing element
console.log(foods[1]); // Sandwich
Last in First out
Stack
(LIFO)
Stack (Last in First out)
1. filter()
2. find()
3. indexOf()
4. lastIndexOf()
5. map()
6. reverse()
7. sort()
And others
String
String
1. toLowerCase() 6. charAt()
2. toUpperCase() 7. replace()
3. slice() 8. split()
4. indexOf()
5. lastIndexOf()
toLowerCase() function
1. charCodeAt() 7. replace()
2. concat() 8. search()
3. endsWith() 9. startsWith()
4. includes() 10. substr()
5. match() 11. substring()
6. repeat() 12. trim()
Math functions
Math class provides
many functions that
Math allows you to perform
mathematical tasks on
numbers
Math.round() function
var a = Math.round(4.7); // 5
var b = Math.round(4.1); // 4
var c = Math.round(4.5); // 5
var d = Math.round(-4.1); // -4
var e = Math.round(-4.7); // -5
var f = Math.round(-4.5); // -4
var g = Math.round(5); // 5
Math.ceil() function
var a = Math.ceil(4.7); // 5
var b = Math.ceil(4.1); // 5
var c = Math.ceil(-4.1); // -4
var d = Math.ceil(-4.7); // -4
Math.floor() function
var a = Math.floor(4.7); // 4
var b = Math.floor(4.1); // 4
var c = Math.floor(-4.1); // -5
var d = Math.floor(-4.7); // -5
Math.random() function
1. Math.pow() 6. Math.min()
2. Math.sqrt() 7. Math.max()
3. Math.abs() 8. Math.exp()
4. Math.sin() 9. Math.log()
5. Math.cos()
Controlling the length of decimals
new Date()
new Date(year, month, day, hours, minutes,
seconds, milliseconds)
new Date(milliseconds)
new Date(date string)
Creating Date Objects
new Date()
new Date(2019, 7, 11, 10, 25, 40, 300);
new Date(1565501140300);
new Date("2019/9/8 10:15:15");
new Date("2019/9/8 10:15:15");
new Date("January 12 2019 10:15:15");
Unix time
1. Declarations
function showMessage(message){
console.log(message);
}
2. Invoking
showMessage("Hello World");
Parameters vs. Arguments
1. Parameters:
a. Function parameters are listed inside the
parentheses () in the function definition.
2. Arguments:
a. Function arguments are the values received by the
function when it is invoked.
Parameters vs. Arguments
1. Declarations
Parameter
function showMessage(message){
console.log(message);
}
2. Invoking
Argument
showMessage("Hello World");
Passing Data to Function
multiply(3,6);
multiply(4,2);
Passing Data to Function
function showMessage(name){
console.log("Hello "+name);
}
showMessage("Mike");
showMessage("John");
Parameter Rules
function showMessage(name){
console.log("Hello "+name);
}
showMessage("John"); // Hello John
showMessage(45); // Hello 45
showMessage(true); // Hello true
showMessage(); // Hello undefined
showMessage("Mike", 12);// Hello Mike
Function Return
function test(){
function test(){
return 45;
}
var a = test(); // return 45
console.log(a); // 45
Function Return
var num = 5;
function changeValue(a){
a = 7; // change to a will not affect num
}
changeValue(num);
console.log(num);//5, num will be be updated
Arguments Passed by Reference
function factorial(n) {
if (n <= 1) {
// Recursion will stop when this condition match
return 1;
} else {
return n * factorial(n - 1); // calling itself
}
}
Switch Statement
switch(expression) {
case 'value1': // same as if (expression === 'value1')
// code block
break;
case 'value2':
// code block
break;
default:
// code block
}
Switch Statement
var day = 3;
switch (day) {
case 6:
console.log("Today is Saturday");
break;
case 0:
console.log("Today is Sunday");
break;
default:
console.log("Looking forward to the Weekend");
}
Switch - Grouping of case
var day = 3;
switch (day) {
case 6: // No break
console.log("Today is Saturday");
case 0: // No break in last case, both will execute
console.log("Today is Sunday");
break;
default:
console.log("Looking forward to the Weekend");
}
Switch - Grouping of case
var day = 3;
switch (day) {
case 6:
case 0:
console.log("Yaaaa! It’s Weekend");
break;
default:
console.log("Looking forward to the Weekend");
}
Switch - Strict Comparison
var x = "0";
switch (x) {
case 0:
console.log("Off");
break;
case 1:
console.log("On");
break;
default: // this will execute as value did not match
console.log("No value found");
}
While loop
Syntax:
while (condition) {
// code block to be executed
}
While loop
1. In this example, the code in the loop will run, over and over
again, as long as a variable (i) is less than 10:
while (i < 10) {
console.log("I " + i);
i++;
}
2. You will use while loop when execution is dependent on
user input
Do/While loop
Syntax:
do {
// code block to be executed
}
while (condition);
Do/While loop
do {
console.log("I " + i);
i++;
}
while (i < 10);
Part 2 Ends