เช็คว่า string มีคำเฉพาะไหม ด้วย JS?
ใช้ built-in includes() method เช็คว่า string มี characters ที่หรือไม่
สมมติอยากรู้ว่า HTML page นี้มีคำ "JavaScript" หรือไม่:
<article>
<p> Lorem ipsum... Beatae JavaScript fuga voluptatibus... </p>
</article>
Select element ที่น่าจะมี string (ที่นี่คือ <article>):
const findJavaScriptString = document.querySelector("article").innerText;
ใส่ innerText เพราะหาเฉพาะ human-readable elements
ถ้าหาใน ทุก HTML element รวม <script>, <style> ใช้ textContent แทน
Apply includes() และ print:
console.log(findJavaScriptString.includes("JavaScript")); // true