JavaScript special character _ basic knowledge

Source: Internet
Author: User
Special JavaScript characters

You can use a backslash in JavaScript to add special characters to a text string.

Insert special characters

A backslash is used to insert ellipsis, line breaks, quotation marks, and other special characters into a text string.

See the following JavaScript code:

var txt="We are the so-called "Vikings" from the north."document.write(txt)

In JavaScript, strings start or end with single or double quotation marks. This means that the above string will be truncated to: We are the so-called.

To solve this problem, you must add a backslash (\) before the quotation marks in "Viking (\). In this way, each double quotation mark can be converted to a literal string.

var txt="We are the so-called \"Vikings\" from the north."document.write(txt)

Now JavaScript can output the correct text string: We are the so-called "Vikings" from the north.

This is another example:

document.write ("You \& me are singing!") 

The preceding example generates the following output:

You & me are singing!

The following table lists other special characters that can be added to a text string using a backslash:

Code Output
\' Single quotes
\" Double quotation marks
\& And number
\\ Backslash
\ N Line break
\ R Carriage return
\ T Tab
\ B Escape character
\ F Page feed

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.