document.queryCommandState() is a deprecated, non-standard instance method that reports whether the current selection already has a given execCommand() applied (see MDN Document: queryCommandState()). Learn the true / false / null return values, MDN’s bold toolbar example, how it differs from queryCommandEnabled(), and five try-it labs.
01
Kind
Instance method
02
Args
command string
03
Returns
boolean | null
04
Reads
Selection
05
Status
Deprecated
06
Also
Non-standard
Fundamentals
Introduction
A rich-text Bold button should look pressed when the selection is already bold, and unpressed when it is not. Legacy editors used document.queryCommandState("bold") for that.
MDN: the method tells you if the current selection has a certain Document.execCommand() command applied. Unlike queryCommandEnabled() (can I run it?), this API asks “is it already on?”
💡
Think: “Is this formatting active on the selection?”
1) Focus a contenteditable area and select text 2) Call document.queryCommandState("bold") 3) Handle true, false, or null (unknown) 4) Optionally toggle with execCommand("bold")
An instance method on the page’s document object that reads whether a named editing command is applied to the current selection (MDN).
command — a command from Document.execCommand() (MDN).
Return value — boolean, or null if the state is unknown (MDN).
true — the command is applied (e.g. selection is bold).
false — the command is not applied.
null — indeterminable / unknown (mixed selection or engine gap).
vs enabled — enabled = can run; state = already applied.
Foundation
📝 Syntax
General form of Document.queryCommandState (MDN):
JavaScript
queryCommandState(command)
Parameters
command — a command from Document.execCommand() (MDN). Common ones for beginners: "bold", "italic", "underline".
Return value
A boolean, or null if the state is unknown (MDN).
MDN-style bold message map
JavaScript
const state = document.queryCommandState("bold");
let message;
switch (state) {
case true:
message = "The bold formatting will be removed from the selected text.";
break;
case false:
message = "The selected text will be displayed in bold.";
break;
default:
message = "The state of the 'bold' command is indeterminable.";
break;
}
console.log(message);
document.execCommand("bold");
Compare
⚖️ queryCommandState vs queryCommandEnabled vs execCommand
MDN’s core pattern: read bold state, explain, then toggle.
Example 1 — MDN: test the state of bold
Select text in a contenteditable region, then map true / false / unknown.
JavaScript
function makeBold() {
const state = document.queryCommandState("bold");
let message;
switch (state) {
case true:
message = "The bold formatting will be removed from the selected text.";
break;
case false:
message = "The selected text will be displayed in bold.";
break;
default:
message = "The state of the 'bold' command is indeterminable.";
break;
}
console.log(message);
document.execCommand("bold");
}
document.querySelector("button").addEventListener("click", makeBold);
Document.queryCommandState() is Deprecated and Non-standard on MDN (not part of any current specification). Logos use the shared browser-image-sprite.png sprite from this project. Still present in many engines for legacy editing, but do not build new products on it.
✓ Deprecated · Non-standard
Document.queryCommandState()
Legacy selection-state probe for editor commands — still seen with old toolbars; prefer modern editor stacks.
LegacyNot for new apps
Google ChromeLegacy editing probe still present — avoid for new apps
Legacy
Mozilla FirefoxLegacy path; expect inconsistencies — avoid for new apps
Legacy
Apple SafariLegacy editing support; verify each command
Legacy
Microsoft EdgeChromium legacy path — avoid for new code
Legacy
OperaFollow Chromium legacy behavior
Legacy
Internet ExplorerHistoric rich-text path only
Legacy
queryCommandState()Avoid
Bottom line: Learn it for legacy execCommand toolbars and interviews. For new rich-text UX, prefer modern editor libraries.
Wrap Up
Conclusion
document.queryCommandState(command) reports whether a legacy editor command is applied to the current selection. It can return true, false, or null. MDN marks it deprecated and non-standard. Use it only when maintaining execCommand-based toolbars; for new apps, choose modern editors.
Build a new rich-text product on queryCommand* + execCommand
Assume every engine returns consistent mixed-selection states
Confuse state with enabled or supported
Ignore null — treat it as “unknown”
Forget to focus / select inside contenteditable first
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about queryCommandState()
Legacy selection-state probe — know it, rarely ship it.
5
Core concepts
📝01
Returns
bool | null
MDN
⚠️02
Status
Deprecated
MDN
🛡03
Also
Non-standard
MDN
📋04
Reads
selection
formatting
📋05
Pairs with
execCommand
legacy
❓ Frequently Asked Questions
MDN: queryCommandState() tells you if the current selection has a certain Document.execCommand() command applied. For example, whether the selection is already bold.
Yes. MDN marks Document.queryCommandState() as Deprecated and Non-standard. It is not part of any current specification and is no longer on track to become a standard.
MDN: a boolean value, or null if the state is unknown (indeterminate).
MDN: if you still use deprecated execCommand() for a rare reason, queryCommandState() can help build a complete toolbar UX (pressed vs unpressed). Prefer modern editor libraries for new products.
queryCommandEnabled() asks whether a command can run right now. queryCommandState() asks whether that command’s formatting is already applied to the current selection.
MDN’s example treats a non-boolean result as indeterminable — for example a mixed selection that is partly bold and partly not, or when the browser cannot determine the state.
Did you know?
MDN’s demo reads queryCommandState("bold")before calling execCommand("bold"), so it can explain whether the next click will add or remove bold. That “preview the toggle” pattern is the classic reason legacy toolbars kept this API around.