0

I have created a function to add a value to an existing array. When i try to access the new array which is newArr22, it says "Uncaught ReferenceError: newArr22 is not defined". I can access the value when its not inside a function. What am I doing wrong ?

 const initialArr = [
      {name: 'eve'},
      {name: 'john'},
      {name: 'jane'}
    ]

function newarr(){

const newArr22 = initialArr.map(v => ({...v, isActive: true}))

}
Tech Tips
  • 21
  • 3
  • 1
    You're declaring `newArr22` within `newarr`, so it only exists within `newarr`. If you want it to exist outside `newarr`, have `newarr` return it to code that saves it in something at the outer level. – T.J. Crowder Nov 25 '21 at 14:57
  • Related, possible duplicate: [*What is the scope of variables in JavaScript?*](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – T.J. Crowder Nov 25 '21 at 14:58
  • The code shown does not produce the error described. – David Nov 25 '21 at 15:00
  • i am declaring the newArr22 in a function and that function will append the value to the existing array. cant I access it outside the function? – Tech Tips Nov 25 '21 at 15:48

0 Answers0