formations/javascript/base/tableaux-1.md

31 lines
677 B
Markdown
Raw Normal View History

2015-03-25 17:27:03 +01:00
# .cadoles-slide-title[Tableaux (1/2)]
.cadoles-clearfix[
**Différentes notations**
]
```js
var arr = [1, 2, 3]; // A préférer
arr = new Array(1, 2, 3);
// Attention !
arr = new Array(3); // -> [undefined, undefined, undefined]
```
**Manipulation d'un tableau**
```js
var arr = [1, 2, 3];
// Propriétés
arr.length // -> 3
arr[0] // -> 1
// Méthodes (liste non exhaustive)
arr.push(4); // -> [1, 2, 3, 4]
arr.pop(); // -> [1, 2, 3]
arr.unshift(0); // -> [0, 1, 2, 3]
arr.shift(); // -> [1, 2, 3]
arr.indexOf(2) // -> 1
arr = [2, 3, 1];
arr.sort(); // -> [1, 2, 3]
arr.join(', '); // -> '1, 2, 3'
```