Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
Why is code giving error if I try to access the array returned by str.split(), immediately in next line

Why does it happen that the below code throws the error Uncaught ReferenceError: Cannot access 'arr' before initialization

function swap(str,a,b){
    let arr = str.split("")
    [arr[a],arr[b]] = [arr[b],arr[a]]
    return arr.join("")
}
swap("abcde",2,4) // throws error "Uncaught ReferenceError: Cannot access 'arr' before initialization"

But as soon as I insert any dummy statement between line 2 and 3, surprisingly code starts to work

function swap(str,a,b){
    let arr = str.split("")
    23456; // this could be anything like a variable declaration, or any valid javascript statement
    [arr[a],arr[b]] = [arr[b],arr[a]]
    return arr.join("")
}
swap("abcde",2,4) // returns "abedc" as expected

I am surprised to see why does this even work now ? It should have given error in both the cases, or should have worked in both cases. I'm sure that in the 1st case, split is not finishing it's work till the time line 3 is executed and in 2nd case, it is getting enough time because of 1 extra statement in between. But I would still love to get a clear explanation as to why it is behaving like this. I'm not sure if this is same for all browsers, but I've tried it on Chrome and Safari on mac, and both of them gave same error.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The semicolon is making the difference. JavaScript thinks you want to access the result of str.split("")[arr[a],arr[b]] like an array. This is why JavaScript autoformatters will insert a semicolon at the beginning of the next line like this:

function swap(str,a,b){
    let arr = str.split("")
    ;[arr[a],arr[b]] = [arr[b],arr[a]]  // <--- semicolon in front of this line
    return arr.join("")
}
about 4 years ago · Juan Pablo Isaza Report

0

I think your syntax is off a bit split will return an array and trailing it with brackets is translated as if you are trying to access an element of the array I believe you are missing the semi colon only

let arr = str.split("")[arr[a],arr[b]] = [arr[b],arr[a]]

function swap(str,a,b){
    let arr = str.split("");
    [arr[a],arr[b]] = [arr[b],arr[a]]
    return arr.join("")
}

console.log(swap('some tpyo', 6, 7))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!