JavaScript knowledge collation (2) strings and arrays

JavaScript knowledge collation (2) strings and arrays

table of Contents

JavaScript knowledge collation (2) strings and arrays

One, string

1. String declaration

2. Multi-line string

3. Template string

4. String method

Second, the array

1. Array declaration

2. Array method


As a programming language, javascript, like java and python, can store strings and arrays, and the usage methods are also very similar.

One, string

1. String declaration

Use double quotes (or single quotes) for strings directly

var str="hello,world!"

2. Multi-line string

Use backticks (`, the one to the left of the horizontal number key 1) to write multi-line strings. This is the new ES6 standard.

var str=`hello,
world
!`

The output in the browser is also multi-line (use the console.log() command to view in the browser right click -> check -> console)

3. Template string

The template string is an enhanced version of string writing, using backticks (` `, the one to the left of the horizontal number key 1), and the template string is a string that can use embedded expressions. Normally we write string splicing

var n1="Jack";
var n2="Mary";
console.log(n1+"loves"+n2)

String splicing will have a lot of + signs and a lot of quotation marks, template strings can solve this problem

Use template strings

var n1="Jack";
var n2="Mary";
console.log(`${n1} loves ${n2}`);

4. String method

  • length
str.length
  • All caps
str.toLocaleUpperCase()
  • Get character position
str.charAt("A")
  • Substring
str.substring(start,end)

Second, the array

The array in js can not only store elements of the same type, but also elements of different types. In other words, this is a list.

1. Array declaration

var arr=[1,"a","hello",34,2.4]

2. Array method

  • Length arr.length
  • Get the element index indexOf()
  • Get sub-array slice(start,end)
  • Tail access element arr.push(), arr.pop()
  • Head access element arr.shift(), arr.unshift()
  • Element flip arr.reverse()
  • Concatenate arr.concat(arr1)
  • Join arr.join("-")

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_41459262/article/details/113920715