Learn Java Script Training from the Best Tutors
Search in
Absolutely. var behaves differently from let and const, and it’s reasonable to think you may need that behavior. For me, this has become more pertinent with my growing usage of async/await.
Take the following code.
getData(userID)
.then( (userProfile) => {
return userProfile.name }
).catch( (err) => {
log.error(`Hit a snag: ${err}`);
})
This is the standard promise flow. But async functions allow a more recognizable synchronous flow.
let userName = await getUserName(userID);
But wait! There’s not error handling in this! That’s ok, because this is now good, old-fashioned synchronous JavaScript. We can just use a try/catch!
try {
let userName = await getUserName(userID);
} catch(err) {
log.error(`Hit a snag: ${err}`);
}
Only, this won’t work. Both let and const are block-scoped, and a try statement is a block scope. the variable userName won’t be available outside of that try block.
Best practices dictate pre-declaration of variables to the block in which they will be used, like so:
let userName;
try {
userName = await getUserName(userID);
} catch(err) {
log.error(`Hit a snag: ${err}`);
}
I find that ugly, though, especially if you are getting a whole bunch of async variables. I don’t want to write a stack of variables then define the same stack later on. I find it much easier to take advantage of var’s function scoping and both declare and define variables within the try block.
That is only one example that I encounter frequently. Again, because var behaves differently, if you desire that different behavior, use it! Don’t only use let and const because of some bizarre dogma promulgated by people who barely know fizz-buzz.
read lessRelated Questions
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Top 6 Skills Required to Become a Web Designer
Here are the top 6 must have skills for every Web Designer : Technical Skills Web designers undoubtedly require very strong technical skills. They must have hands on using Web designing software and tools, such as Adobe Dreamweaver, Fireworks, Photoshop, Flash etc. In addition to the knowledge of design...
Learn Hadoop and Big Data
Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...
Learn Microsoft Excel
Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...
Top 5 Skills Every Software Developer Must have
Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today. In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...
Looking for Java Script Training classes?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for Java Script Training Classes are on UrbanPro
The best Tutors for Java Script Training Classes are on UrbanPro