UrbanPro

Learn Java Script Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Why do people prefer TypeScript with most of the JavaScript frameworks?

Asked by Last Modified  

Follow 1
Answer

Please enter your answer

IT Professional Trainer with 15 years of experience in IT Industry

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(...
read more


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 less
Comments

Related Questions

Is it worth learning Node.js? I feel like I should learn TypeScript as well.
Yes, both are good, TypeScript will have bright future
Shubham
0 0
5
How Java is different from Java script?
Java is server side language is used to make application. Java is need to compile. The end user can not see the code of java it is secured. But Javascript is script language is a open source can be viewed by end user in browser.
Samadhan
What's the event loop?
In computer science, the event loop is a programming construct that waits for and dispatches events or messages in a program. It works by making a request to some internal or external "event provider"....
Spandana
0 0
5
Which language has the best future prospects: Python, Java, or JavaScript?
According to me, "Python" is the programming language having the best future prospect considering the current industry trend. Reason: Python is extensively used in advanced technologies such as Machine...
Atharva
0 0
5

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

What Is Mean Stack? Why Mean Stack Is Popular Now?
Mean Stack is standing for MongoDB, ExpressJS, AngularJS and NodeJS. These four javascript solutions create a high performing web application. Mainly JavaScript is the frontend language. It’s working...

Dependency Injection in Angular 2
Dependency injection Dependency injection is a way to supply a new instance of a class with the fully-formed dependencies it requires. Most dependencies are services. Angular uses dependency injection...

Advantage of Node.js over Angularjs
i. Nodejs helps a user to develop scalable network applications. It delivers efficiency along with with event-driven, non-blocking I/O model, which has been proved ideal for real-time data-intensive applications....

Mark And Sweep Algorithm in Javascript Memory Management
This algorithm reduces the definition of "an object is not needed anymore" to "an object is unreachable". This algorithm assumes the knowledge of a set of objects called roots (In JavaScript, the root...

JavaScript Arrow Function
Javascript Arrow Functions : JavaScript ES6 is a type or version which let us provide a way to write javascript syntax in a better way. ES6 version is having a feature or new method to use functions...
A

Abhay

0 1
0

Recommended Articles

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...

Read full article >

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,...

Read full article >

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...

Read full article >

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...

Read full article >

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 you
X

Looking for Java Script Training Classes?

The best tutors for Java Script Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Script Training with the Best Tutors

The best Tutors for Java Script Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more