JavaScript Arrays
const countries = ["India","United States","Germany"];
Important Points
In JavaScript, arrays aren't primitives but are instead Array objects with the following core characteristics:- JavaScript arrays are resizable and can contain a mix of different data types. Example
- JavaScript arrays are not associative arrays. Hence array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using non-negative integers (or their respective string form) as indexes. Example
- JavaScript arrays are zero-indexed. The first element of an array is at index 0. Example
- JavaScript array-copy operations create shallow copies. Example
const countries = [ "India", "United States", "Germany" ];Example: JavaScript arrays are resizable and can contain a mix of different data types.
const countries = [ "India", "United States", "Germany" ]; console.log(countries); //JavaScript arrays are resizable and can contain a mix of different data types countries.push(123); console.log(countries);Initially array values are of string data type. Later number value is added inside an array without any error. Also array resize automatically while adding number data.
Output:
Example: JavaScript arrays are not associative arrays. Hence array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using non-negative integers (or their respective string form) as indexes.
[ 'India', 'United States', 'Germany' ] [ 'India', 'United States', 'Germany', 123 ]
const countries = [ "India", "United States", "Germany" ]; //JavaScript arrays are not associative arrays. Hence array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using non-negative integers (or their respective string form) as indexes. //non-negative integers console.log(countries[0]); //non-negative integers in string form //Note: 1 and 01 are not same. Only countries['1'] is an actual array index. countries['01'] is an arbitrary string property that will not be visited in array iteration. console.log(countries["1"]); //string index - undefined console.log(countries["India"]); //negative index - undefined console.log(countries[-1]);
Output:
Example: JavaScript arrays are zero-indexed. The first element of an array is at index 0.
India United States undefined undefined
const countries = [ "India", "United States", "Germany" ]; //length of an array is 3 console.log(countries.length); //try to access last value inside an array at index 3 console.log(countries[3]); //try to access last value inside an array at index 2 console.log(countries[2]);At index position equal to length of an array got undefined value. But we found last value at (length of an array - 1) index position.
Output:
Example: JavaScript array-copy operations create shallow copies.
3 undefined Germany
const a = [ "India", "United States", "Germany" ]; const b = a; b[1] = "France"; console.log(a); console.log(b);There are two ways to copy objects: shallow copy and deep copy. Shallow copying creates a new object with references to the same memory locations as the original object, while deep copying creates a new object with new memory locations for all of its properties and nested objects or arrays. Above example is shallow copy.
Output:
[ 'India', 'France', 'Germany' ] [ 'India', 'France', 'Germany' ]