JavaScript String with quotes

Strings are commonly utilized for managing and storing text information, although they can also accommodate special characters and numerical data. For instance, strings can hold diverse data types like addresses or email IDs.

String values are formed by enclosing data within quotation marks. Various programming languages, including JavaScript, provide flexibility in using either double quotes (" ") or single quotes (' ') to define string data. In the following section, you will learn how to display strings containing quotes.

For example

Quotes Example
Single Quote (' ') 'This is a string inside single quotes.'
Double Quote (" ") "This is a string inside double quotes."

These single quotes and double quotes do not print with string on the web browser. But sometimes we need to print the quotes with the string as well. Some words like it's, b'day, seven o'clock, can't, and etc. Firstly, see how a string simply display on the web.

Example: String without quotes

Observe this straightforward example demonstrating how to display strings without using single or double quotes.

Copy Code

Example

<script>

var pat1 = 'A string inside single quote';

var pat2 = "A string inside double quote";

document.write(pat1 + "</br>");

document.write(pat2);

</script>

Output

Output

A string inside single quote

A string inside double quote

In this example, it is evident that both strings have been displayed without quotation marks.

Enclosing quotation marks

To prevent issues with quotation marks, you have the option of employing a backslash (\) before the specific word or string. It is essential to note that if you opt not to utilize the backslash, you must employ an alternate quotation mark inside and outside of a string. For instance, if you intend to include a single quote within a string, the external quotes should be double quotes. Likewise, if you aim to incorporate a double quote within a string, the external quotes should be single quotes.

Let's see how it will be done in JavaScript.

Example: Print quotes using backslash (\)

In this instance, we shall utilize the backslash (\) as an escape character for the quotation mark.

Copy Code

Example

<html>

<body>

<script>

var singleQ = 'It\'s nine o\' clock in the morning.';

var doubleQ = "Mukesh Ambani is \"the richest man\" of India.";

document.write(singleQ + "</br>");

document.write(doubleQ + "</br>");

</script>

</body>

</html>

Output

Output

It's nine o' clock in the morning.

Mukesh Ambani is "the richest man" of India.

JavaScript may misinterpret a string if it is written as follows:

Example

var dq = "Mukesh Ambani is "the richest man" of India.";

Note: Do not forget to insert a backslash while escaping the quotation mark, especially when outside and inside quotes are the same.

Example: Print quotes using alternative String syntax

In this instance, we will demonstrate the utilization of distinct quotation marks both within and outside a string. This enables the same functionality to be achieved. Refer to the following illustration to observe how this can be implemented:

Copy Code

Example

<html>

<body>

<script>

var singleQ = "It's nine o' clock in the morning.";

var doubleQ = 'Always say "Thank you" when anyone helps you.';

document.write(singleQ + "</br>");

document.write(doubleQ + "</br>");

</script>

</body>

</html>

Output

Output

It's nine o' clock in the morning.

Always say "Thank you" when anyone helps you.

In addition to the aforementioned techniques, there is another method available for showing single and double quotes on a web browser.

Example: Use apostrophe to print single quote

In this instance, we will utilize an apostrophe within a single-quoted string. Take a look at the following illustration to observe how this can be achieved:

Copy Code

Example

<html>  

<body>  

<script>  

var sq = 'It\'s an example of printing the single quote with string.';  

document.write(sq);  

</script>  

</body>  

</html>

Output

Output

It's an example of printing the single quote with string.

Example: Use &quot to print double quote

In JavaScript, the "&quot" entity can be utilized within a string to display a double quotation mark. This enables the flexibility to use any type of quotation mark. Refer to the example provided below:

Copy Code

Example

<html>  

<body>  

<script>  

var dq1 = "Always say "Thank you" when anyone helps you.";  

var dq2 = 'Always say "Thank you" when anyone helps you.';  

document.write(dq1 + "</br>");  

document.write(dq2);  

</script>  

</body>  

</html>

Output

Output

Always say "Thank you" when anyone helps you.

Always say "Thank you" when anyone helps you.

Input Required

This code uses input(). Please provide values below: