JavaScript
String Methods
endsWith
เรียนรู้การใช้ endsWith() ตรวจสอบว่า string ลงท้ายด้วยข้อความที่กำหนดหรือไม่ พร้อมเข้าใจ case-sensitivity, length parameter และการใช้งานในสถานการณ์จริง
endsWith() — ตรวจสอบการลงท้ายของ string
endsWith() คือ method สำหรับตรวจสอบว่า string ลงท้ายด้วย substring (ข้อความย่อย) ที่เรากำหนดหรือไม่ syntax: str.endsWith(searchString, length?) endsWith() จะคืนค่าเป็น boolean: true ถ้า string ลงท้ายด้วย substring นั้น, false ถ้าไม่ใช่ บอกแค่ว่า "ลงท้ายด้วยสิ่งนี้หรือไม่" — ไม่ได้บอกตำแหน่ง, ไม่ได้บอกจำนวนครั้ง และเหมือนกับ string method ทุกตัว — endsWith() จะไม่เปลี่ยน string เดิม (string เป็น immutable) แต่คืนค่า boolean เป็นผลลัพธ์
endsWith() คืนค่า true หรือ false เท่านั้น
let text = "JavaScript";
console.log(text.endsWith("Script")); // true — "JavaScript" ลงท้ายด้วย "Script"
console.log(text.endsWith("Java")); // false — "JavaScript" ไม่ได้ลงท้ายด้วย "Java"
console.log(text.endsWith("script")); // false — ตัวพิมพ์เล็ก-ใหญ่ต่างกัน
console.log(text.endsWith("")); // true — string ว่าง "อยู่" ท้ายทุก string
console.log(text); // "JavaScript" — ตัวเดิมไม่เปลี่ยน- endsWith() คืนค่า boolean — true ถ้าลงท้ายด้วย substring นั้น, false ถ้าไม่ใช่
- รับ parameter ได้ 2 ตัว: searchString (จำเป็น) และ length (ไม่จำเป็น) — ใช้กำหนดความยาวของ string ที่ต้องการตรวจสอบ
- string เดิมไม่ถูกเปลี่ยนแปลง — immutable — endsWith() คืนค่า boolean ไม่ใช่ string ใหม่
- searchString เป็น string ว่าง ("") จะคืนค่า true เสมอ — เพราะ "ไม่มีอะไร" อยู่ท้ายทุก string ในทางเทคนิค
Case-Sensitivity — endsWith() คำนึงถึงตัวพิมพ์เล็ก-ใหญ่
endsWith() แยกแยะตัวพิมพ์เล็ก (a-z) และตัวพิมพ์ใหญ่ (A-Z) — กล่าวคือมันเป็น case-sensitive "Hello".endsWith("hello") จะคืนค่า false เพราะ "H" ตัวใหญ่ ไม่ตรงกับ "h" ตัวเล็ก — แม้ว่าจะเป็นคำเดียวกันก็ตาม ถ้าต้องการตรวจสอบแบบไม่สนใจ case (case-insensitive) — ให้แปลงทั้ง string ตั้งต้นและ searchString ให้เป็น case เดียวกันก่อนเรียก endsWith() โดยใช้ toLowerCase() หรือ toUpperCase()
ใช้ toLowerCase() เพื่อทำ case-insensitive check
let filename = "Annual-Report.PDF";
// --- แบบ case-sensitive (ปกติ) ---
console.log(filename.endsWith(".pdf")); // false — "PDF" ตัวใหญ่ ไม่ตรงกับ "pdf" ตัวเล็ก
console.log(filename.endsWith(".PDF")); // true — ตรง case
// --- แบบ case-insensitive (แปลงเป็น lowercase ก่อน) ---
console.log(filename.toLowerCase().endsWith(".pdf")); // true — "annual-report.pdf" ลงท้ายด้วย ".pdf"
// ตัวแปรต้นฉบับไม่เปลี่ยน
console.log(filename); // "Annual-Report.PDF"- endsWith() เป็น case-sensitive — "A" กับ "a" ถือว่าไม่เหมือนกัน
- วิธีทำ case-insensitive check: แปลงทั้งสองด้านเป็น case เดียวกันก่อนด้วย toLowerCase() หรือ toUpperCase()
- รูปแบบที่ใช้บ่อย: str.toLowerCase().endsWith(search.toLowerCase())
length Parameter — กำหนดขอบเขตของ string ที่ต้องการตรวจสอบ
endsWith() รับ parameter ตัวที่สองคือ length — ระบุความยาวของ string ที่ต้องการตรวจสอบ (default = str.length คือตรวจสอบทั้ง string) syntax: str.endsWith(searchString, length) เมื่อระบุ length — endsWith() จะทำเหมือน string มีความยาวเท่ากับ length ตัวอักษรแรกเท่านั้น ตัวอักษรที่เกิน length จะถูกตัดทิ้งก่อนตรวจสอบ กฎสำคัญ: - ถ้าไม่ระบุ length — ตรวจสอบทั้ง string (default = str.length) - ถ้า length > str.length — จะถูกปรับเป็น str.length (ตรวจสอบทั้ง string) - length = 0 — คืนค่า true เสมอ (เพราะ string ว่างลงท้ายด้วยทุกสิ่ง) - มีประโยชน์เมื่อต้องการตรวจสอบว่า string ในช่วงที่กำหนดลงท้ายด้วยข้อความอะไร — เช่น ตรวจสอบ prefix หรือ substring ก่อนตำแหน่งที่รู้
length กำหนดว่าต้องการมอง string ยาวเท่าไหร่ก่อนตรวจสอบ
let text = "hello world!";
// ไม่ระบุ length — ตรวจสอบทั้ง string (length = 12)
console.log(text.endsWith("world!")); // true — "hello world!" ลงท้ายด้วย "world!"
// length = 5 — มองแค่ "hello" (5 ตัวแรก)
console.log(text.endsWith("hello", 5)); // true — "hello" ลงท้ายด้วย "hello"
console.log(text.endsWith("world", 5)); // false — "hello" ไม่ได้ลงท้ายด้วย "world"
// length = 11 — มองแค่ "hello world" (11 ตัวแรก)
console.log(text.endsWith("world", 11)); // true — "hello world" ลงท้ายด้วย "world"
console.log(text.endsWith("hello", 11)); // false — "hello world" ไม่ได้ลงท้ายด้วย "hello"
// length = 0 — string ว่าง
console.log(text.endsWith("hello", 0)); // true — "" ลงท้ายด้วยทุกสิ่ง
// length > str.length — ปรับเป็น str.length
console.log(text.endsWith("world!", 100)); // true — 100 ถูกปรับเป็น 12, "hello world!" ลงท้ายด้วย "world!"
console.log(text); // "hello world!" — ไม่เปลี่ยน- length = ไม่ระบุ: ตรวจสอบทั้ง string (default = str.length)
- length ระบุ: มอง string แค่ length ตัวแรก — ตัวอักษรที่เกินมาถูกตัดทิ้งก่อนตรวจสอบ
- length > str.length: ถูกปรับเป็น str.length — ตรวจสอบทั้ง string
- length = 0: คืนค่า true เสมอ — เพราะ string ว่างลงท้ายด้วยทุกสิ่ง
- ใช้ length parameter เมื่อต้องการตรวจสอบการลงท้ายภายในช่วงที่กำหนด — เช่น ตรวจสอบ prefix โดยไม่สนใจส่วนที่เหลือ
กรณีพิเศษ — empty string และ edge cases
endsWith() มีพฤติกรรมพิเศษบางอย่างที่ควรรู้ — เพื่อไม่ให้แปลกใจเมื่อเจอในโค้ดจริง: 1) searchString เป็น string ว่าง ("") — คืนค่า true เสมอ เพราะ "ไม่มีอะไร" ถือว่าอยู่ท้ายทุก string ในทางเทคนิค 2) length = 0 — คืนค่า true เสมอ — เพราะ string ความยาว 0 (string ว่าง) ลงท้ายด้วยทุกสิ่ง 3) length > str.length — ถูกปรับเป็น str.length โดยอัตโนมัติ — ไม่เกิด error 4) length เป็นค่าติดลบ — จะถูกปรับเป็น 0 — ทำให้คืนค่า true เสมอ (เพราะ string ว่างลงท้ายด้วยทุกสิ่ง) 5) endsWith() ไม่รองรับ regular expression (RegExp) — ถ้าส่ง regex เข้าไป จะเกิด TypeError — ใช้ได้เฉพาะ string ธรรมดาเท่านั้น
ทดสอบ edge case ต่าง ๆ ของ endsWith()
let text = "Hello";
// --- 1) empty string ---
console.log(text.endsWith("")); // true — มี "" อยู่ท้ายทุก string
// --- 2) length = 0 ---
console.log(text.endsWith("Hello", 0)); // true — string ความยาว 0 ลงท้ายด้วยทุกสิ่ง
// --- 3) length > str.length ---
console.log(text.endsWith("Hello", 100)); // true — 100 ถูกปรับเป็น 5
// --- 4) length ติดลบ ---
console.log(text.endsWith("Hello", -5)); // true — -5 ถูกปรับเป็น 0 → string ว่าง
// --- 5) endsWith() ใช้กับ regex ไม่ได้ ---
// text.endsWith(/lo/); // ❌ TypeError!
console.log(text.endsWith("lo")); // true — ใช้ string ปกติ- empty string ("") → true เสมอ — ต้องระวังถ้า searchString อาจเป็นค่าว่างจาก user input
- length = 0 → true เสมอ — ไม่มีข้อยกเว้น
- length > str.length → ถูกปรับเป็น str.length — ไม่เกิด error
- length ติดลบ → ถูกปรับเป็น 0 — ทำให้คืนค่า true เสมอ
- endsWith() ไม่รองรับ regex — ใช้ string ธรรมดาเท่านั้น — ถ้าต้องการค้นหาด้วย pattern ที่ซับซ้อน ต้องใช้เครื่องมืออื่นในบทต่อ ๆ ไป
การใช้งานจริง
endsWith() ใช้บ่อยมากในงานจริงเมื่อต้องการตรวจสอบรูปแบบการลงท้ายของ string — โดยเฉพาะการตรวจสอบนามสกุลไฟล์, ตรวจสอบ URL path, ตรวจสอบรูปแบบข้อมูล, หรือยืนยันว่า string ลงท้ายด้วยค่าที่คาดหวัง มาดูตัวอย่างการใช้งานที่พบบ่อย:
use case ที่พบบ่อยของ endsWith()
// 1) ตรวจสอบนามสกุลไฟล์
let filename = "document.pdf";
let isPdf = filename.endsWith(".pdf");
let isImage = filename.endsWith(".png") || filename.endsWith(".jpg");
console.log(isPdf); // true
console.log(isImage); // false
// 2) ตรวจสอบ URL path
let url = "https://example.com/api/users";
let isApiEndpoint = url.endsWith("/users");
console.log(isApiEndpoint); // true
// 3) ตรวจสอบรูปแบบข้อมูล
let phoneNumber = "081-234-5678";
let endsWithDigit = phoneNumber.endsWith("8");
console.log(endsWithDigit); // true
// 4) ตรวจสอบ email domain
let email = "user@example.com";
let isCompanyEmail = email.endsWith("@example.com");
console.log(isCompanyEmail); // true
// 5) ตรวจสอบวันที่ในชื่อไฟล์
let report = "sales-report-2024-03.pdf";
let isMarchReport = report.endsWith("-03.pdf");
console.log(isMarchReport); // true- ตรวจสอบนามสกุลไฟล์ — ใช้ endsWith('.pdf'), endsWith('.jpg') — ระวัง case-sensitivity
- ตรวจสอบ URL path — ใช้ endsWith() เพื่อรู้ว่า path ลงท้ายด้วย resource ที่ต้องการหรือไม่
- ตรวจสอบ email domain — ใช้ endsWith('@domain.com') เพื่อกรอง email ตามองค์กร
- ตรวจสอบรูปแบบข้อมูล — ใช้ endsWith() เพื่อยืนยันว่า string ลงท้ายด้วยรูปแบบที่คาดหวัง
- endsWith() ใช้คู่กับ startsWith() — ตรวจสอบทั้งต้นและท้ายของ string เพื่อความแม่นยำ
ข้อผิดพลาดที่พบบ่อย
มือใหม่มักเจอข้อผิดพลาดเหล่านี้เมื่อใช้ endsWith():
- ลืมว่า endsWith() เป็น case-sensitive — "File.PDF".endsWith(".pdf") → false — ต้องใช้ toLowerCase() ช่วยถ้าต้องการ case-insensitive
- สับสนระหว่าง includes() กับ endsWith() — includes() ตรวจสอบว่ามี substring อยู่ที่ไหนก็ได้ใน string, endsWith() ตรวจสอบเฉพาะตอนท้าย — "JavaScript".includes("Java") → true แต่ "JavaScript".endsWith("Java") → false
- ลืมว่า endsWith() ตรวจสอบทั้งคำ ไม่ใช่ตัวอักษร — "abcde".endsWith("cde") → true, "abcde".endsWith("e") → true, "abcde".endsWith("c") → false
- ลืมวงเล็บ () — endsWith เป็น method ต้องมี () ต่อท้ายเสมอ — str.endsWith("x") ไม่ใช่ str.endsWith"x"
- คาดหวังว่า endsWith() จะคืนค่าตำแหน่ง — endsWith() คืนค่า boolean (true/false) เท่านั้น ไม่ได้บอกว่าลงท้ายที่ตำแหน่งไหน
- ลืมว่า string ว่าง ("") คืนค่า true เสมอ — "Hello".endsWith("") → true — อาจทำให้เกิด bug ถ้าไม่ทันระวัง
ข้อผิดพลาดที่พบบ่อยและวิธีแก้ไข
let filename = "Report.PDF";
// ❌ ผิด: ลืมว่า case-sensitive
console.log(filename.endsWith(".pdf")); // false — ไม่ใช่สิ่งที่คิด!
// ✅ ถูก: ใช้ toLowerCase() เพื่อ case-insensitive
console.log(filename.toLowerCase().endsWith(".pdf")); // true
// ❌ ผิด: ใช้ includes() เมื่อต้องการตรวจสอบการลงท้าย
let url = "https://example.com/api";
console.log(url.includes("/api")); // true — แต่นี่แค่บอกว่า /api อยู่ใน URL ที่ไหนก็ได้
// ✅ ถูก: ใช้ endsWith() เมื่อต้องการตรวจสอบการลงท้าย
console.log(url.endsWith("/api")); // true — URL ลงท้ายด้วย /api จริง ๆ
// ❌ ผิด: ใช้ endsWith ตรวจสอบตรงกลาง
console.log("JavaScript".endsWith("va")); // false — "va" อยู่ตรงกลาง ไม่ใช่ท้าย
// ✅ ถูก: endsWith ใช้ตรวจสอบการลงท้ายเท่านั้น
console.log("JavaScript".endsWith("Script")); // true
// ❌ ผิด: length = 0 ให้ true เสมอ — อาจทำให้ logic ผิดพลาด
console.log("Hello".endsWith("Hello", 0)); // true — นี่อาจไม่ใช่สิ่งที่คุณต้องการสรุป
- endsWith(searchString, length?) — ตรวจสอบว่า string ลงท้ายด้วย substring ที่ระบุหรือไม่ คืนค่า boolean (true/false)
- case-sensitive — "A" กับ "a" ถือว่าไม่เหมือนกัน — ใช้ toLowerCase() หรือ toUpperCase() เพื่อทำ case-insensitive check
- length parameter — กำหนดความยาวของ string ที่ต้องการตรวจสอบ (default = str.length) — มีประโยชน์เมื่อต้องการตรวจสอบแค่ช่วงต้นของ string
- string เดิมไม่เปลี่ยน — immutable — endsWith() คืนค่า boolean ไม่ใช่ string ใหม่
- ใช้ endsWith() ใน if statement ได้ตรง ๆ เพราะคืนค่า boolean — if (str.endsWith(x)) { ... } — ไม่ต้องเทียบกับ true/false
- endsWith() ไม่รองรับ regular expression — ถ้าส่ง regex เข้าไปจะเกิด TypeError — ใช้ได้เฉพาะ string ธรรมดาเท่านั้น
- ระวัง: string ว่าง ("") จะคืนค่า true เสมอ — และ length = 0 จะคืนค่า true เสมอ — "Hello".endsWith("", 0) → true
- ใช้ endsWith() ในงานจริงเพื่อตรวจสอบนามสกุลไฟล์, URL path, email domain, และรูปแบบข้อมูล
- endsWith() ใช้คู่กับ startsWith() ได้ดี — ตรวจสอบทั้งต้นและท้ายของ string เพื่อยืนยันรูปแบบที่ซับซ้อน