Lecture4 Javascript
Lecture4 Javascript
• Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in
both.
<head> <body>
<script> <script>
console.log(‘not best practice’); console.log(‘not best practice’);
</script> </script>
<script>
console.log(‘script2’); <h1>header 1</h1>
</script>
… <script>
</head> console.log(‘not best practice’);
</script>
<h2>header 2</h2>
<script>
console.log(‘above </body> is the best
practice’);
</script>
</body>
Javascript Outputs
• Statement
Statement end with ; in programming.
example =>
let a = 10;
let value = document.getElementById(‘textbox’).value;
Javascript Comment
/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
in my web page:
*/
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";
Note – In visual studio code, you can comment the code with this shortcut (Ctrl + /).
Computer Memory
• Temporary Memory
All the data on the memory has lost when the electrictiy is off or the computer is
switched off.
eg - RAM (Random Access Memory)
• Permanent Memory
All the data on the memory retain when the electrictiy is off or the computer is
switched off.
eg - CD, Memory Stick, Hard Disk, SSD
Extensions
It a small program or feature that help the main software.
Extensions support the additional feature to the main software.
Variable
Variable Declaration
let a; //declaration
let b; //declaration
Variable Assignment
a = 10; //assignment
b = 20; //assignment
let a = 100;
Left Value Assign Operator Right Value
Note - Assign operator assign the right value to the left value;
Data Types
Primitive Data Types Object Data types
Eg –
// array declaration and assignment
let arr = [1,2,3,100,200,300];
Convert To Computerise
Object is object.
• Properties => Variable
• Behaviors => Function
Disadvantage Disadvantage
• not symmetric • To be symmetric, need to create template class
and need to create object from template class.
Object Example
Building Direct Object Building Template Class
class Person{
let mgmg = { name;
name: "mg mg", age;
age: 15, address;
profession: 'student', study() {
console.log('studying');
study: function(){ }
console.log('Studying'); eat() {
} console.log('Eat');
} }
}
Building Object From Template Class
(1)Addition
Integer + Integer
(2)Concatination
Integer + String
String + Integer
String + String
Operators (1)
Arithmetic Operators
Operators (2)
Assignment Operators
Operators (3)
Comparison Operators
Operators (4)
Logical Operators
Function
• Function ကလ
ို ပ
ို ်ငန််းစဉ်လခ
ို ဲ့ ေါ်တယ်။ မမလိုပ် ျင်ခ ော လိုပ်ငန််းစဉ်ခတွေကို Function ထမော ောွေ ်းခရ်းရပ မယ်။
Example
//function declaration
function a(){
console.log(“my first function”);
}
//function call
a();
Predefined & User Defined Function
Predefined Function
အလွေယတ် ကူ အဆင် င ်ဲ့ ခ ေါ်ယအ
ူ ်းို ပပြုနင
ို ခ
် အောင် ကကြုတင် တ်မတ်ထော်းခ ော Function မျော်း ပြစ် ည်။
Example –
alert(‘my first predefined function’);
console.log(‘my first predefined function’);
//function call
addTwoNumber();
Function Argument & Parameter
Function တွေင ်
• No Argument Function
• Argument Function ဟူ၍ နစ်မျြု်းရ ည်။
No Argument Function
function addTwoNumber(){ No Argument ()
let num1 = 10;
let num2 = 20;
console.log(num1+num2);
}
addTwoNumber(); //result is 30
Argument Function
function addTwoNumber(num1,num2){ Two Arguments (num1,num2)
console.log(num1+num2);
}
addTwoNumber(50,100); //result is 150 Two Parameters (50,100)
Return & No Return Function
No Return Function
No Return Function မျော်း ည် Function အတွေင်း် ော လိုပ်ငန််းစဉ် ပပ်းခပမောက် ောွေ ်းကက ည်။ တစ်ပ ော်းခ ော အရောမျော်းနင ်ဲ့
တွေြက်လပ
ို ်ခဆောင်၍ မရ။
function addingTwoNumber(num1,num2){
console.log(num1+num2);
}
addingTwoNumber(20,40); // 60
addingTwoNumber(10,30); // 40
Return Function
Return Function မျော်း ည် တန်ြို်းတစ် ိုကို return(ပပန်ထတ ို )် ပပန် ည်။
ထို function ည် return ပပန်ခ ော တန်ြို်းရ ည်။
next process မျော်းကို ဆက်လပ
ို ်ရန် တစ်ပ ော်းခ ော အရောမျော်းနင် တွေြက်လပ ို ်ခဆောင်ရောတွေင ် ခရ်းခလဲ့ခရထရ ည်။
function addingTwoNumber(num1,num2){
return num1+num2;
}
Event Handler
Event Handler မျော်း ည် function မျော်းပြစ်ကကပပ်း Event မျော်းနင် တွေလျက်ရကက ည်။ Event ပြစ်တောနင်ဲ့
အလိုပ်လပ
ို ်ခ ော function မျော်းကို Event Handler ဟိုခ ေါ် ည်။
Click Event
<html>
<head>
<title>Event And Event Handler</title>
</head> Event Handler => showAlert()
<body>
<button onclick=“showAlert()" >alert</button>
<script>
function showAlert(){
alert('my first event and event handler');
}
</script>
</body>
</html>
Structure Programming (1)
Structure Programming
• Sequence Structure (အစဉ်အတငို ်း် အလိုပ်လပ
ို ်တဲ့ structure)
• Conditional Structure (အခပ အခနခပေါ်မတ ူ ည်ပပ်း အလိုပ်လပ ို ်တဲ့ structure)
• Iteration Structure (ထပ်တစ်လလလိုပ်မဲ့ structure)
Sequence Structure
Program တင ို ်း် ဟော Sequence အတင
ို ်း် Top to Bottom အလိုပ်လပ
ို ်ပ တယ်။
Note - Sequence Structure မော်းယွေင်း် ခ ောအ မန်ကန်ခ ော result မထွေကတ ် ောမျြု်း ( )ို ဲ့ Error
ပြစ်တောမျြု်းကကြုခတွေွေ့ရတက်ပ တယ်။
Conditional Structure
(i) if;
(ii) if else;
(iii) if elseif;
(iv) if elseif elseif;
(v) if elseif else;
(vi) if elseif elseif else;
Note - if ခတွေွေ့တင
ို ်း် condition င်စစ်မယ်
elseif က အခပေါ်က if န ဲ့ else if ခတွေအော်းလ်းို false ပြစ်မ condition င်စစ်မယ်
else က အခပေါ်က if န ဲ့ else if ခတွေအော်းလ်းို fasle ပြစ်မ အလိုပ်လပ
ို ်မယ်
- if န ဲ့ else if မော condition ပ တယ်။ else မပ ဘူ်း။
Structure Programming (2)
Iteration Structure (Or) Looping Structure
• for
• foreach
• forin
• forof
• while
• do while
Note - ကယ
ို လ
် ပ
ို ် ျင်တဲ့ process ကို အကကမ် အခရအတွေက် အမျော်းကက်း လိုပ် ျင်တအ
ဲ့ အ ်းို ပပြုပ တယ်
Loop Explanation
String Functions (1)
length (စာလုံးို အရေအတွကက
် ို သချင်ေင်သုံးို )
let txt = “apple”;
Console.log(txt.length); //5
slice() (စာထဲမှ ကယ ို လ
် ခ
ို ျင်တဲ့ဲ စာကို string position နဲ ဲ့ ဖြတ်ပ ုံး ယူတာ)
//အခရ ွေ့ကခနယူတပ
ဲ့ စ
ို
let str = “Apple, Banana, Kiwi”;
let part = str.slice(7,13); //Banana
//အခနောက်ကခနယူတပ ဲ့ စ
ို
let str = “Apple, Banana, Kiwi”;
let part = str.slice(-12,-6); //Banana
//pass ခပ်းလက
ို တ
် ဲ့ string position ကခနစပပ စောခနောက်ဆ်းို ထယူတပ
ဲ့ စ
ို (positive number)
let str = “Apple, Banana, Kiwi”;
let part = str.slice(7); //Banana, Kiwi
//pass ခပ်းလက
ို တ
် ဲ့ string position ကခနစပပ စောခနောက်ဆ်းို ထယူတပ
ဲ့ စ
ို (negative number)
let str = “Apple, Banana, Kiwi”;
let part = str.slice(-12); //Banana, Kiwi
String Functions (2)
substring() (စာထဲမှ ကယ ို လ
် ခ
ို ျင်တဲ့ဲ စာကို string position နဲ ဲ့ ဖြတ်ပ ုံး ယူတာ)
ခအောက်ပ Link တွေင ် ခလဲ့လောရန်
https://www.w3schools.com/js/js_string_methods.asp
charAt() (ထညဲ့ လ
် က
ို တ
် ဲ့ဲ index မှာ ရှတဲဲ့ character ကလ
ို ခ
ို ျင်ေင်သုံးို )
let text = "HELLO WORLD";
let char = text.charAt(0); // H
String Functions (4)
charCodeAt() (ထညဲ့ လ
် က
ို တ
် ဲ့ဲ index မှာ ရှတဲဲ့ character code ကလ
ို ခ
ို ျင်ေင်သုံးို )
let text = "HELLO WORLD";
let char = text.charCodeAt(0); // 72
indexOf() ( ထမဆုံးို ရတွွေ့တဲ့ဲ စာသာုံးေဲ ဲ့ position ၏ index ကို return ဖ န်ရ ုံးတယ်, စရှာရြွရသာ position မှ
စရှာသည်)
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15); //21
lastIndexOf() (ရနာက်ဆုံးို ရတွွေ့တဲ့ဲ စာသာုံးေဲ ဲ့ position ၏ index ကို return ဖ န်ရ ုံးတယ်, စရှာရြွရသာ position မှ
စရှာသည်)
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate"); //21
indexOf() (ရနာက်ဆုံးို ရတွွေ့တဲ့ဲ စာသာုံးေဲ ဲ့ position ၏ index ကို return ဖ န်ရ ုံးတယ် , စရှာရြွရသာ position မှ
စရှာသည်)
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate“,15); //21
Note – indexOf() and lastIndexOf() function သည် ရှာမရတွွေ့လျှင် (-1) return ဖ န်သည်
String Search (2)
search() ( ထမဆုံးို ရတွွေ့တဲ့ဲ စာသာုံးေဲ ဲ့ position ၏ index ကို return ဖ န်ရ ုံးတယ်)
let str = "Please locate where 'locate' occurs!";
str.search("locate"); // 7
match()
(/g (global) ည် regular expression ပြစ် ည် ain နင ်ဲ့ တူညခ ော အရောမျော်းကို ရောခြွေပပ်း ရ ခလောက် array
တစ် ိုအခနန ဲ့ return ပပန် ည်။)
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g); // ['ain','ain','ain’]
(/gi (global and case-insensitive) ည် regular expression ပြစ် ည် ain နင ်ဲ့ တူညခ ော အရောမျော်းကို
ရောခြွေပပ်း ရ ခလောက် array တစ် ိုအခနန ဲ့ return ပပန် ည်။)
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi); // ['ain’,’AIN’,'ain','ain’]
includes() (သတ်မှတလ် က
ို ရ
် သာ စာသာုံးသည် ရှာချင်ရသာ စာသာုံးထဲတင
ွ ရ
် ှလျှင် true return ဖ န်သည်)
let text = "Hello world, welcome to the universe.";
text.includes("world"); // true
String Templates
String Template မျာုံးကို ရေုံးသာုံးောတွင ် double quote ("") အစာုံး back-ticks(``) ကို အသုံးို မျာုံးသည်
let text = `Hello World!`;
Numeric Strings
let x = 100; // x is a number
let y = "100"; // y is a string
isNaN()
let x = 100 / "Apple";
isNaN(x); // true
Number Functions (1)
toString() (Number to String ရဖ ာင်ုံးေင်သုံးို တယ်)
let x = 123;
x.toString();
toPrecision() (သတ်မှတလ် က
ို တ
် ဲ့ဲ length အတင
ို ုံး် ထွကတ
် ယ်)
let x = 9.656;
x.toPrecision(); // 9.656
x.toPrecision(2); // 9.7
x.toPrecision(4); // 9.656
Number Functions (2)
Number() (Number ရဖ ာင်ုံးေင်သုံးို တယ်)
Number(true); //1
Number(false); //0
Number("10"); //10
Number(" 10"); //10
Number("10 "); //10
Number("10.33"); //10.33
Number("10,33"); //NaN
Number("10 33"); //NaN
Number("John"); //NaN
Number(new Date("1970-01-02")) //86400000
Array Length
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.length); // 4
Array Function (3)
splice() (Array ထဲက value အသစ်ရတွကို ကယ ို ထ
် ညဲ့ ခ
် ျင်တရ
ဲ့ဲ နောက ထညဲ့ ခ
် ျင်ေင် သုံးို )
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let removed = fruits.splice(2, 2, "Lemon", "Kiwi");
// original fruits array => ["Banana", "Orange", "Apple", "Mango"];
// fruits array after splice => ["Banana", "Orange“ , “Lemon", “Kiwi", "Apple", "Mango"];
// removed array => [“Apple", “Mango“]
map()
map() function သည် အလို ် လို ် ပ ုံးတာနဲတာ
ဲ့ new array တစ်ခို ကို return ဖ န်တယ်။
map() function သည် original array ေဲ ဲ့ တန်ြိုုံးကို မရဖ ာင်ုံးလဲ ေါ။
map() function သည် empty array တွင ် အလို ် မလို ် ေါ
const numbers1 = [45, 4, 9, 16, 25];
const numbers2 = numbers1.map(
(value,index,array)=>{
return value * 2;
}
);
// [90, 8, 18, 32, 50]
Array Iteration (2)
filter() (array ထဲက value ရတွကို စစ်ထတ
ို ခ
် ျင်ေင်သုံးို )
const numbers = [45, 4, 9, 16, 25];
const over18 = numbers.filter(
(value,index,array)=>{
return value > 18
}
);
// [45,25]
new Date(dateString)
const d = new Date("October 13, 2014 11:13:00");
//Mon Oct 13 2014 11:13:00 GMT+0630 (Myanmar Time)
new Date(miliseconds)
const d = new Date(0);
//Thu Jan 01 1970 06:30:00 GMT+0630 (Myanmar Time)
getFullYear()
const d = new Date();
d.getFullYear(); // 2022
getMonth()
const d = new Date();
d.getMonth(); // 3
getDate()
const d = new Date();
d.getDate(); // today date (example - 17)
getHours()
const d = new Date();
d.getHours(); // current hour (example - 19 = 7:00PM)
JS Date Get Methods (2)
getMinutes()
const d = new Date();
d.getMinutes(); // current minutes (example -12)
getSeconds()
const d = new Date();
d.getSeconds(); // current seconds (example – 1)
getMilliseconds()
const d = new Date();
d.getMilliseconds(); // current milliseconds (example - 683)
getDay()
const d = new Date();
d.getDay(); // weekday as a number (0-6)
JS Date Set Methods (1)
setFullYear()
const d = new Date();
d.setFullYear(2020); // Tue Mar 17 2020 19:46:11 GMT+0630 (Myanmar Time)
setMonth()
const d = new Date();
d.setMonth(11); // Sat Dec 17 2022 19:47:38 GMT+0630 (Myanmar Time)
setDate()
const d = new Date();
d.setDate(15); // Tue Mar 15 2022 19:48:01 GMT+0630 (Myanmar Time)
setHours()
const d = new Date();
d.setHours(22); // Thu Mar 17 2022 22:49:33 GMT+0630 (Myanmar Time)
JS Date Set Methods (2)
setMinutes()
const d = new Date();
d.setMinutes(30); // Thu Mar 17 2022 19:30:09 GMT+0630 (Myanmar Time)
setSeconds()
const d = new Date();
d.setSeconds(30); // Thu Mar 17 2022 19:51:30 GMT+0630 (Myanmar Time)
getMilliseconds()
const d = new Date();
d.getMilliseconds(); // current milliseconds (example - 683)
getDay()
const d = new Date();
d.getDay(); // weekday as a number (0-6)
Math
Math.PI; // 3.14
Math.round(4.6); // 5 (Returns x rounded to its nearest integer)
Math.ceil(4.3); // 5 (Returns x rounded up to its nearest integer)
Math.floor(4.7); // 4 (Returns x rounded down to its nearest integer)
Math.trunc(3.7) // 4 (Returns the integer part of x)
Math.sign(-4); // -1 (returns if x is negative, return -1 or if x is positive, return 1)
Math.pow(8,2) // 64 (returns the value of x to the power of y)
Math.sqrt(64); // 8 (returns the square root of x)
Math.abs(-4.7); // 4 ( returns the absolute (positive) value of x:)
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min(0, 150, 30, 20, -8, -200); // -200
Math.max(0, 150, 30, 20, -8, -200); // 150
Math.random(); // returns a random number between 0 (inclusive), and 1 (exclusive):