-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
77 lines (54 loc) · 2.15 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
title: TypedArray.prototype.set()
slug: Web/JavaScript/Reference/Global_Objects/TypedArray/set
page-type: javascript-instance-method
browser-compat: javascript.builtins.TypedArray.set
---
{{JSRef}}
The **`set()`** method of {{jsxref("TypedArray")}} instances stores multiple values in the typed
array, reading input values from a specified array.
{{EmbedInteractiveExample("pages/js/typedarray-set.html")}}
## Syntax
```js-nolint
set(array)
set(array, targetOffset)
set(typedarray)
set(typedarray, targetOffset)
```
### Parameters
- `array`
- : The array from which to copy values. All values from the source array are copied
into the target array, unless the length of the source array plus the target offset exceeds
the length of the target array, in which case an exception is thrown.
- `typedarray`
- : If the source array is a typed array, the two arrays may share the same underlying
{{jsxref("ArrayBuffer")}}; the JavaScript engine will intelligently
**copy** the source range of the buffer to the destination range.
- `targetOffset` {{optional_inline}}
- : The offset into the target array at which to begin writing values from the source
array. If this value is omitted, 0 is assumed (that is, the source array will
overwrite values in the target array starting at index 0).
### Return value
None ({{jsxref("undefined")}}).
### Exceptions
- {{jsxref("RangeError")}}
- : Thrown in one of the following cases:
- An element will be stored beyond the end of the typed array, either because `targetOffset` is too large or because `array` or `typedarray` is too large.
- `targetOffset` is negative.
## Examples
### Using set()
```js
const buffer = new ArrayBuffer(8);
const uint8 = new Uint8Array(buffer);
uint8.set([1, 2, 3], 3);
console.log(uint8); // Uint8Array [ 0, 0, 0, 1, 2, 3, 0, 0 ]
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Polyfill of `TypedArray.prototype.set` in `core-js`](https://github.com/zloirock/core-js#ecmascript-typed-arrays)
- [JavaScript typed arrays](/en-US/docs/Web/JavaScript/Guide/Typed_arrays) guide
- {{jsxref("TypedArray")}}
- {{jsxref("ArrayBuffer")}}