How to handle an “undefined” error while converting CSV file to JSON using Papa Parse framework?

So, this is my JS code:

function main(){
    let myJSON = parseCSV();
    let myCSV = transformCSV(myJSON);

function parseCSV(){
    let parsedJSON;
    let selectedFile = document.getElementById('fileIn').files[0];
    Papa.parse(selectedFile, {
        complete: function(results) {
            parsedJSON =;
    return parsedJSON;

function transformCSV(JSONIn){
    let csvOut = ""; // i will do something here later
    let dCol = ""; // i will do something here later
    let dRow = ""; // i will do something here later
    for (let i = 0; i < JSONIn.length - 1; i++) {
        // i will do something here later
    return csvOut;

And this is my test html page:



When I try to read length of myJSON, I get error message in Chrome console: Uncaught TypeError: Cannot read property 'length' of undefined. Why is it undefined? It is present in console! Why does this happen and how to fix it? How to work with resulted myJSON as a perfectly normal static JSON?