The basic structure to store several elements in a sequence is called
list in Python and Array in JavaScript.
| Python | Javascript |
|---|---|
| Creation of list / array # | |
[3, 2, 10, 'Hello'] |
[ 3, 2, 10, 'Hello' ] |
| Constructor-style creation of list / array # | |
['H', 'e', 'l', 'l', 'o', '!'] |
[ 1, 2, 3, 'Hello' ]
[ 1, 2, 3, 'Hello' ]
[ <4 empty items> ]
[ '4' ]
[ 'Hello!' ] |
| Access by index # | |
3
2
'Hello' |
3
2
Hello |
| String indexes # | |
Exception: TypeError list indices must be integers or slices, not str |
2 In JavaScript, Arrays are Objects, and Objects are indexed by strings. Numeric indexes are coerced into strings. |
| Modification of the value of elements of list / array # | |
[100, 2, 10, 'Hello'] |
[ 100, 2, 10, 'Hello' ] |
| Length of list / array # | |
4 |
4 |
| Appending element to list / array # | |
[3, 2, 10, 'Hello', 5] |
[ 3, 2, 10, 'Hello', 5 ] |
| Adding several elements in list / array # | |
[3, 2, 12, 20] |
[ 3, 2, 12, 20 ] |
| Extending list / array with elements of another list / array # | |
[3, 2, 10, 'Hello', 1, 2, 3] |
[ 3, 2, 10, 'Hello', 1, 2, 3 ]reference
[ 3, 2, 10, 'Hello', 1, 2, 3 ]reference
[ 3, 2, 10, 'Hello', 1, 2, 3 ]reference This is the most efficient way to extend an array in case
the
[ 3, 2, 10, 'Hello', 1, 2, 3 ]reference require: es6 |
| Concatenation # | |
[1, 2, 3, 'Hello', 'World'] |
[ 1, 2, 3, 'Hello', 'World' ] |
| Remove array element by index # | |
[2, 15]
15
2 |
[ 2, <1 empty item>, 15 ]
undefined
3 See also splices. |
| Clear list / array # | |
[] |
[] |
| Get element with index out of range # | |
Exception: IndexError list index out of range |
undefined |
| Add element out of range # | |
Exception: IndexError list assignment index out of range |
[ 1, 2, 10, <2 empty items>, 100 ]
undefined
100 |
| Operator in # | |
True
True
True
False
False Operator
True
False
False
True
False If one creates a list-like dictionary (with numeric keys),
operator |
true
false
false
true
true As Array is a subtype of object (i.e. something dict-like), |
| Slices # | |
[20, 30]
[0, 10]
[20, 30, 40, 50, 60]
[20, 30, 40]
[0, 10, 20, 30, 40, 50, 60]
[20, 40]
[40, 30] |
[ 20, 30 ]
[ 0, 10 ]
[ 20, 30, 40, 50, 60 ]
[ 20, 30, 40 ]
[ 0, 10, 20, 30, 40, 50, 60 ]reference There's no slices with step (third argument in Python's) by default in JavaScript. |
| Splice modifications # | |
[0, 10, 200, 300, 400, 40]
[0, 200, 300, 400, 40]
[0, 200, 300, 999, 400, 40] |
[ 0, 10, 200, 300, 400, 40 ]
[ 20, 30 ]
[ 0, 200, 300, 400, 40 ]
[ 0, 200, 300, 999, 400, 40 ]reference Method |
In JavaScript, one can use
Arrayfunction to create an Array (with or without preceedingnew). If this function receives an integer as the only argument, that integer is interpreted as the length of a newly created Array with empty slots. In other cases (several arguments, one string argument), the arguments become the elements of a newly created Array. Due to inconsistent behaviour of this function and Arrays with empty slots the usage of this method is strongly discouraged, see YDKJS for details.