Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) Programming AJAX Parsing JSON Data

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

if i use let instead of var here , are there any issues i should be aware of?

let xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(){
    if(xhr.readyState === 4){
        let employees = JSON.parse(xhr.responseText);

    }

};

2 Answers

Steven Parker
Steven Parker
229,732 Points

Just be aware that "let" (or "const") has block scope, which means it will only be available inside the conditional block. But if you don't need to access it after the conditional that's a good choice.

And it doesn't apply here, but you should bear in mind that declarations using "var" are hoisted, but those with "let" or "const" are not.

Teacher Russell
Teacher Russell
16,873 Points

Here's how I did it. https://w.trhou.se/s1hxglfs0b

I would use const instead of let, any time you can. That's just me, and I'm pretty new at all of this. I don't see a reason to use let unless it's really necessary. Of course, I'm a hobbyist, one year in, and I only discovered ES6 very recently. Just a note, I enjoy Dave's courses, and though there's a lot out of date here, it's fun, and Iove to know how we got to where we are now. I recommend taking this course. So much to learn here.

Steven Parker
Steven Parker
229,732 Points

If a value will not be changed, using "const" is the recommended "best practice".