Learning Javascript From Mr Saem
2
3
4
HTML CSS JavaScript 5
6
7
8
9
First Script 10
Add Javascript to it 11
Script Position 12
13
Should end with 14
15
16
Not to use In-Line Script 17
CSS at the Top Scripts at the bottom 18
19
20
21
22
23
24
Variables 25
We create variable to hold some data. So variable is a container. That works as a little piece of computer memory. 26
These need to be in the lowercase. The name of the variable is up to us. No spaces are allowed while naming these. These could be letters, Numbers, _ or $ But you cannot start with a number 27
28
29
30
You may split the variables into one line aswell 31
32
33
You may use “ “ or ‘ ’. But do not mix them such as “ ‘ Javascript understands the data type on its own by the way we assign it. It will understand boolean /logic when we define it as true/false 34
We need to ask questions so we shall use IF for that 35
You will always find these in pairs if you have an opening one you will find A closing one as well. Even if it is after many multiple lines. But it needs to be there 36
A All conditions should boil down into TRUE or FALSE TRUE FALSE 37
oth 38 So both are not =
39
40
Not equal to 41
Code Block 42
var amount = 500; if ( amount < 1000 ) { alert ("Its less than 1000"); } 43
Using else 44
45
46
47 = is a command. NOT a polite description We are setting a value We are assigning a value
48 OR
Some operators are considered more important than others eg Particularlay * and / are regarded more important than others. 49
50
IF you are checking values are equal use the == operator 51
52
53 You are telling You are asking Exactly equal
54
55 AND OR
56
57
58
var a=15; alert(++a); 59 What should the output be?
var a=15; alert(a++); 60 What should the output be?
61
62
Loops 63
64
65
var a=1; while (a<10) { alert(a); } 66 Infinite Loop Created While Loop
var a=1; while (a<10) { alert(a); a++; } 67 While Loop (Output)
var a=100; while (a<10) { alert(a); a++; } 68 While Loop (Output).What should it be ?
Do While. The output ? var a=100; do { alert(a); a++; } while (a<10); 69
70
A short hand way 71
72
The for loop 73 OUTPUT
Three Parts of Four Loop 74 VARIABLE DECLARATION - The first part of the loop which initializes the variable at the beginning of the loop to some value. This value is the starting point of the loop. INCREMENT STATEMENT - The increment statement is the third part of the loop. It is the part of the loop that changes the value of the variable created in the variable declaration part of the loop. The increment statement is the part of the loop which will eventually stop the loop from running. CONDITION - The second part of the loop, and it is the part that decides whether the loop will continue running or not. While the condition in the loop is TRUE, it will continue running. Once the condition becomes FALSE, the loop will stop.
The while loop The code within a while loop will execute while the specified condition is true. 75
In the above code, a variable named num is initialized with the value of 0. The condition in the while loop is that while num is less than 25, 5 should be added to num. Once the value of num is greater than 25, the loop will stop executing The while loop
The do-while loop The do-while loop is very similar to the while loop, but it does things in reverse order. The mechanism of the while loop is - while a condition is true, perform a certain action. The mechanism of the do-while loop is - perform a certain action while a condition is true. Furthermore, the code within a do-while loop will always execute at least once, even if the specified condition is false. This is because the code is executed before the condition is tested. 77
Syntax: do{ execute this code; } while (condition); In the above code, a variable named num is initialized with the value of 5. The condition in the do-while loop is that while num is less than 25, 5 should be added to num. Once the value of num is greater than 25, the loop will stop executing The do-while loop
Breaking out of a loop You can completely break out of a loop when it is still running. This is achieved with the break keyword. Once a loop is exited, the first statement right after it will be executed. The break keyword provides an easy way to exit a loop if an error occurs, or if you found what you were looking for 79
In the above example, the for loop is set to iterate 9 times and print the current value of the variable a during each iteration. The if statement within the loop states that when the variable a is equal to 5, break out of the loop Breaking out of a loop
81