JavaScript program development (9) - data types of js syntax

Continue with the previous chapters.

String type

The String type is a sequence of characters consisting of zero or more 16-bit Unicode characters, that is, a string. Strings can be represented by double quotes or single quotes, but they must appear in pairs, that is, strings that start with double quotes must end with double quotes, and strings that start with single quotes must end the string with single quotes. This should be easy to understand, without writing an example.

1. Character literals

String contains a set of special character literals, also known as escape sequences, called escape characters in other programming languages, used to represent non-printing characters, or characters with special purposes. The specific table is as follows:

literal meaning
\n newline
\t tabulation
\b space
\r Enter
\f paper feed
\\ slash
\' apostrophe
\" Double quotes
\ xnn Use hexadecimal code nn to represent a character (n is 0-F), such as \x41 for "A"
\ unnnn A Unicode character represented by the hexadecimal code nnnn (n is 0-F)

2. String Features

  Strings in js are immutable. That is to say, once a string in js is created, its value cannot be changed. If you want to change the string in a variable, you must destroy the string originally stored in the variable. Such as

   var lang = "Java";
   lang = lang + "Script";
   alert(lang); //Output JavaScript

3. Convert to String

  There are two ways to convert a value to a string, one is through the toString() method, and the other is through the transformation function String().

  Almost every numeric value has a toString() method, such as numbers, booleans, objects and strings, all have toString(), null and undefined do not have this method.

  In most cases, calling the toString() method does not need to pass parameters, but it does not mean that parameters cannot be passed. You can pass a parameter that represents the base of the output value. By default, toString() returns a string representation of the value in decimal format, but with an argument representing the radix, it is possible to output string values ​​in binary, octal, hexadecimal, or any other valid base format.

  The conversion function String() can convert any type of value to a string, but the following rules must be followed:

  • If the value has a toString() method, the toString() method is called;
  • If it is null, return "null";
  • If undefined, return "undefined".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325815525&siteId=291194637