Originally published: Holmeshe.me

This article is a Chinese remake.

I remember doing a lot of crap with Ajax early on and being impressed by JS as “very weird”. Recently CHANGED a job, the working language is JS. And then I found that this language is really amazing, both front and back, basically become a synonym for full stack. So this is a good opportunity to learn the language systematically. Because it’s for programmers, this series won’t cover basic “if-else”, looping, or object orientation. Instead, I pay attention
differences, hope to give you a code-like learning experience in the Pull Request!

In JS, strings and arrays are used in a very similar way. First, strings cannot be modified, whereas arrays can.

In code:

var str = "123";
str[0] = "0";
alert(str);
 
var arr = [1, 2, 3];
arr[0] = "0";
alert(arr);
Copy the code

Running results:

123,1,3Copy the code

Incidentally, strings in JS are represented by a pair of double quotes “”, a pair of single quotes “, or reverse single quotes (used to declare multi-line strings). Arrays are represented by a bunch of brackets [].

In code:

var str = "123";
str.length = 10;
alert(str);
 
var arr = [1, 2, 3];
arr.length = 10;
alert(arr);
alert(arr[3]);
Copy the code

Running results:

123 1, 2, 3,,,,,,,,, undefinedCopy the code

Interestingly, if you forcibly change the length of the array, the JS engine will assign undefined to the extra members at runtime. Of course, strings cannot be changed this way because they cannot be modified.

Turn the ideogram \

JS, like most languages, uses \ to translate special characters. (for example, in a string represented by “”).

In code:

alert("\"");
alert("a\nb");
alert("a\tb");
Copy the code

Running results:

"
a
b
a    b
Copy the code

As mentioned above, \” represents in a string “, \n represents a newline, and \t represents a TAB character.

If you want to know all the ways to represent special characters, Google the keyword “Javascript Special Characters.”

In fact, we can use \ to write ascII and Unicode in strings. However, I feel that this does not make sense in a high-level language such as JS, so I omit this part of the explanation.

Joining together

We use 1) +, or 2) string templates to concatenate strings,

In code:

var js = "Javascript";
var message1 = "I like " + js;  //using +
alert(message1);
var message2 = `and ${js}'s particularities`; //using string template
alert(message2);
Copy the code

Running results:

I like Javascript
and Javascript's particularities
Copy the code

If it’s an array, we use concat(),

In code:

var arr = ['a', 'b', 'c'];
var added = arr.concat(['d', 'e', 'f']);
alert(added);
Copy the code

Running results:

a,b,c,d,e,f
Copy the code

Visit the subscript

Use []

In code:

var arr = ['a', 'b', 'c'];
var str = "abc";
alert(arr[0]);
alert(str[0]);
Copy the code

Running results:

a
a
Copy the code

To find the

Use indexOf ()

In code:

var arr = ['abc', 'xyz', 123, 789]; alert(arr.indexOf(123)); alert(arr.indexOf('xyz')); alert(arr.indexOf(789)); alert(arr.indexOf('abc')); var str = "abcxyz"; //a=>0 b=>1 c=>2 x=>3 alert(str.indexOf('xyz'));Copy the code

Running results:

2, 1, 3, 0, 3Copy the code

Don’t explain.

Subsets (substrings or subarrays)

Substring () for strings and slice() for arrays

In code:

var str = "abcxyz"; //a=>0 b=>1 c=>2 x=>3 y=>4 z=>5 alert(str.substring(0, 5)); // does not include 5(z) alert(str.substring(3)); //start from 3(x) to the end var arr = ["a", "b", "c", "x", "y", "z"]; alert(arr.slice(0, 5)); // does not include 5(z) alert(arr.slice(3)); //start from 3(x) to the endCopy the code

Running results:

abcxy
xyz
a,b,c,x,y
x,y,z
Copy the code

Okay, so that’s where I’ll stop today. If you think this article is good, please follow this column on Zhihu. I can also go to Medium and slap my other articles. Thanks for reading! 👋