Javascript 1: Nota Asas Javascript
"nixk".length
// PAPARAN: 4
4+3
// PAPARAN: 7
6/2
// PAPARAN: 3
// This is a comment that the computer will ignore.
// It is for your eyes only!
"cake".length
// Ini adalah komen dan komputer akan ignore baris ini.
// PAPARAN: 4
confirm('This is an example of using JS to create some interaction on a website. Click OK to continue!');
// PAPARAN: Dialog confirm
confirm ("I feel awesome!");
confirm ("I'm ready to go.");
// PAPARAN: Ada 2 Dialog confirm
prompt ("Where are you come from?");
// PAPARAN: Ada 1 dialog pertanyaan, bila dijawab Kelantan, result akan papar Kelantan
alert('Hello nixk, welcome to Code Avengers')
// PAPARAN: Keluar dialog alert
Data Types I & II: Numbers & Strings
Data mempunyai pelbagai jenis. Kita baru menggunakan dua iaitu number dan strings.
a. Number: Kuantiti dan boleh lakukan math.
b. Strings: adalah aksara a-z,space dan mungkin juga number, biasanya string digunakan sebagai label, nama dan isi didalam program kita
c. Jika ingin menjadikan number sebagai number dan bukan string, taip tanpa quotes ". Contoh: 190.12334.
d. JIka ingin menjadikan string, gunakan quotes ".
Data Type III: Booleans
Booleans digunakan untuk membandingkan dua perkara untuk mendapat keputusan samada true atau false.
23 > 10 is true
5 < 4 is false
"I'm coding like a champ".length >10
// PAPARAN: true
Console.log
console.log( 2*5)
console.log( "Hello")
// PAPARAN:
10
Hello
Penggunaan console log adalah untuk mengetahui apa sebenarnya computer sedang lakukan. Juga dikenali sebagai printing out.
// Here is an example of using the greater than (>) operator.
console.log(15 > 4); // 15 > 4 evaluates to true, so true is printed.
// Fill in with >, <, === so that the following print out true:
console.log("Xiao Hui".length < 122);
console.log("Goody Donaldson".length > 8);
console.log(8*2 == 16);
// PAPARAN:
true
true
true
true
if (10 >=7 ) {
console.log("You have a long name!" );
}
// PAPARAN:You have a long name!
if ("left".length >10 )
{
console.log("Let's go down the first road!");
}
else
{
// What should we do if the condition is false? Fill in here:
console.log("Let's go down the second road!");
}
// PAPARAN: Let's go down the second road!
