Get and set array values
To get and set array values, you use square brackets and provide the zero-based numeric index:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var colors = ["A", "B", "C"]; //define an array of strings
document.writeln(colors[0]); //A,display the first item
colors[2] = "black"; //change the third item
document.writeln(colors[2]); //,display the third item
colors[3] = "brown"; //add a fourth item
document.writeln(colors[3]); //display the fourth item
</script>
</head>
<body>
</body>
</html>
The following code changes the first element in the array:
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
var myArray = [ 100, "Adam", true ];
myArray[0] = "Tuesday";
document.writeln("Index 0: " + myArray[0]);
</script>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Array:
- The Array Type
- Array Built-in Methods
- Detecting Arrays
- Get and set array values
- Enumerating the Contents of an Array
- Array Length
- Array join() method
- Array concat()
- Array indexOf()
- Array lastIndexOf()
- Array every()
- Array filter() filters array with the given function.
- Array map()
- Array forEach()
- push() and pop():Array Stack Methods
- push(), shift():Array Queue Methods
- Array reduce()
- Array reduceRight()
- reverse():Reordering array
- Array slice()
- Array some()
- Array splice()
- Array sort()
- toString(), toLocaleString() and valueOf Array
- Array unshift()