// AND, OR, NOTlet result1 =true&&false; // falselet result2 =true||false; // truelet result3 =!true; // false// Short-circuit evaluationlet a =null;let b = a ||"default"; // "default"
2.3.4 Assignment Operators
let x =5;x +=3; // x = x + 3x -=2; // x = x - 2x *=4; // x = x * 4x /=2; // x = x / 2x %=3; // x = x % 3
2.4 Struktur Kontrol
2.4.1 If Statement
let age =18;if (age >=18) {console.log("Anda sudah dewasa");} elseif (age >=13) {console.log("Anda remaja");} else {console.log("Anda masih anak-anak");}// Ternary operatorlet status = age >=18?"dewasa":"belum dewasa";
2.4.2 Switch Statement
let day ="Monday";switch (day) {case"Monday":console.log("Hari kerja");break;case"Saturday":case"Sunday":console.log("Akhir pekan");break;default:console.log("Hari kerja biasa");}
2.5 Loop
2.5.1 For Loop
// Standard for loopfor (let i =0; i <5; i++) {console.log(i);}// For...of (untuk iterables)let array = [1,2,3];for (let item of array) {console.log(item);}// For...in (untuk object properties)let object = { a:1, b:2 };for (let key in object) {console.log(key, object[key]);}
2.5.2 While dan Do-While Loop
// While looplet count =0;while (count <5) {console.log(count); count++;}// Do-while looplet x =0;do {console.log(x); x++;} while (x <3);
2.6 Function
2.6.1 Function Declaration
// Basic functionfunctiongreet(name) {return`Hello, ${name}!`;}// Function with default parametersfunctionmultiply(a, b =1) {return a * b;}// Arrow function (ES6+)constadd= (a, b) => a + b;// Function expressionconstsubtract=function(a, b) {return a - b;};