--- title: Array.prototype.unshift() slug: Web/JavaScript/Reference/Global_Objects/Array/unshift translation_of: Web/JavaScript/Reference/Global_Objects/Array/unshift ---
{{JSRef}}
המתודה ()unshift  מוסיפה אלמנט אחד או יותר לתחילת מערך ומחזירה את גודלו החדש של המערך. 
{{EmbedInteractiveExample("pages/js/array-unshift.html")}}

Syntax

arr.unshift(element1[, ...[, elementN]])

פרמטרים

elementN
אלמנטים להוספה לתחילת המערך.

ערך מוחזר

את ערך השדה {{jsxref("Array.length", "length")}} החדש של המערך עליו הופעלה המתודה.

תאור

המתודה unshift מכניסה את ערכי האלמנטים שקיבלה כפרמטר לתחילת אובייקט המערך. 

unshift is intentionally generic; this method can be {{jsxref("Function.call", "called", "", 1)}} or {{jsxref("Function.apply", "applied", "", 1)}} to objects resembling arrays. Objects which do not contain a length property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.

Examples

var arr = [1, 2];

arr.unshift(0); // result of call is 3, the new array length
// arr is [0, 1, 2]

arr.unshift(-2, -1); // = 5
// arr is [-2, -1, 0, 1, 2]

arr.unshift([-3]);
// arr is [[-3], -2, -1, 0, 1, 2]

Specifications

Specification Status Comment
{{SpecName('ES3')}} {{Spec2('ES3')}} Initial definition. Implemented in JavaScript 1.2.
{{SpecName('ES5.1', '#sec-15.4.4.13', 'Array.prototype.unshift')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-array.prototype.unshift', 'Array.prototype.unshift')}} {{Spec2('ES6')}}  
{{SpecName('ESDraft', '#sec-array.prototype.unshift', 'Array.prototype.unshift')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{Compat("javascript.builtins.Array.unshift")}}

See also