17

What does Array.find method returns value some specifical copy of a found value or the reference from the array. I mean what it returns value or reference of the matched element from the given array.

MD Jahid Hasan
  • 786
  • 1
  • 9
  • 19
  • 5
    You mean `array.find`? – ibrahim mahrir Jun 21 '20 at 10:18
  • yes and i corrected by post. – MD Jahid Hasan Jun 21 '20 at 10:34
  • What specifically do you mean by "copy"? And why are you wondering this specifically for `.find()`? Your question looks a duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language), but I'm not fully convinced. – Ivar Jun 21 '20 at 10:35
  • new instance which does not related to the array which it belongs. Pass by value mainly – MD Jahid Hasan Jun 21 '20 at 10:37
  • 1
    @MDJahidHasan JavaScript is pass-by-value, so you will _always_ get a value. But that value could be a reference to an object. But that is completely unrelated to the `Array.prototype.find()` function. That's just how JavaScript works. See the link I put in my previous comment. – Ivar Jun 21 '20 at 10:40

4 Answers4

28

From MDN (emphasis theirs):

The find() method returns the value of the first element in the provided array that satisfies the provided testing function.

Whether it returns a copy of or a reference to the value will follow normal JavaScript behaviour, i.e. it'll be a copy if it's a primitive, or a reference if it's a complex type.

let foo = ['a', {bar: 1}];
let a = foo.find(val => val === 'a');
a = 'b';
console.log(foo[0]); //still "a"
let obj = foo.find(val => val.bar);
obj.bar = 2;
console.log(foo[1].bar); //2 - reference
Mitya
  • 33,629
  • 9
  • 60
  • 107
5

That's a tricky question.

Technically speaking, find always returns a value, but that value could be a reference if the item you're looking for is an object. It will still be a value nonetheless.

It's similar to what's happening here:

let a = { some: "object" };

let b = a;

You are copying the value of the variable a into b. It just so happens that the value is a reference to the object { some: "object" }.

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

The find() method returns the value of the first element in the provided array that satisfies the provided testing function.

const obj = {}

console.log(obj.find)

const arr = ['a', 'b', 'c']

console.log(arr.find(e => e === 'a'))
console.log(arr.find(e => e ==='c'))
ebyte
  • 1,469
  • 2
  • 17
  • 32
0

Returns Value

The find() method returns the value of the first element in an array that pass a test (provided as a function).

The find() method executes the function once for each element present in the array:

If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values) Otherwise, it returns undefined

Click Here

Merrin K
  • 1,602
  • 1
  • 16
  • 27