JavaScrript单引号与双引号的区别

JavaScrript单引号与双引号的区别

[TOC]

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var str = 'This is Jack';
var str2 = "This is Tom";
var str3 = 'This is "Rose"';
var str4 = "This is 'LiLi'";
var str5 = "This is \"Sun\"";
var str6 = 'This is \'Mary\'';
var str7 = "This is \'Lucy\'";
var str8 = 'This is \"Rooney\"';
var str9 = 'This is \\Ronaldo\\';
console.log(str);
console.log(str2);
console.log(str3);
console.log(str4);
console.log(str5);
console.log(str6);
console.log(str7);
console.log(str8);
console.log(str9);
//显示结果
This is Jack
This is Tom
This is "Rose"
This is 'LiLi'
This is "Sun"
This is 'Mary'
This is 'Lucy'
This is "Rooney"
This is \Ronaldo\
  1. 只使用字符的字符串,单引号和双引号没有区别

  2. 在单引号包括的字符串中可以直接用双引号,在双引号包括的字符串中可以直接用单引号

  3. 如果在双引号包括的字符串中用双引号,需要用反斜杠转义,注意是”\” ;同样在单引号包括的字符串中用单引号,也需要转义

  4. 如果要用反斜杠,则输入‘\’