SoFunction
Updated on 2025-02-28

JavaScript skills that some veterans may not know

Some less commonly used but powerful JavaScript tips are not necessarily known by novices and veteran js developers.

1. Cut down array and array length

Copy the codeThe code is as follows:
var arr1 = arr2 = [1, 2, 3];

//Change arr1
arr1 = []; // arr2 is still [1,2,3]

You will find that arr1 uses the [] method to clear the value of arr2. If you want arr1 to change and arr2 to change, you can do this
Copy the codeThe code is as follows:

 var arr1 = arr2 = [1, 2, 3];
=0; // Pay attention to this step instead of arr1=[]
alert(arr2)

Arr2 was also cleared

2. Combination and combination

Copy the codeThe code is as follows:

var  arr1 = [1,2,3];
var  arr2 = [4,5,6];
var arr3=(arr2);
alert(arr3)

arr3 becomes
Copy the codeThe code is as follows:
[1,2,3,4,5,6]

In fact, there is also a simple method, such as using
Copy the codeThe code is as follows:
var  arr1 = [1,2,3];
var  arr2 = [4,5,6];
(arr1,arr2);
alert(arr1)

At this time, arr1 becomes 1,2,3,4,5,6

3. Browser feature detection

Look at the code to determine whether your browser operates

Copy the codeThe code is as follows:
if(){
alert("is opera")
}else{
alert("not opera")
}

You can do this the same
Copy the codeThe code is as follows:
if("opera" in window){
alert("is opera")
}else{
alert("not opera")
}

4. The object to be checked is an array

Copy the codeThe code is as follows:
var obj=[];
 if((obj)=="[object Array]")
alert("is array");
   else
alert("not an array");

Similarly, you can also determine whether the object is a string
Copy the codeThe code is as follows:
var obj="fwe";
 if((obj)=="[object String]")
alert("is a string");
   else
alert("not a string");