1.

Display a floating-point pseudo-random number between 0 and 1 in JavaScript

Answer»

Yes, we can add one or more element to the front of an array using the unshift() METHOD.<BR>
Let us first see the syntax of the unshift() method to understand it further:

array.unshift( e1, e2, e3, ..., eN );

Here, e1, e2, e3, … , en are the elements to be added in the front of the array.

We added the FOLLOWING elements to the array:

var arr = [20, 11, 44, 39, 88, 48, 89, 47 ];

Let’s say we need to add a new element in the front of the above array. For that set the element value as a parameter in the unshift method as shown below:

var len = arr.unshift("5");

The following is an example wherein we are adding an element in the beginning of an array:

<!DOCTYPE html> <html> <body> <script> var arr = [20, 11, 44, 39, 88, 48, 89, 47 ]; document.write("Array = "+arr);   // adding element 5 in the front var len = arr.unshift("5"); // displaying the new array document.write("<br>New array = " + arr ); document.write("<br /> Length of the array = " + len ); </script> </body> </html>

The output:

Array = 20,11,44,39,88,48,89,47 New array = 5,20,11,44,39,88,48,89,47 Length of the array = 9


Discussion

No Comment Found