This is the 15th day of my participation in the August Text Challenge.More challenges in August

Type String

The String type is used to represent sequences of zero or more 16-bit Unicode characters, called strings. Strings can be represented by double “or single” quotes, so either of the following strings is correct:

var str1 = 'Alvin';
var str2 = "Alvin";
Copy the code

Although both single and double quotation marks can be used to represent strings in JavaScript, single or double quotation marks must occur in pairs and cannot be mixed. Such as:

var name = "Alvin';
var name1 = 'Alvin";
Copy the code

Both of these are wrong.

Character literal

The String data type contains special character literals, also known as escape sequences, that are used to represent non-print characters or characters that have other uses. These character literals look like this:

literal meaning
\n A newline
\t tabs
\b backspace
\r enter
\f Change the page
\ slash
Single quotes
Double quotation marks
\xnn A character represented by A hexadecimal code nn, such as \x41 for A
\unnnn A Unicode character represented by the hexadecimal code NNNN, such as \ U03A3 for the character ∑

These character literals can appear anywhere in the string and will also be parsed as a character, as shown in the following example:

var text = "This is the letter sigma: \u03a3";
Copy the code

In this example, the variable text is 28 characters long, with six character long escape sequences representing one character.

The length of any string can be obtained by accessing the length attribute, as in the following code:

console.log(text.length); / / 28Copy the code

Character string

ECMAScript strings are immutable, that is, once they are created, their values cannot be changed. If you want to change the string held by a variable, you first destroy the original string and then populate the variable with another string containing the new value, as shown in the code below:

var lang = 'Java';
lang = lang + "Script";
Copy the code

The variable lang in the above code starts with just the string “Java”. The second line of code redefines the value of lang as a combination of “Java “and” Script”, that is, “JavaScript”. This is done by first creating a new string of 10 characters, then populating the string with “Java” and “Script”, and finally destroying the original string “Java” and string “Script” because they are no longer useful. This happens in the background, which is why concatenating strings in some older browsers is slow. But these browsers have solved this inefficiency problem in later versions.

Convert to string

There are two ways to convert a value to a string. The first is to use the toString() method found on almost every value, which does nothing but return a string representation of the corresponding value. Consider the following example:

var age = 11;
var ageAsString = age.toString(); // String "11"
var found = true;
var foundAsString = found.toString(); // String "true"
Copy the code

Numeric values, Booleans, objects, and string values (each string also has a toString method, which returns a copy of the string) have toString() methods. Null and undefined do not have this method.

In most cases, the toString method is called without passing an argument. But when you call the toString method of a numeric value, you can pass one argument: the cardinality of the output value. By default, the toString method returns a string representation of the value in decimal format. By passing the base, toString can output string values in binary, octal, hexadecimal, or any other valid base format. Here are a few examples:

var num = 10;
console.log(num.toString());10 "/ /"
console.log(num.toString(2));/ / "1010"
console.log(num.toString(8));/ / "12"
console.log(num.toString(10));10 "/ /"
console.log(num.toString(16));//"a"
Copy the code

As you can see from this example, the toString method changes the output value by specifying the cardinality. The value 10 can be converted to a different numeric format on output, depending on the cardinality. Note that by default (with no arguments) the output value is the same as when the base 10 is specified.

If you don’t know whether the value to be converted is null or undefined, you can also use the String() function, which can convert any type of value to a String. The String function follows the following conversion rules:

  • If the value has a toString method, that method is called (with no arguments) and the result is returned
  • If the value is null, return “null”
  • If the value is undefined, return “undefined”

Here are a few more examples:

var value1 = 10;
var value2 = true;
var value3 = null;
var value4;

console.log(String(value1)); 10 "/ /"
console.log(String(value2)); //"true"
console.log(String(value3)); //"null"
console.log(String(value4)); //"undefined"
Copy the code

There are four values converted: numeric, Boolean, NULL, and undefined. Values and booleans are converted to the same result as when the toString method is called. Because null and undefined have no toString methods, String returns the literals of both values.