Handling Single and Double Quotes

Handling Single and Double Quotes


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?


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


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