The task is to perform unshift operation without using the unshift() method with the help of jQuery. There are two approaches that are discussed below:
Approach 1: We can use the Array concat() method which is used to join two or more arrays. Just pass the newElement as the arrays of size 1 and the rest of the array.
- Example:
<!DOCTYPE HTML><html>ÂÂ<head>   Â<title>       ÂHow to perform the unshift() operation       Âwithout using it in JavaScript   Â</title></head>ÂÂ<bodystyle="text-align:center;">   Â<h1style="color:green;">           Âneveropen   Â</h1>   Â<pid="GFG_UP">   Â</p>   Â<buttononclick="myGFG()">       ÂClick Here   Â</button>   Â<pid="GFG_DOWN">   Â</p>   Â<script>       Âvar array = ['Geeks', 'GFG', 'Geek', 'neveropen'];       Âvar up = document.getElementById("GFG_UP");       Âup.innerHTML = "Array = [" + array + "]";       Âvar down = document.getElementById("GFG_DOWN");       Âfunction myGFG() {           Âvar newElement = 'gfg';           ÂnewArray = [newElement].concat(array);           Âdown.innerHTML = "Elements of array = ["                             Â+ newArray + "]";       Â}   Â</script></body>ÂÂ</html> -
Output:
Approach 2: We can use the ES6 spread operator to perform the operation.
- Example:
<!DOCTYPE HTML><html>ÂÂ<head>   Â<title>       ÂHow to perform the unshift() operation without       Âusing it in JavaScript   Â</title></head>ÂÂ<bodystyle="text-align:center;">   Â<h1style="color:green;">           Âneveropen   Â</h1>   Â<pid="GFG_UP">   Â</p>   Â<buttononclick="myGFG()">       ÂClick Here   Â</button>   Â<pid="GFG_DOWN">   Â</p>   Â<script>       Âvar array = ['Geeks', 'GFG', 'Geek', 'neveropen'];       Âvar up = document.getElementById("GFG_UP");       Âup.innerHTML = "Array = [" + array + "]";       Âvar down = document.getElementById("GFG_DOWN");       Âfunction myGFG() {           Âvar newElement = 'gfg';           ÂnewArray = [newElement, ...array];           Âdown.innerHTML = "elements of array = ["                           Â+ newArray + "]";       Â}   Â</script></body>ÂÂ</html> -
Output:

