Last active
November 4, 2016 22:42
-
-
Save dimMaryanto93/b956151354fb8a1454d3b36d22d61a63 to your computer and use it in GitHub Desktop.
JavaScript fundamental
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bil1 = 10; | |
var bil2 = 20; | |
console.log("bil1 : " + bil1 + "; bil2 : " + bil2 + ";\n \n"); | |
var result = null; | |
result = bil1 + bil2; | |
console.log("bil1 + bil2 : " + result); | |
result = bil1 * bil2; | |
console.log("bil1 x bil2 : " + result); | |
result = bil1 / bil2; | |
console.log("bil1 / bil2 : " + result); | |
result = bil1 - bil2; | |
console.log("bil1 - bil2 : " + result); | |
result = Math.sqrt(bil1); | |
console.log("Math.sqrt(bil1) : " + result); | |
bil1 += 1; | |
console.log("bil1+= 1 : " + bil1); | |
bil2 -= 2; | |
console.log("bil2-= 2 : "+ bil2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var values = new Array(10, 100, 50, 28); | |
console.log(values); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var daftarBuah = ["mangga", "aple", "jeruk", "sirsak"]; | |
console.log("Jumlah buah yang ada adalah " + daftarBuah.length + " buah"); | |
for (var i = 0; i < daftarBuah.length; i++) { | |
console.log("buah yang tersedia " + daftarBuah[i]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var laptops = ["Acer", "Asus", "MSI", "Lenovo"]; | |
for (kom in laptops) { | |
console.log(kom + ". " + laptops[kom]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numbers = []; | |
console.log("jumlah array sekarang : " + numbers.length); | |
for (var i = 0; i < 10; i++) { | |
numbers.push(i + 1); | |
} | |
console.log("jumlah aray setelah push : "+ numbers.length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str = "Dimas Maryanto"; | |
console.log("Nama saya adalah " + str); | |
var fullName = "Dimas" + "Maryanto"; | |
console.log("Full Name is " + fullName); | |
var strConcat = "+62" + 82117355133; | |
console.log("the phone number is " + strConcat + ", and data type is " + typeof (strConcat)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i = 0; | |
do { | |
i++; | |
console.log("Pesan ke " + i); | |
} while (i < 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (var i = 0; i < 10; i++) { | |
console.log("Halo ini pesan ke " + (i + 1)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function perkalian(bil1, bil2) { | |
return bil1 * bil2; | |
} | |
var sepuluh = 10; | |
var sembilan = 9; | |
console.log("perkalian antara sepuluh dikali sembilan adalah " + perkalian(sepuluh, sembilan)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function halo(){ | |
console.log("Halo ini function dari JavaScript"); | |
} | |
// panggil function halo(); | |
halo(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Mahasiswa() { | |
function quis() { | |
return 100; | |
} | |
function uts(nilai) { | |
return nilai; | |
} | |
function uas(uts, uas) { | |
return (uts + uas) / 2; | |
} | |
var nilaiUTS = uts(80); | |
return uas(nilaiUTS, 60); | |
} | |
console.log("Nilai akhir mahasiswa tersebut adalah "+ Mahasiswa()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var global = "Dimas Maryanto"; | |
function halo() { | |
console.log("diakses di dalam method halo()" + global); | |
} | |
halo(); | |
console.log("diakses di luar method halo()" + global); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bilangan = 10; | |
if(bilangan == 10){ | |
console.log("variable bilangan sama dengan sepuluh"); | |
}else{ | |
console.log("variable bilangan tidak sama dengan sepuluh!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bilangan = "10"; | |
if (bilangan === 10) { | |
console.log("triple equals"); | |
} else if (bilangan == 10) { | |
console.log("double equals"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var kata = "Sepuluh"; | |
if (kata == 10) { | |
console.log("Kata sama dengan 10"); | |
} else if (kata == "10") { | |
console.log("Kata sama dengan '10'"); | |
} else if (kata == "Sepuluh") { | |
console.log("Kata sama dengan Sepuluh"); | |
} else { | |
console.log("Kata lainnya"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bil; | |
if (!bil) { | |
console.log("bil tidak memilik nilai"); | |
} else { | |
console.log("bil memiliki nilai"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mahasiswa = { | |
nim: "10511148", | |
nama: "Dimas Maryanto", | |
semester: 8, | |
cuti: false | |
}; | |
console.log("Mahasiswa dengan nim " + mahasiswa.nim + " adalah " + mahasiswa.nama); | |
console.log("Tipe dari variable mahasiswa adalah " + typeof (mahasiswa)); | |
console.log(mahasiswa); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function halo(){ | |
var local = "Dimas Maryanto"; | |
console.log(local); | |
} | |
halo(); | |
// akan mengembalikan nilai undefined | |
// console.log(local); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mahasiswa = { | |
"nim": 10511148, | |
"nama" : "Dimas Maryanto", | |
"kelas" : { | |
"id" : "si05", | |
"semester" : 07, | |
"jurusan" : { | |
"id": 105, | |
"nama" : "Sistem Informasi", | |
"alias" : "SI" | |
} | |
} | |
}; | |
console.log(mahasiswa.kelas.jurusan.nama); | |
console.log(mahasiswa['kelas']['jurusan']['alias']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mahasiswa = { | |
"nim": 10511148, | |
"nama": "Dimas Maryanto", | |
"getMahasiswa": function () { | |
return mahasiswa.nim + " " + mahasiswa.nama; | |
} | |
} | |
console.log(mahasiswa.getMahasiswa()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mahasiswa = { | |
"nim": 10511148, | |
"nama": "Dimas Maryanto", | |
"kelas": { | |
"id": "si05", | |
"nama": "Dimas Maryanto", | |
"getKelas": function () { | |
return this.id + " " + this.nama; | |
} | |
} | |
} | |
var str = "Nim : " + | |
mahasiswa.nim + " namanya adalah " + | |
mahasiswa.nama + " terdapat di kelas " + | |
mahasiswa.kelas['getKelas'](); | |
console.log(str); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bil = 10; | |
console.log((bil != 10 ? "Benar": "Salah!")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var grade = "B"; | |
switch (grade) { | |
case "A": console.log("Selamat anda lulus dengan predikat terpuji"); | |
break; | |
case "B": console.log("Selamat anda lulus dengan predikat Sangat Memuaskan"); | |
break; | |
case "C": console.log("Selamat anda lulus dengan predikat Memuaskan"); | |
break; | |
case "D": console.log("Selamat anda lulus dengan perikat Cukup"); | |
break; | |
default : console.log("Anda dinyatakan tidak lulus!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var log = "Dimas Maryanto"; // variable bertipe string | |
console.log(log); | |
log = 12; // variable bertipe number | |
console.log(log); | |
log = false; // variable bertipe boolean | |
console.log(log); | |
log = new Date(); // variable bertipe Date(); | |
console.log(log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var log = "Dimas Maryanto"; | |
console.log(log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i = 0; | |
while(i < 10){ | |
i++; | |
console.log("loop ke "+ i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment