JavaScript doesn't enforce types, and so you are allowed to do this. It might seem like JavaScript is more flexible because of this, but in practice this can be a headache, as it allows this even if it's a mistake. JavaScript will just carry on running your code with unexpected types and it may not fail until later on - and worse, it may fail in a strange way, such as producing the wrong result, or showing a confusing error message. TypeScript will report an error if you do this though, and Construct won't let you preview your project until you fix it. This can help make sure your code is correct and catch coding errors more quickly.
It's also worth mentioning other programming languages have different types for numbers, such as representing integers and floating point (fractional) numbers with separate data types. JavaScript does not distinguish these types though, and so neither does TypeScript. There's only one number type, which is a floating point number type.
Conclusion
In this part we've covered;
- Statements
- Comments
- Variables and types
- Assignment and constants
- Numbers, strings and boolean data types
- The
typeof
operator
- How TypeScript and JavaScript are related
Hopefully by now you're getting familiar with entering code in both Construct and the browser console. The guide will continue to use both. In the next part, we'll learn more about operators, booleans, comparisons, and converting types.
Learn more
At the end of each part of this guide we'll include links to learn more about the various TypeScript and JavaScript features mentioned. We'll refer to the MDN Web Docs for JavaScript features which TypeScript merely inherits, or the official TypeScript website for TypeScript-specific features. You can skip over this and just carry on with the guide if you want, but if you're comfortable with what you've learned and want to dig deeper with a more complete reference or dig in to more advanced details, you can use these links to learn more.