document.createRange() is an instance method that returns a new Range object (see MDN Document: createRange()). Learn the default collapsed boundaries, how to set start/end points, useful Range helpers, how Ranges relate to Selection, and five try-it labs.
01
Kind
Instance method
02
Args
None
03
Returns
Range
04
Default
Collapsed @ 0
05
Next step
setStart / setEnd
06
Status
Baseline
Fundamentals
Introduction
A Range describes a continuous slice of the document — from one boundary point to another. Editors, find-in-page features, and “highlight this word” tools all lean on Ranges.
document.createRange() gives you a fresh Range. MDN: its start and end both begin at offset 0 of the Document on which you called the method. That default is almost never the slice you want, so the next step is always to set real boundaries.
💡
Create → set boundaries → use
1) const range = document.createRange() 2) range.setStart(...) / range.setEnd(...) (MDN) 3) Read text, extract nodes, or add the range to a Selection
MDN Notes: once a Range is created, you need to set its boundary points before you can make use of most of its methods.
extractContents() removes the ranged slice from the live tree and returns it as a fragment — useful for cut / move operations in editors.
Example 5 — Add the Range to Selection
Make the range visible as a browser highlight.
JavaScript
const p = document.querySelector("#highlight");
const range = document.createRange();
range.selectNodeContents(p);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
console.log(sel.toString()); // same text as range.toString()
Document.createRange() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createRange()
Create Range objects for DOM selection and editing across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createRange()Wide
Bottom line: Use createRange() to build a Range, set boundaries with setStart/setEnd (or helpers), then read, extract, or add it to Selection.
Wrap Up
Conclusion
document.createRange() returns a new Range collapsed at the Document. Follow MDN: set setStart / setEnd (or selectNodeContents), then use the range to read text, extract content, or drive the Selection API.
Set boundaries right after createRange() (MDN Notes)
Prefer selectNodeContents when you want a whole element interior
Use range.collapsed to detect caret-like ranges
Clear and addRange when you need a visible highlight
Work with text nodes carefully when using character offsets
❌ Don’t
Assume the default range selects page content (it does not)
Expect a blue highlight without the Selection API
Pass invalid offsets to setStart / setEnd
Confuse Range with DocumentFragment
Forget that extractContents mutates the live DOM
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createRange()
Build DOM Ranges, then set boundaries before using them.
5
Core concepts
📝01
Returns
Range
MDN
📄02
Args
none
MDN
⚖️03
Default
doc @ 0
collapsed
⚡04
Next
setStart/End
MDN
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createRange() returns a new Range whose start and end are both at offset 0 of the Document it was called on. You then set real boundaries with setStart, setEnd, or helpers like selectNodeContents.
No. MDN marks Document.createRange() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A new Range object (MDN). It does not select text in the UI by itself — attach it to a Selection (for example selection.addRange(range)) if you want a visible highlight.
No. createRange() takes no parameters (MDN). Boundaries are configured afterward.
MDN Notes: once a Range is created, you need to set its boundary points before you can make use of most of its methods. The default range is collapsed at the document.
createRange builds a Range you control in code. getSelection() reads (or updates) what the user currently selected. You often create a Range, then add it to the Selection to show a highlight.
Did you know?
Modern browsers also support new Range() as a constructor. For tutorials and older codebases, document.createRange() remains the classic MDN-documented factory — and it always associates the range with that document context.