Arrays

Estimated reading: 1 minute 124 views

Arrays are zero-indexed lists of values. They are a handy way to store a set of related items of the same type (such as strings), though in reality, an array can include multiple types of items, including other arrays.

Asimple array

				
					 var myArray = [ ’hello’, ’world’ ];

				
			

Accessing array items by index

				
					var myArray = [ ’hello’, ’world’, ’foo’, ’bar’ ];
 console.log(myArray[3]);
 // logs ’bar’

				
			

Testing the size of an array

				
					var myArray = [ ’hello’, ’world’ ];
 console.log(myArray.length);
 // logs 2
				
			

Changing the value of an array item

				
					var myArray = [ ’hello’, ’world’ ];
 myArray[1] = ’changed’;
				
			

Adding elements to an array

				
					var myArray = [ ’hello’, ’world’ ];
 myArray.push(’new’);
				
			

Working with arrays

				
					var myArray = [ ’h’, ’e’, ’l’, ’l’, ’o’ ];
 var myString = myArray.join(’’); // ’hello’
 var mySplit = myString.split(’’); // [ ’h’, ’e’, ’l’, ’l’, ’o’ ]
				
			
Share this Doc

Arrays

Or copy link