dimanche 26 mai 2019

JavaScript: Console Log Value Pushed Into Function's Input Parameter After If Statement--> Reference Error [duplicate]

This question already has an answer here:

I have the following code below:

function test(arr, initialValue) {

  console.log(initialValue)

  if (initialValue === {}) {
    let reverseStr = {};
  } else {
    let reverseStr = '';
  }

  console.log(reverseStr)

}


test(['a', 'b', 'c', 'd'], {})

I am expecting the invoked function to console log {}. Instead, I get ReferenceError: reverseStr is not defined

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire