Before reading this article, please go through the following articles:
-
concat() and lastIndexOf() array object method in typescript
-
join() and lastIndexOf() array object method in typescript
-
push() and pop() array object method in typescript
-
reverse() and shift() array object method in typescript
-
slice() and sort() array object method in typescript
-
splice() and toString() array object method in typescript
Introduction
In TypeScript, the array object makes work easier. The TypeScript Array object stores multiple values in a single variable at a time. TypeScript provides many methods.
In this article I am describing the TypeScript Unshift array method.
Unshift() Method
In TypeScript the unshift() method is used to add one or more elements at the beginning of the array.
Syntax
|
array.unshift(value1,value2, ..., valueX) |
values are required parameters. The values to add to the beginning of the array.
Function
Unshift()
{
var fstarry: string[] = ['C','Sharp','Corner','Dot','Net','Heaven','Modeling','Corner'];
fstarry.unshift('MCN','Solution').toString();
var span = document.createElement("span");
span.innerText = "Unshift Method \n After add element, Array is -> " + fstarry + "\n";
document.body.appendChild(span);
}
Complete Program
Unshift_ValueOf.ts
class Unshift
{
Unshift()
{
var fstarry: string[] = ['C','Sharp','Corner','Dot','Net','Heaven','Modeling','Corner'];
fstarry.unshift('MCN','Solution').toString();
var span = document.createElement("span");
span.innerText = "Unshift Method \n After add element, Array is -> " + fstarry + "\n";
document.body.appendChild(span);
}
}
window.onload = () =>
{
var obj = new Unshift();
obj.Unshift();
};
Unshift_ValueOf_MethodDemo.htm
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>TypeScript HTML App</title>
<link rel="stylesheet" href="app.css" type="text/css" />
<script src="Unshift_ValueOf.js"></script>
</head>
<body>
<h3>Unshift() Array Method In TypeScript</h3>
</body>
</html>
Unshift_ValueOf.js
var Unshift = (function () {
function Unshift() { }
Unshift.prototype.Unshift = function () {
var fstarry = [
'C',
'Sharp',
'Corner',
'Dot',
'Net',
'Heaven',
'Modeling',
'Corner'
];
fstarry.unshift('MCN', 'Solution').toString();
var span = document.createElement("span");
span.innerText = "Unshift Method \n After add element, Array is -> " + fstarry + "\n";
document.body.appendChild(span);
};
return Unshift;
})();
window.onload = function () {
var obj = new Unshift();
obj.Unshift();
};
//@ sourceMappingURL=Unshift_ValueOf.js.map
Output
![result.gif]()