Skip to main content
0 reactions

How to Write a String in JavaScript

Card titled Strings in JavaScript, labelled JavaScript basics, with four ticked lines: the string Pune in single quotes, India in double quotes, and their output Pune and India.

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

SyntaxJavaScript
'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

The Example

Two strings, one in each kind of quote, in a file named strings.js.

Step 1: Write the Code

strings.jsJavaScript
const city = 'Pune';
const country = "India";

console.log(city);
console.log(country);

Step 2: Run It

TerminalBash
node strings.js
OutputPlain text
Pune
India

The quotes aren’t part of the text: both strings print without them.

Common Mistakes

MistakeWhat happensDo this instead
Opening with one kind of quote and closing with the other: 'Pune"The program doesn’t start: SyntaxError: Invalid or unexpected tokenClose 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

JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.

0 comments