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 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
Some basic program bass of java and how it work?
JavaScript (Lightweight & loosely typed client-side Scripting Language which needs interpreter) is entirely different from Java (Programming Language which needs compilation)
Shekhar

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

Ask a Question

Related Lessons

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

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

Differences Between HashMap vs HashSet In Java.
HashSet HashMap HashSet implements Set interface. HashMap implements Map interface. HashSet stores the data as objects. HashMap stores the data as key-value pairs. HashSet...

NgModules vs. JavaScript modules (Angular 2)
The NgModule — a class decorated with @NgModule Decorator — is a fundamental feature of Angular. JavaScript also has its own module system for managing collections of JavaScript objects. It's...

Web Technology Tutorial
Web Technology has 2 category Front End (HTML,CSS,JS Etc) Back End/Server Side (PHP,JSP,ASP,ROR Etc) Front End technologies are - HTML, CSS, Bootstrap, Javascript, jQuery etc. Using front end...

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