string

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"'
"Let's go!"
Copy the code

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

'a
b
c'
/ / an error
Copy the code

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

var str = 'abc \
def \
ghij \
klmn';

str
// "abc def ghij klmn"
Copy the code

Backslashes must be followed by newlines and must not be followed by other characters (such as Spaces), otherwise an error will be reported.

The concatenation operator (+) can concatenate multiple single-line strings.

var str = 'abc '
  + 'def '
  + 'higk '
  + 'lmn';
Copy the code

You can concatenate variables

var str1 = 'opqrst'
var str2 = 'abc '
  + 'def '
  + 'higk '
  + 'lmn '
  + str1;
Copy the code

Escape of special characters

The backslash (\) is also called an escape character because it has special meaning in a string and is used to indicate special characters.

Common special characters that need to be escaped with backslashes:

‘: single quotes “: double quotes \ : backslash \ N: newline \ R: enter key \t: TAB

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. The same goes for using double quotes inside strings.

'Did you say \'Hai\'? '
// "Did you say 'Hai'?"

"Did you say \"Hai\"?"
// "Did you say "Hai"?"

console.log('abcd\nefg')
//abcd
//efg
Copy the code

Template string: backquotes

Template strings are enhanced strings, identified by backquotes (‘). It can be used as a regular string, it can be used to define multi-line strings, or it can be used to embed variables in strings.

The array nature of strings

Strings can be thought of as an array of characters, so you can use the square bracket operator of the array to return a character at a position (position numbering starting at 0).

var arr = 'Hello World';

console.log(arr[1]) // "e"
console.log(arr[6]) // "W"
console.log(arr[8]) // "r"

// Use the square brackets operator directly on strings
console.log('Hello World'[0]) // "H"
Copy the code

If the number in square brackets exceeds the length of the string, or if there are no numbers in square brackets at all, undefined is returned.

console.log('abc'[3]) // undefined
console.log('abc'[-1]) // undefined
console.log('abc'['y']) // undefined
Copy the code

But the similarity between strings and arrays ends there. In fact, you cannot change a single character in a string.

var arr = 'hello';

delete arr[0];
console.log(arr) // "hello"

arr[1] = 'a';
console.log(arr) // "hello"

Copy the code

The above code shows that individual characters inside a string cannot be changed, added or deleted

The length property of the string

The length property returns the length of the string, which is also unchangeable.

var arr = 'hello';
console.log(arr.length) / / 5

arr.length = 3;
console.log(arr.length) / / 5

arr.length = 9;
console.log(arr.length) / / 5
Copy the code

The above code indicates that the length property of the string cannot be changed, but it does not report an error.

Boolean value

Operator that can return a Boolean value

  • Pre-logic operator:! (Not)
  • Equality operators: ===,! ==, ==,! =
  • Comparison operators: >, >=, <, <=

Six values that are false when cast to Boolean

Undefined null false 0 NaN “” or ” (empty string)

All other values are considered true.

Undefined and null

Null and undefined can both mean “nothing” and have very similar meanings. There is little difference in grammatical effect.

undefined= =null
// true
Copy the code

Null refers to an empty object and is 0 when converted to a value. Undefined is a raw value that means “not defined here”. When converted to a value, it is NaN.

console.log(6 + null); / / 6
console.log(6 + undefined); // NaN
Copy the code

practice

Print a formatted poem