A string is a piece of text. In JavaScript, you write it between single quotes or double quotes, and both give the same text.
Every command and output on this page is real, run with Node.js 26.13.1 on macOS.
Syntax
'text'
"text"
'text': a string between single quotes."text": the same string between double quotes.- A string ends with the same kind of quote it starts with.
See String (opens in a new tab) on MDN.
Prerequisites
- Node.js, to run the example. Install it from the Node.js download page (opens in a new tab).
The Example
Two strings, one in each kind of quote, in a file named strings.js.
Step 1: Write the Code
const city = 'Pune';
const country = "India";
console.log(city);
console.log(country);
Step 2: Run It
node strings.js
Pune
India
The quotes aren’t part of the text: both strings print without them.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Opening with one kind of quote and closing with the other: 'Pune" | The program doesn’t start: SyntaxError: Invalid or unexpected token | Close the string with the quote you opened it with |
FAQ
Should I use single or double quotes?
Either gives the same string. Pick one and use it throughout a file.
Conclusion
Put text between matching quotes, single or double, and it’s a string.
Additional Resources
- String (opens in a new tab) (MDN)
- Grammar and types (opens in a new tab) (MDN)
- Run Node.js scripts from the command line (opens in a new tab) (Node.js docs)
JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.
0 comments