Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between single quotation marks and double quotes in javascript

Shulou Source: shulou.com Published: 2022-06-03 17:38:21 10月03日 Update

This article introduces the relevant knowledge of "what is the difference between single quotation marks and double quotation marks in javascript". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

There is no difference between single quotes and double quotes in JavaScript, see if you are used to it.

/* Initialize the string with single quotes and double quotes */var str1='I love JavaScript';var str1='I love JavaScript';docunment.write(str1+'

');docunment.write(str2+'

');

The results were as follows:

I love JavaScriptI love JavaScript

However, if we use quotation marks again, we can adopt the format of "outer double inside single" or "outer single inside double"; if we want the double quotation mark itself, we can only use "outer single inside double":

Example: Initialize a string with a mixture of single and double quotes

var str1 = 'I love "JavaScript"';var str2 = "I love 'JavaScript'";[xss_clean](str1);[xss_clean](str2);

The results were as follows:

I love "JavaScript"I love 'JavaScript'

Example: All single quotes or all double quotes

var str1 = 'I love 'JavaScript'';var str2 = "I love "JavaScript"";[xss_clean](str1);[xss_clean](str2);

As a result, the program directly went wrong!

Example: Transfer with backslash

var str1 = 'I love \'JavaScript\'';var str2 = "I love \"JavaScript\"";[xss_clean](str1);[xss_clean](str2);

The results were as follows:

I love 'JavaScript'I love "JavaScript"

So the fool thinks the conclusion is as follows

Conclusion:

(1) Character strings that use only characters, there is no difference between single quotes and double quotes

(2) Double quotation marks can be used directly in strings included in single quotation marks, and single quotation marks can be used directly in strings included in double quotation marks.

(3) If double quotation marks are used in the string included in double quotation marks, backslashes are needed to escape, note "\" ; similarly, single quotation marks are used in the string included in single quotation marks, and escape "\" is also needed.

"javascript single quotation marks and double quotation marks what is the difference" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Quotation marks characters strings results examples usage content slashes more knowledge conclusions escape utility achievement next dilemma reality situation fool tutorial Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MySQL Shulou Technology Shulou Tech Info OPPO Reno