Skip to content
Grok

if else

ตรวจสอบเงื่อนไข - ทำงานในบล็อก if เมื่อเงื่อนไขเป็นจริง, else เมื่อเป็นเท็จ

js
if (age >= 18) {
  console.log("You can vote");
} else {
  console.log("You cannot vote yet");
}

switch

ตรวจสอบหลายเงื่อนไข เหมาะกับการเปรียบเทียบค่าตัวแปรเดียวกับหลายค่า

js
switch (fruit) {
  case "apple":
    console.log("Red fruit");
    break;
  case "banana":
    console.log("Yellow fruit");
    break;
  default:
    console.log("Other fruit");
}

ternary operator

เงื่อนไขแบบสั้นๆ รูปแบบ เงื่อนไข ? ค่าเมื่อจริง : ค่าเมื่อเท็จ

js
let status = age >= 18 ? "adult" : "child";

logical operators

รวมเงื่อนไขด้วย && (AND), || (OR), ! (NOT)

js
if (username && password) {
  console.log("Ready to log in");
}

nullish coalescing operator

กำหนดค่าเริ่มต้นเมื่อตัวแปรเป็น null/undefined ด้วย ??

js
let displayName = username ?? "Guest user";