3 JavaScript primer, part 1

 

This chapter covers

  • Using the JavaScript types
  • Coercing JavaScript types
  • Defining and using JavaScript functions and arrays
  • Creating and implementing JavaScript objects
  • Understanding the this keyword

Effective TypeScript development requires an understanding of how JavaScript deals with data types. This can be a disappointment to developers who adopt TypeScript because they found JavaScript confusing, but understanding JavaScript makes understanding TypeScript easier and provides valuable insights into what TypeScript offers and how its features work. In this chapter, I introduce the basic JavaScript type features, continuing with more advanced features in chapter 4.

3.1 Preparing for this chapter

To prepare for this chapter, create a folder called primer in a convenient location. Open a command prompt, navigate to the primer folder, and run the command shown in listing 3.1.

Tip

You can download the example project for this chapter—and for all the other chapters in this book—from https://github.com/manningbooks/essential-typescript-5.

Listing 3.1 Preparing the project folder
npm init --yes

To install a package that will automatically execute the JavaScript file when its contents change, run the command shown in listing 3.2 in the primer folder.

Listing 3.2 Installing a package
npm install nodemon@2.0.20

3.2 Getting confused by JavaScript

3.3 Understanding JavaScript types

3.3.1 Working with primitive data types

3.3.2 Understanding type coercion

3.3.3 Working with functions

3.4 Working with arrays

3.4.1 Using the spread operator on arrays

3.4.2 Destructuring arrays

3.5 Working with objects

3.5.1 Adding, changing, and deleting object properties

3.5.2 Using the spread and rest operators on objects

3.5.3 Defining getters and setters

3.5.4 Defining methods