Home > Mobile >  Access data from inside a function in the global scope
Access data from inside a function in the global scope

Time:05-21

I'm still learning JS and i'm currently struggeling with this problem. I want to be able to access the data from "ordArray" in the global scope. How do i do this? I've tried with returns but struggleing to solve it.

    const startOrd = function(){
   const ordArray = [];
    val = document.getElementById("val_ord").value;
    ordArray.push(val)
    console.log(ordArray)
    
}

CodePudding user response:

Simply create ordArray outside of the function :)

const ordArray = [];
const startOrd = function () {
  val = document.getElementById("val_ord").value;
  ordArray.push(val)
  console.log(ordArray)

}

CodePudding user response:

Declare/initialize the array outside of the function.

const ordArray = [];

const startOrd = function() {
    val = document.getElementById("val_ord").value;
    ordArray.push(val)
    console.log(ordArray)
}
  • Related