ใช้เมธอด toLowerCase() แปลงสตริงเป็นตัวพิมพ์เล็กทั้งหมด คืนค่า string ใหม่ (ไม่เปลี่ยน string เดิม)
ตัวอย่าง
<html>
<head>
<title>JavaScript String toLowerCase() Method</title>
</head>
<body>
<script>
var str = "DEMO TEXT";
document.write(str.toLowerCase()); // "demo text"
</script>
</body>
</html>
เมธอดเกี่ยวกับ Case
| Method | คำอธิบาย | ตัวอย่าง |
|---|---|---|
toLowerCase() | แปลงเป็นตัวพิมพ์เล็ก | "HELLO" → "hello" |
toUpperCase() | แปลงเป็นตัวพิมพ์ใหญ่ | "hello" → "HELLO" |
toLocaleLowerCase() | แปลงตาม locale (ภาษา) | TR: "İ" → "i" (not "ı") |
toLocaleUpperCase() | แปลงตาม locale | TR: "i" → "İ" (not "I") |
การใช้งานทั่วไป
// Case-insensitive comparison
if (userInput.toLowerCase() === "yes") { ... }
// Normalize for storage
var email = userInput.toLowerCase().trim();
// CSS alternative (visual only)
.element { text-transform: lowercase; }
หมายเหตุ: ไม่เปลี่ยนสตริงต้นฉบับ (strings are immutable) ต้องเก็บค่าที่คืนมา