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

I know HTML, CSS, and a bit of JavaScript. What should I learn next?
HTML,CSS and Javascript are Tools which are used for Front-End Web Development. The next step is to learn the following: 1)Learn Javascript Frameworks like node.js,react.js,angular.js etc. 2)Learn Back-End...
Inch By
How do I become better at JavaScript?
Go through JavaScript tutorials from w3schools.com site. They have good examples with description. Then learn some es6 and es7 syntax. You will be pro in JavaScript.
Pooja
0 0
5
x eskor plus two x plus one
what is the question? can't understand the meaning.
Ankur
0 0
8
How to learn Javascript?
I would say, https://developer.mozilla.org/en-US/docs/Learn/JavaScript is the best place to learn if you want real good knowledge in Javascript. Have a little bit patience and put some effort in learning...
Ramya

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

Ask a Question

Related Lessons

String Comparison In Javascript
String Comparison in javascript: Let us learn how two strings are compared in javascript Assume you have two strings: "ant" and "ele" Case 1: alert("ant" < "ele" ); will give the result as true...
G

Closures in Javascript
A closure is the combination of a function and the lexical environment within which that function was declared. Consider the following example: function makeFunc() { var name = 'Mozilla'; function...

How can everyone prepare to clear any Java interview?
Java interview your java should be much strong then J2EE. core java and Advance java is the basic foundation for Interview. Some of the topic about which you should know before going for a java interview...

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

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

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