How to Check Whether a Character Is a Double Quote in JavaScript
- Authors

- Name
- Geeks Kai
- @KaiGeeks
Use value === '"' when the value must be exactly one ASCII double quote, U+0022. It returns false for an empty string, a longer string, a curly quote or a non-string value. Escaping a quote in JavaScript source does not change the character stored in the resulting string.
function isDoubleQuote(value) {
return value === '"'
}
function containsDoubleQuote(text) {
if (typeof text !== "string") {
throw new TypeError("Expected a string.")
}
return text.includes('"')
}
const supportedQuoteCharacters = new Set(['"', "“", "”"])
function isSupportedDoubleQuote(value) {
return supportedQuoteCharacters.has(value)
}
console.log(isDoubleQuote('"')) // true
console.log(isDoubleQuote('"hello')) // false: longer than one quote
console.log(isDoubleQuote("“")) // false: different code point
console.log(isDoubleQuote(null)) // false: no coercion
console.log(containsDoubleQuote('say "hello"')) // true
console.log(isSupportedDoubleQuote("“")) // true: explicitly allowed
| Requirement | Check for a string input |
|---|---|
| Exactly one ASCII double quote | text === '"' |
| First character is a double quote | text.startsWith('"') |
| A double quote anywhere | text.includes('"') |
| Both a single quote and a double quote occur | Two includes() checks combined with && |
const quotedText = '"hello'
console.log(quotedText.startsWith('"')) // true: starts with a quote
console.log(quotedText.charCodeAt(0) === 34) // true: first UTF-16 code unit is U+0022
console.log(quotedText === '"') // false: the whole string is not one quote
const bothQuoteTypes = "it's a \u0022quote\u0022"
console.log(bothQuoteTypes.includes("'") && bothQuoteTypes.includes('"')) // true
console.log(/["']/.test("|")) // false: two quote characters, not a pipe
charCodeAt(0) === 34 inspects only the first UTF-16 code unit. It does not prove that the entire string has length one. Likewise, /"/.test(text), match(/"/) and indexOf('"') !== -1 detect presence, not exact equality.
ASCII ", left curly “ and right curly ” are distinct characters. The Set-based helper explicitly permits these three and nothing else. It is an application policy, not a claim that all quotation marks in every language are included.
Inside a regular-expression character class, characters are alternatives; || is not a logical OR operator. A class such as /['|" ]/ would also allow a pipe or space. Use /["']/ only when the intended task is presence of either ASCII quote character.
Finding quote characters is not sufficient to parse JSON, CSV, escaped strings or nested language syntax. Use the relevant parser for those tasks. For this character-level question, direct strict equality is the clearest starting point.