document.queryCommandState(command) reports whether the current selection already has a given execCommand applied — for example, whether selected text is bold. It returns true, false, or null (unknown).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.queryCommandState. Start with this line.
JavaScript
document.queryCommandState(command)
Call
What it does
document.queryCommandState(command)
Primary signature.
document.queryCommandState(...)
Call with the arguments your case needs.
Built into the browser. Call queryCommandState(...) on document — no import required.
Library declarations
JavaScript
document.queryCommandState(command)
Parameters
Parameter
Description
command
Required. An execCommand name to inspect
Return Value
Return Value
Description
*
true, false, or null
JavaScript Examples
Example 1 — Read the Bold State
Ask whether the current selection is bold. Handle true, false, and null.
JavaScript
const state = document.queryCommandState("bold");
if (state === true) {
console.log("bold is applied");
} else if (state === false) {
console.log("bold is not applied");
} else {
console.log("bold state unknown (null)");
}
It tells you whether the current selection already has a given Document.execCommand() command applied — for example, whether the selection is bold.
Yes. MDN marks it Deprecated and Non-standard. It is not part of any current specification and is no longer on track to become a standard.
A boolean (true or false), or null if the state is unknown. Treat null as indeterminate — for example a mixed selection.
Only when maintaining legacy execCommand() toolbars. MDN notes it can help build a complete 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.
When the browser cannot decide — often a mixed selection that is partly bold and partly not. MDN’s example treats a non-boolean result as indeterminable.