This dynamically typed behavior is one of the main value propostions of TypeScript, a JavaScript superset that supports static typing and is eventually transpiled into JavaScript. This can seem like a trivial issue to detect in this case, but in projects spanning hundreds or thousands of lines it can result in odd bugs that are difficult to diagnose. Because the last x assignment is a string - x = "1" - the x + y operation does not result in a mathematical operation, but rather a string concatenation, therefore x + y results in 12, not in 3. Although this cuts down on the amount of typing, notice the last operation x + y that involves the y = 2 variable. JavaScript dynamically typed behaviorĪs you can see in listing 4-1, the x variable is first interpreted as a number, then as an array and finally as a string. Listing 4-1 illustrates JavaScript's dynamically typed behavior. Although this creates a much quicker development process, it also introduces subtle data type bugs, that would otherwise be easily detectable in statically typed languages (e.g. This means data types get determined automatically at run-time and it isn't necessary to declare variable data types. JavaScript was conceived as a dynamically typed language. Dynamically typed behavior and data types In this chapter you'll learn about JavaScript data types and their dynamically typed behavior, how there are only a few core JavaScript data types, how these JavaScript data types have evolved over time, as well as how more sophisticated JavaScript data types have been incorporated into later ECMAScript versions to support more advanced constructs. Similar to JavaScript key concepts & concepts explored in the previous chapter, JavaScript data types have evolved considerably throughout the various ECMAScript versions. Although JavaScript data types can be a rather dry subject, the topic holds an important key to making better JavaScript design choices, which includes discovering techniques to achieve results with faster execution times and less typing. JavaScript data types are an essential piece to understanding modern JavaScript.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |