devxlogo

Handling Single and Double Quotes

Handling Single and Double Quotes

Question:

I am trying to put lines of text into an array:

messageArray = new Array();messageArray[0] = 'Information deduced from the document's content.'

Iā€™m running into problems with the single quotes (ā€˜) because it thinks that the text is over and that there is an extra ā€˜. What can I do about this?

Answer:

Luckily there is a simple solution to the problem youā€™ve run into. You can either use double quotes (ā€œ) or single quotes (ā€˜) to delimit your JavaScript strings. So if your string is likely to include one kind of quote within it, just use the other kind to actually tell the browser where the string starts and ends. For example:

messageArray[0] = "Information deduced from the document's content.";

You can also escape the quotes within your strings by putting a backslash before them. Like this:

ā€˜ Single quotation mark
ā€ Double quotation mark

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist