-2

i want to access const newdata outside a function

    function updateFields(response) {
        $("#FIELDSET").removeProp('disabled');

        const parser = new DOMParser();
        const xmlDoc = parser.parseFromString(response, "text/xml");
        const data = xmlDoc.getElementsByTagName("responses")[0].innerHTML;
        const rdata = data.split('//')[0]
        const payload = data.split(':1:1,')
            .map(x => x.split('='))
            .reduce((obj, x) => {
                obj[x[0].replace(/\./g, '_')] = x[1];
                return obj;
            }, {})

        const newdat="string";


        CO_CODE=payload.CO_CODE.split(':1:1')[0]
        const {AMOUNT, INT_RATE, TERM, FREQUENCY, REP_START_DATE, CUSTOMER_ID, INSTALMENT} = payload;

    }
Taplar
  • 24,788
  • 4
  • 22
  • 35
  • 11
    `const` are block scoped. You cannot access it outside the function. Either raise the scope of the variable to a higher scope, or `return newdat` at the end of the function – Taplar Jun 13 '19 at 17:11
  • 3
    You can declare `newdat` as `let` outside your function scope, then set value inside function – noitse Jun 13 '19 at 17:12
  • And you should not. Your code is mixing all kinds of responsibilities together. Create a data structure and have a logic function to do the work and expose that – unional Jun 13 '19 at 17:12
  • lets say i add a variable like var dom="123456"; how do i access it outside the function. thanks – Engr Ebuka Orioha Jun 13 '19 at 17:13
  • `var` are function scoped. You also cannot access those outside the function. Same solution applies – Taplar Jun 13 '19 at 17:13
  • I feel like this an event handler/callback so he cannot return a value out of it nor hoist it out as the callback runs asynchronous to his other code. – Avin Kavish Jun 13 '19 at 17:14
  • Proposed duplicate: https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Taplar Jun 13 '19 at 17:14

3 Answers3

3

You could assign the value to a variable declared outside of your function, but since your const newdat is scoped within the function, you won't be able to access it outside of it.

Something like this:

let newdat;
function updateFields(response) {
    $("#FIELDSET").removeProp('disabled');

    const parser = new DOMParser();
    const xmlDoc = parser.parseFromString(response, "text/xml");
    const data = xmlDoc.getElementsByTagName("responses")[0].innerHTML;
    const rdata = data.split('//')[0]
    const payload = data.split(':1:1,')
        .map(x => x.split('='))
        .reduce((obj, x) => {
            obj[x[0].replace(/\./g, '_')] = x[1];
            return obj;
        }, {})

    newdat="string";


    CO_CODE=payload.CO_CODE.split(':1:1')[0]
    const {AMOUNT, INT_RATE, TERM, FREQUENCY, REP_START_DATE, CUSTOMER_ID, INSTALMENT} = payload;

}

Though be mindful that in this case, since the variable declaration is left undefined, you might want to test it before using it OR define it to a default value, like an empty string, when declaring it.

  • Your answer does not contain a suggested solution, so it is not necessary. Use comments instead... //edit: NOW it contains :) – Toxi Jun 13 '19 at 17:15
  • @Toxi This is pretty much a solution, it suggests what to do in the first part. – tadman Jun 13 '19 at 17:17
  • I'm wondering why, given how it doesn't change, that it can't be `const newdat = "string"` outside. – tadman Jun 13 '19 at 17:18
  • I added the code to represent what I meant by that. Is this better? Otherwise, I'll simply delete the answer. – Sebastien Dufresne Jun 13 '19 at 17:18
  • If you declare it outside with const, you won't know what you'll want to assign to it within the function. When the function tries to assign something to it, it won't let you. You can declare object structures with const and modify the value of their properties, but declaring a string with const prevents the variable binding from referencing a different value. – Sebastien Dufresne Jun 13 '19 at 17:23
2
let newdat;
function updateFields(response) {
  $("#FIELDSET").removeProp('disabled');

  const parser = new DOMParser();
  const xmlDoc = parser.parseFromString(response, "text/xml");
  const data = xmlDoc.getElementsByTagName("responses")[0].innerHTML;
  const rdata = data.split('//')[0]
  const payload = data.split(':1:1,')
      .map(x => x.split('='))
      .reduce((obj, x) => {
          obj[x[0].replace(/\./g, '_')] = x[1];
          return obj;
      }, {})

  newdat="string";


  CO_CODE=payload.CO_CODE.split(':1:1')[0]
  const {AMOUNT, INT_RATE, TERM, FREQUENCY, REP_START_DATE, CUSTOMER_ID, INSTALMENT} = payload;

}
/* run the function */
console.log(newdat); // string
Waldir Bolanos
  • 422
  • 3
  • 7
0

If newdat is not specific to — or specifically dependent upon — your updateFields method, you could maintain it outside of updateFields scope; updating it whenever updateFields is called.

let newdat = '';
function updateFields() {
    // ...
    newdat = "string";
    // ...
}

function someOtherFunction() {
    console.log( 'newdat =' + newdat ); // would log the current value of `newdat` when called
}

let newdat = '';

function updateFields() {
  newdat = "string";
}

function someOtherFunction() {
  console.log('newdat = ' + newdat); // would log the current value of `newdat` when called
}

updateFields();
someOtherFunction();
Ito Pizarro
  • 1,607
  • 1
  • 11
  • 21