JavaScript AJAX Basics (retiring) Programming AJAX Parsing JSON Data

Saqib Ishfaq
Saqib Ishfaq
13,911 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
174,115 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,868 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
174,115 Points

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