javascript 字符串中单引号和双引号区别

最近在使用JavaScript编程,遇到使用字符串的情况。

以下是一些测试代码:

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)如果要用反斜杠,则输入‘\\’

你可能感兴趣的:(javascript 字符串中单引号和双引号区别)