0

I am trying to reset an array through a function parameter.

Here is my code:

<script>
fruitArray = ["apple", "banana", "orange", "pineapple"]
function newFruit(myArray){
    myArray=[];
}
newFruit(fruitArray)
alert(fruitArray[0])//Returns "apple"
</script>

I want to clear the array, but it is not working through the function. I am not sure about the line:

myArray=[];

Please help me learning this.

Thank you.

user3378165
  • 6,546
  • 17
  • 62
  • 101
Steven
  • 15
  • 4

4 Answers4

0

Do like this. Set the length to 0

myArray.length = 0
brk
  • 48,835
  • 10
  • 56
  • 78
0

You don't set your array with the change you made, you send to the newFruit function an array, you change it but this has nothing to do with your fruitArray.

So all you have to do is to return the changed array from the function and to set your fruitArray with the array returned from the function, as follow:

    fruitArray = ["apple", "banana", "orange", "pineapple"]
    function newFruit(myArray){
       myArray = [];
       return myArray;
    }
    fruitArray = newFruit(fruitArray);
    alert(fruitArray[0]);//Returns "undefined" because there is no a [0] place in the fruitArray.
    alert(fruitArray);//Returns ""-empty array.

And by the way your code is missing ;

You can also clear an array this way:

fruitArray.length = 0
user3378165
  • 6,546
  • 17
  • 62
  • 101
0

user3378165 gave the correct answer:

<script>
fruitArray = ["apple", "banana", "orange", "pineapple"];
function newFruit(myArray){
  myArray.length = 0;
}
newFruit(fruitArray);
alert(fruitArray[0]);//Returns "apple"
</script>

Actually, the question is by value topic, you can learn more here: In practical terms, this means that if you change the parameter itself (as you did), that won't affect the item that was fed into the parameter. But if you change the INTERNALS of the parameter(array.length = 0), that will propagate back up

Community
  • 1
  • 1
CoderLim
  • 1,222
  • 3
  • 11
  • 23
-1

There is no concept of pass by reference in javascript. You can pass object literals by reference to a function.

fruitObj = new Object;
fruitObj.fruits = ["apple", "banana", "orange", "pineapple"];
function newFruit(myObj) {
    myObj.fruits = [];
}
newFruit(fruitObj);
alert(newFruit.fruits); // alerts an empty array
lucifer
  • 110
  • 8
  • An array is an object. – zerkms Sep 06 '16 at 05:50
  • @zerkms Thanks for correcting me. All the objects in javascript are objects because of the prototype inheritance. But only the direct instance of Object i.e; objects whose `typeof(obj) == "object"` can be passed by reference to function according to my understanding. Correct me if I am wrong. – lucifer Sep 06 '16 at 06:14
  • Unfortunately you're wrong. "All the objects in javascript are objects because of the prototype inheritance" --- this makes no sense. " i.e; objects whose typeof(obj) == "object"" --- `typeof(null) == 'object'` while `null` is definitely not an object. – zerkms Sep 06 '16 at 08:45
  • Your new wording is also misleading: "You can pass object literals by reference" --- this makes very little sense. Object literal is just a syntactical notation, it still represents an object. It is references to objects that are passed in runtime not references to object literals. – zerkms Sep 06 '16 at 08:46