Pet Bed WareHouse

Best Friends by Sheri The Original Calming Donut Cat and Dog Bed in Shag or Lux Fur, Machine Washable, High Bolster, Multiple Sizes S-XL Size18-InchMaterialPolyesterBrandMidWest Homes for…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Enforcing Data Types in JavaScript

I have been spending a LOT of time with JavaScript recently, and I’ve noticed that JS is super chill. It doesn’t ever ask me what data type my variable is going to have. How will it know how to run my program? That seems like a pretty big oversight!

JavaScript is a weakly typed language…

Compare that with Java, an example of a strongly typed language…

Java knows right away if you’re making a mistake by not assigning it an acceptable value. Java also knows when you’re trying to use a method or function on a variable with the wrong type, and gives you errors. It knows all that before you even need to run the code!

The good and bad thing about JavaScript is that I don’t have to spend much time thinking and planning my code based on data types. (That’s something for future Linda to worry about!) As I write increasingly complex programs and working with API data, the more I realize that the flexibility of JS types can be a double-edged sword. It’s easier to learn but sometimes I wish it was a bit stricter.

Does JavaScript know what type my variable is?

This way, the only way to find a bug is to execute the exact line the method is on. Most of the time it works well, but sometimes we get unpredictable results.

Oh no!

Don’t worry, there are ways of enforcing data types!

Advantages? The program will know at compile time whether something works or not and warn you! Less time spent debugging if you write less bugs into the program!

Using TypeScript makes the code more readable. You are reminded of how to use it AS you’re using it. Compare this with combing through dense or poorly-written documentation which is most likely out of date.

2. Using React? Try PropTypes!

The first two options are great for complex projects. But sometimes you don’t need the additional code and mental load….

3. Runtime Typechecker

A screenshot from the tern.js readme showing a pre-runtime error message generated after type inference

4. Nothing wrong with vanilla 🍨

Takeaways!

Start thinking about data types! With all these type checking options available to you, can you think of some places they might help you avoid type errors?

Resources I used and recommend:

Add a comment

Related posts:

Quick History Of World War Two European Theater

Some would say that World War Two began with Germany’s Invasion of Poland on the 1st September 1939 and the ultimatum of Britain that without a German withdrawal a State of War would exist. Needless…

What is Data Centric Design?

What is Data Centric Design?. By Yoon Auh, Founder of NUTS Technologies, Inc., the world leader in Data Centric Designs of secure data systems. Visit blognuts.org.

Best Site for Live Crypto Prices

Would you like to find out the best and most accurate website for tracking the real-time price of Bitcoin, Ethereum, and thousands of other cryptocurrencies? To know the websites to check live crypto…