0

I am writing a very easy function where I am checking the even numbers from an array of integer and adding those even numbers into the new array. But after getting the even numbers from first array when I am trying to push into second array its showing undefined.

const arr = [1,2,3,4,5,6];
const newArr = [];

const loop = () => {
 for (var item of array) {
  if (item % 2 == 0) {
   console.log(item);
   newArr.push(item);
  }
 }
};

console.log(loop());     

Output

2 4 6 undefined

Why new array is showing undefined.

6
  • loop is returning undefined Commented Jun 15, 2022 at 7:06
  • You push to newArr but never do anything to it or print it Commented Jun 15, 2022 at 7:07
  • loop() has no return statement, therefore, it implicitly produces undefined. And when you log the return value of executing the function loop() you see this undefined in the console. Commented Jun 15, 2022 at 7:10
  • arr is defined: check ... newArr is defined: checked ... code iterates over array ... which is nowhere to be seen: check Commented Jun 15, 2022 at 7:10
  • 1
    Oh my bad I forgot to return newArr. Commented Jun 15, 2022 at 7:12

2 Answers 2

1

You can do it simply with forEach.

const arr = [1,2,3,4,5,6];
const newArr = [];


arr.forEach(item => {
  if (item % 2 == 0) {
   newArr.push(item);
  }
})

console.log(newArr);     

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't explain why OP's code wasn't working, nor why your code does work.
0

either return the newArray or execute the loop method and print the new Array.

The reason why you get undefined is because loop is currently a void operator and returning nothing. so if you want the loop method to return the array then the second code sample I showed is the better solution. if you just want to print the array then the first one does the trick.

const arr = [1,2,3,4,5,6];
const newArr = [];


arr.forEach(item => {
  if (item % 2 == 0) {
   newArr.push(item);
  }
})

console.log(newArr); 

or

const arr = [1,2,3,4,5,6];

const loop = () => {
 const newArr = [];
 for (var item of arr) {
  if (item % 2 == 0) {
   console.log(item);
   newArr.push(item);
  }
 }
 return newArr
};
console.log(loop());  

both will work.

5 Comments

in your 2nd solution why have you put loop() inside console.log() even if inside loop() we have an return newArr used.
"because loop is currently a void." You might want to reword that. @Digvijay: What else would you have put in the console.log?
@Digvijay well i don't know exactly what your use case is for the loop method. i just thought it could've been your use case as your question has loop in the console log
@Cerbrus hey, i tried to add some information. if you think I'm missing something, please edit so that I can approve it and maybe learn something aswell.
That's a lot better! Note that users above 2k rep can't "suggest" edits, any more. Edits by those users just get applied :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.