Home >Web Front-end >JS Tutorial >TypeScript for Beginners, Part 2: Basic Data Types
Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript.
JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail.
Like JavaScript, null
in TypeScript means that the value of the variable is empty.
(The descriptions about Infinity
and any
types in the original text here are weakly related to the data type topic and there are logical jumps. It is recommended to delete or rewrite to avoid affecting the consistency of the article.)
Use include() method to check whether a string contains a substring
JavaScript's includes()
method can check whether a string contains another substring. However, this method has no meaning for numeric calls.
Type check of function parameters and return value
clampNumber()
function takes an input number and limits its value between 0 and 255. In the above code, the rgbToHex()
function is called three times. However, due to incorrect parameter type and return value type, the first two calls will cause an error. The code cannot be compiled into JavaScript until these errors are resolved. This type check of function parameters and return values helps us write error-free code, ultimately increasing productivity.
This tutorial describes all available data types in TypeScript. We learned how to assign different types of values to variables, and how TypeScript displays errors. This kind of checking can help you avoid many errors when writing large programs. We also learned how to locate different versions of JavaScript.
If you are looking for other learning resources or resources for work, check out the resources on the Envato market.
The next tutorial will learn about interfaces in TypeScript. If you have any questions about this tutorial, please let me know in the comments.
The above is the detailed content of TypeScript for Beginners, Part 2: Basic Data Types. For more information, please follow other related articles on the PHP Chinese website!