define

A string is zero or more characters grouped together in single or double quotation marks.

'abc'
"abc"
Copy the code

Inside a single quoted string, you can use double quotes. Inside a string, single quotes can be used.

'key = "value"'
"It's a long journey"
Copy the code

If you want to use single quotes inside a single quoted string, you must precede the inner single quotes with a backslash to escape them. Double quotation marks are used inside strings, as well.

'Did she say \'Hello\'? '
// "Did she say 'Hello'?"

"Did she say \"Hello\"?"
// "Did she say "Hello"?"
Copy the code

A string can only be written on one line by default, and multiple lines will cause an error.

'a
b
c'
// SyntaxError: Unexpected token ILLEGAL
Copy the code

If a long string must be split into multiple lines, use a backslash at the end of each line.

var longString = 'Long \
long \
long \
string';

longString
// "Long long long string"
Copy the code

The concatenation operator (+) can concatenate multiple single-line strings, breaking a long string into multiple lines and printing a single line.

var longString = 'Long '
  + 'long '
  + 'long '
  + 'string';
Copy the code

The string length returned by JavaScript may be incorrect.

JavaScript support for UTF-16 is incomplete, supporting only two-byte characters and not four-byte characters due to historical reasons. This is because when the first version of JavaScript was released, Unicode code points were only coded to U+FFFF, so two bytes was sufficient for representation. Later, Unicode included more and more characters, and four-byte encodings emerged. However, the JavaScript standard was set at this point, limiting the character length to two bytes, making it impossible to recognize the four-byte character 𝌆. The four-byte character 𝌆 from the previous section will be correctly recognized by the browser as a single character, but JavaScript will not recognize it as two characters.

'𝌆'.length / / 2
Copy the code

Base64 transcoding#

Base64 is an encoding method that converts any value into 64 printable characters: 0 to 9, A to Z, A to Z, + and /. The main purpose of using it is not to encrypt, but to avoid special characters and simplify the processing of the program.

JavaScript natively provides two Base64-related methods.

  • btoa(): Converts any value to Base64 encoding
  • atob(): Base64 encoding is converted to the original value
var string = 'Hello World! ';
btoa(string) // "SGVsbG8gV29ybGQh"
atob('SGVsbG8gV29ybGQh') // "Hello World!"
Copy the code

Note that these methods do not work with non-ASCII characters and will report errors.

btoa('hello') / / an error
Copy the code

To convert non-ASCII characters into Base64 encoding, you must insert a transcoding link and use these two methods.

function b64Encode(str) {  
    return btoa(encodeURIComponent(str));
}
function b64Decode(str) { 
    return decodeURIComponent(atob(str));
}b64Encode('hello') 
// "JUU0JUJEJUEwJUU1JUE1JUJE"b64Decode('JUU0JUJEJUEwJUU1JUE1JUJE') 
// "Hello"
Copy the code