“Composite” as in “composed”, i.e. made up of smaller pieces.
Numbers and boolean are not composite because we don’t really care about the pieces (bits) that they’re made up of.
[] operatorstr[0] |
first character of string |
arr[0] |
first element of array |
obj['foo'] |
foo property of object |
[] with assignmentarr[0]=42 |
Assign to first element of array |
obj['foo']='whatever' |
Assign to foo property of object |
We can't assign to elements of strings because strings are immutable.
lengthstr.length |
Number of characters in string |
arr.length |
Number of elements in array |
Objects, in general, don’t have a length property
because they are not inherently sequential though it’s perfectly
possible to define an object with a length property.
slicestr.slice(2,3) |
a string containing characters from str |
arr.slice(2,3) |
an array containing elements from arr |
No slice on objects because they are not inherently
sequential.
sliceIndices start at 0.
Second argument can be negative, indicating distance from the end of the string or array.
Second argument can be omitted meaning slice all the way to the end of the string or array.
indexOfstr.indexOf("x") |
The index where "x" first occurs in
str or -1
|
arr.indexOf(value) |
The index where value first appears in
arr or -1
|
No indexOf on objects, again, because they are not
inherently sequential.
| String | "foo", 'bar', "" |
| Array | [10, 20, 30], [] |
| Object | {x: 10, y: 20}, {} |
'foo' + 'bar' ⟹ 'foobar'
'foo'.toUpperCase() ⟹ 'FOO'
'FOO'.toLowerCase() ⟹ 'foo'
arr.push(x) |
Add x at the end of arr |
arr.pop() |
Remove and return value at the end of arr |
Arrays can contain other arrays.
const board = [
['', '', ''],
['', '', ''],
['', '', '']
];
A 3x3 array.
board[0][0] is top left corner.
board[2][2] is bottom right corner.
Dot operator.
When property names follow rules for variable names, can use
. instead of []:
x.foo is equivalent to x['foo']
x.foo = 42 is equivalent to x['foo'] = 42