The difference between single and double quotes in JavaScript strings

Source: Internet
Author: User

Recently using JavaScript programming, encountering the use of strings

Wrote a little bit of test code

        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);
Test Results:

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\

Conclusion:

(1) Use only character strings, single and double quotes without distinction

(2) You can use double quotes directly in a string enclosed in single quotes, and you can use single quotes directly in a string enclosed in double quotes

(3) If double quotes are used in double quotes, you need to escape with a backslash, note "\", and a single quote in the string enclosed in single quotes, and you also need to escape

(4) If you want to use a backslash, enter ' \ \ '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.