Site icon JD Bots

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

Syntax

'text'
"text"

See String 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.js

const city = 'Pune';
const country = "India";

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

Step 2: Run It

Terminal

node strings.js

Output

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.

Exit mobile version