Javascript arrays work similarly to arrays in most programming languages.
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>learn4Array</title></head><body><script type="text/javascript">//Create and fill array var myArray = new Array();myArray[0] = 100;myArray[1] = "Luka";myArray[2] = true;</script></body></html>
When creating an array, you do not need to declare the number of elements in the array. Javascript arrays are automatically resized to accommodate all elements.
There is no need to declare the type of data contained in the array. JavaScript arrays can mix types containing various data.
1. Use array literals
Using literals, you can create and fill an array in a statement.
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>learn4Array</title></head><body><script type="text/javascript">var myArray = [100,"Luka",true];</script></body></html>
This example creates a new array by specifying the required array elements between a pair of square brackets ([ and ]) and assigns it to the variable myArray .
2. Read and modify the array content
To read the value of an array element at a specified index position, use a pair of square brackets ([ and ]) and place the index value between square brackets. The index value of the JavaScript array starts at 0.
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>learn4Array</title></head><body><script type="text/javascript">var myArray = [100,"Luka",true];//Read the array element value of the specified index position document.writeln("Index 0:"+myArray[0]+"<br />");//Modify the array myArray[0] = "Monday";document.writeln("Index 0:"+myArray[0]);</script></body></html>Output result:
Index 0:100
Index 0:Monday
3. Enumerate the array contents
You can enumerate the array contents using a for loop.
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>learn4Array</title></head><body><script type="text/javascript">var myArray = [100,"Luka",true];for(var i= 0;i < myArray.length;i++){document.writeln("Index "+i+": "+myArray[i]+"<br />")}</script></body></html>Output result:
Index 0: 100
Index 1: Luka
Index 2: true
3. Use built-in array method
The Array object in Javascript defines many methods. The following figure lists some of the most commonly used methods.
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>learn4Array</title></head><body><script type="text/javascript">var myArray1 = [100,"Luka"];var myArray2 = [true];//concat(<otherArray>)var myArray = myArray1.concat(myArray2);for(var i= 0;i < myArray.length;i++){document.writeln("myArray["+i+"]: "+myArray[i]+"<br />")}document.writeln("<br />");//join(<separator>)var strArray = myArray.join('-');document.writeln(strArray+"<br />");document.writeln("<br />");//pop()myArray.pop();document.writeln(myArray+"<br />");document.writeln("<br />");//push(<item>)myArray.push("Hello Javascript",200);document.writeln(myArray+"<br />");document.writeln("<br />");//reverse()myArray = myArray.reverse();document.writeln(myArray+"<br />");document.writeln("<br />");//slice(<start>,<end>)document.writeln( myArray.slice(1,3)+"<br />");document.writeln( myArray.slice(0)+"<br />");document.writeln("<br />");//sort()function sortNumber(a,b){return b - a;}var myArray3 = ["George","John","Thomas","James","Adrew","Martin"];var myArray4 = [10,5,40,25,1000,1];document.writeln( myArray3.sort()+"<br />");document.writeln( myArray4.sort()+"<br />");//Use a sort function to sort numbers by the size of the value document.writeln( myArray4.sort(sortNumber)+"<br />");document.writeln("<br />");//unshift(<item>)myArray.unshift(1,2);document.writeln(myArray+"<br />");</script></body></html>Output result:
myArray[0]: 100
myArray[1]: Luka
myArray[2]: true
100-Luka-true
100,Luka
100, Luka, Hello Javascript, 200
200, Hello Javascript, Luka, 100
Hello Javascript, Luka, 100
Luka,100
Hello Javascript, Luka, 100
Adrew, George, James, John, Martin, Thomas
1,10,1000,25,40,5
1000,40,25,10,5,1
1,2, Hello Javascript, Luka,100
The above content is the use of Javascript basic arrays introduced to you by the editor. I hope it will be helpful to you!