JavaScript data types and type coercion: What you need to know as a beginner

Oladipupo Ishola
4 min readMar 17, 2023
Photo by Proful Sadangi on Twitter

JavaScript is a dynamically typed language, which means that variables can hold values of different types at different times, as opposed to static-type programming languages like Java, where the type of each and every variable must be known. Furthermore, a variable declared with a specific data type cannot be assigned to another variable of a different data type later on.

As a beginner, it is very important to understand the different data types in JavaScript and how type coercion works, which are essential for writing efficient and bug-free code.

In this short article, we'll look briefly at the various data types in JavaScript and how they're coerced.

Data Types in JavaScript

There are seven primitive data types and one complex data type in JavaScript. The following are the primitive data types:

  1. Number:

This data type is a number that can be either an integer or a floating-point number. For instance, 5 or 3.14.

2. String:

This data type represents a string of characters surrounded by quotes.

For instance, "hello world" or "42."

3. Boolean:

This data type represents either a true or false logical value.

For instance, true or false.

4. Undefined:

This data type represents a variable that has been declared but has yet to be initialized. As an example, suppose let x;

5. Null:

This data type represents the intentional absence of any object value. For example, let x = null;

6. Symbol:

This data type represents a unique identifier. For example, const id = Symbol(“id”);

7. BigInt:

This data type represents integers larger than 2⁵³ — 1. For example, const bigInt = 9007199254740991n;

The complex data type

8. Object:

This data type represents a collection of properties.

For example,

const person = { name: “John”, age: 30 }.

What then is Type Coercion in JavaScript?

The process of converting one data type to another is referred to as "type coercion." JavaScript automatically performs type coercion when comparing values of different types or concatenating strings and numbers. Understanding type coercion is essential for writing bug-free code.

Let us investigate the various types of coercion.

  1. Implicit coercion:

This occurs when JavaScript automatically converts a value from one type to another without the intervention of the programmer.

For example:

const x = 5 + "5";
console.log(x); // "55"

In this example, the number 5 is implicitly coerced into a string and concatenated with the string "5", yielding the string "55."

2. Explicit coercion:

This happens when a programmer uses built-in functions like parseInt(), parseFloat(), and String() to explicitly convert a value from one type to another.

For example:

const x = "42";
const y = parseInt(x);
console.log(y); // 42

In this example, the string "42" is explicitly converted into a number using the parseInt() function.

3. Comparison coercion:

This happens when comparing values of different types, such as a number and a string using the strictly equal sign

For example:

console.log(5 == "5"); // true
console.log(5 === "5"); // false

In the first example, JavaScript uses implicit coercion by converting the string "5" to the number 5 before comparing the values. The strict equality operator "===" is used in the second example, which performs no type coercion and returns false.

Now that you understand the various types of coercion in JavaScript, let us look at some common pitfalls that occur when writing JavaScript code.

Common pitfalls and best practices for managing data types and type coercion

The following are some common pitfalls when managing data types and type coercion in JavaScript:

  1. Comparing values with the == operator: The == operator performs type coercion, which can result in unexpected results. It’s recommended to use the strict equality operator (===) instead.
  2. Using the typeof operator: When determining the type of a value, the typeof operator can be unreliable.

Best Practice

When working with JavaScript data types and type coercion, keep the following best practices in mind:

  • Be mindful of type coercion and use explicit conversions as needed.
  • Always use === instead of == for equality comparisons.
  • Use variable names that are descriptive and indicate the data type.
  • To avoid naming conflicts, avoid using global variables as much as possible.

Debugging Type Coercion

Type coercion can occasionally result in unexpected outcomes and bugs in your code. Using the 'typeof' operator to check the data type of a variable is one way to debug type coercion.

For example:

let num = 5;
console.log(typeof num); // Output: number

let str = '5';
console.log(typeof str); // Output: string

You can also use the 'Number()' and 'String()' functions to explicitly convert data types as needed.

Conclusion

Understanding JavaScript data types and type coercion is an important part of learning the language. By mastering these concepts, you will be better equipped to write efficient and effective code. Always remember to follow best practices and to be aware of the data types you're working with.

Thank you for taking the time to read this, and I hope you found it useful.

--

--

Oladipupo Ishola

Tech Writer | Mentor & Code Instructor | Bootcamp Grad | Full Stack Developer | Frontend Developer | Backend Developer | MLH ’21 Alum