Data typing

Every programming language has data types. They only vary in the number of types available, and the values (and range of values, for number types) that the typed variable can hold. I won't wade into the philosophical debate between strongly typed versus statically typed versus loosely typed languages (usually referred to as static versus dynamic typing) in this book—but since this chapter is devoted to JavaScript and TypeScript, I do need to say a couple of quick things about their typing. 

JavaScript is a loosely typed language—which is to say it is a dynamic language as opposed to it being a static language. What this means is that variables in JavaScript are not bound to any particular type, but rather their values are associated with a type. Variables can be assigned, and re-assigned, to values of all available types. While convenient, hard-to-find bugs can occur, since there is no compiler that checks for adherence of value to a typed reference—and this is because when you declare a variable using var, let, or const, you do not specify an associated type.

In contrast, TypeScript is optionally statically typed. The important word here is optionally. TypeScript is a statically typed language, yes, but it does not force you to explicitly annotate your variable with the intended type. This is because TypeScript has what is called type inference, which is to say that the TypeScript runtime will infer the variables data type at runtime. This is the default behavior of TypeScript. Now, this is where the optional part of it comes… if you want to strictly type the variable, thereby binding the datatype to the variable instead of it resting with the variable's value, you have to explicitly add a type annotation to the variable declaration.

Here it is in code:

var num: number = 12345; // this is a statically typed variable
var num = 12345; // this is a dynamically typed variable

Both the preceding lines are valid TypeScript syntax, but here are their differences:

  • The first line, the statically typed variable, which is annotated with the num keyword, is checked by the TypeScript transpiler, and any issues will be reported by it
  • The second line, where the variable declaration is made in the JavaScript fashion (that is, no annotations for static typing), goes unchecked, and any issues will only be found at runtime

ES6 has seven data types, six of which are known as primitive data types, and one of which is a reference data type (which is just called Object). JavaScript also has several built-in data types in its standard library, but since this is not comprehensive coverage of JavaScript, we'll only cover a few of them here: the ones that you're likely to use in your Angular development.

The following is the list of the primitive data types provided:

  • Null
  • Undefined
  • Boolean
  • Number 
  • String 
  • Symbol

The following are the built-in data types provided:

  • Dates
  • Arrays
  • Maps
  • Sets
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.203.134