Example 1 — setStart and setEnd (MDN)
Create a Range, bound it on a text node, then read the slice.
const p = document.querySelector("p");
const text = p.firstChild; // Text node
const range = document.createRange();
range.setStart(text, 0);
range.setEnd(text, 5);
console.log(range.toString()); // e.g. "Hello" How It Works
- Create.
createRange()returns a freshRange(MDN). - Pick a text node. Offsets count characters inside that node.
- Set boundaries. MDN example uses
setStartthensetEnd. - Read text.
toString()returns the characters inside the Range.

