Home >Common Problem >What are JavaScript escape characters?

What are JavaScript escape characters?

百草
百草Original
2023-09-04 11:03:032244browse

The escape characters in JavaScript are backslashes and quotes, which can represent special characters in strings or change the meaning of characters. Detailed introduction: 1. Backslash is an escape character in JavaScript. It can be used in combination with other characters to represent special characters. Use two backslashes to represent the backslash itself. Use backslashes to escape some special characters. defined as their ASCII code representation. Use backslashes to escape some non-printing characters to their Unicode representation; 2. Quotation marks, etc.

What are JavaScript escape characters?

Escape characters in JavaScript are backslash (\) and quotation marks (' or "). These escape characters can represent special characters in strings character or change the meaning of a character.

Backslash (\)

Backslash is an escape character in JavaScript, which can be used in combination with other characters to Represents special characters. Here are some common backslash usages:

Represents the backslash itself: Use two backslashes (\) to represent the backslash itself. For example, the string "\n" means A newline character.

represents special characters: Use backslash to escape some special characters to their ASCII code representation. For example, "\0" represents a null character, "\t" represents a system table character, "\b" represents a backspace character, etc.

represents non-printing characters: Use backslashes to escape some non-printing characters to their Unicode representation. For example, "\u0000 " means a null character, "\u0009" means a tab character, etc.

Quotation marks (' or ")

In JavaScript, strings can Surrounded by single quotes (') or double quotes ("). These two quotes are used in basically the same way, but it should be noted that if the string contains single quotes or double quotes, you need to use another kind of quotes. Indicates the boundaries of strings. For example:

var myString = "He said, 'Hello!'"   // 使用双引号括起来的字符串中包含单引号
var myString = 'She said, "Hi!"'    // 使用单引号括起来的字符串中包含双引号

In addition, quotation marks can also be used to indicate property accessors or constructors. For example:

var myObject = {
  name: "John",
  age: 30
}
console.log(myObject["name"])   // 输出 "John"
console.log(myObject.age)       // 输出 30

Constructors are also expressed using quotation marks, for example:

var myArray = new Array("apple", "banana", "orange")

The above is the detailed content of What are JavaScript escape characters?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is ESD file?Next article:What is ESD file?