<!DOCTYPE html>
<
html
>
<
body
>
<
center
>
<
h1
style
=
"color: green;"
>
GeeksForGeeks
</
h1
>
<
h2
>use .join() with push and replace together</
h2
>
<
p
>The join() method joins array elements into
a string.</
p
>
<
p
>The splice() method that helps us in order to
replace the items of an existing array by removing and inserting
new elements at the required/desired index.</
p
>
<
p
>The push() method appends a new element to an
array.</
p
>
<
button
onclick
=
"myFunction()"
>Push</
button
>
<
p
id
=
"demo"
></
p
>
<
br
/>
<
script
>
// Initializing the array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
// joining the array elements using .join method
document.getElementById(
"demo").innerHTML = fruits.join(" *");
function myFunction() {
// splicing the array elements(delete_count=2, which will replace
//("Orange", "Apple" with "Lion", "Horse") using splice() method
fruits.splice(1, 2, "Lion", "Horse");
document.getElementById("demo").innerHTML = fruits;
// pushing the array elements("Kiwi, Grapes") using push() method
fruits.push("Kiwi, Grapes");
document.getElementById("demo").innerHTML = fruits;
// expected output [Banana, Lion, Horse, Mango, Kiwi, Grapes]
}
</
script
>
</
center
>
</
body
>
</
html
>