Skip to main content

Command Palette

Search for a command to run...

Understanding Data Types in JavaScript

Updated
6 min readView as Markdown

Introduction

JavaScript is a popular programming language used for building web applications and other software. A key feature of JavaScript is its ability to work with different data types. In this article, you will get a good understanding of the data types in JavaScript, that is how they're used, and why they're important.

Overview of Data Types

What are Data Types?

Data types refer to the different types of values that can be stored and manipulated in a program. All values in JavaScript have a data type, and this determines the kind of operations that can be performed on them. For example, arithmetic operations cannot be performed on strings(shh I will explain) but only on numbers.

Classification of Data Types

JavaScript has seven different data types, which can be categorized into two main groups: primitive and reference data types.

Primitive Data Types

Primitive data types are the simplest form of data that JavaScript can work with. There are five primitive data types:

  1. Number:

    they are either integers or floating-point numbers(that is values that have decimal points). For example:

    let x = 7; //this is an integer

    let y = 3.14; //this is a floating point number

    Like I said Numbers permit carrying out arithmetic operations but strings do not. Example

    let x = 7 + 3

    console.log(x) // Output: 10

  2. Boolean:

    this data type is used for logical values that can only either be true or false(just like you can only either read this article or not). For example:

    let userIsOnline = true;

    let isLoggedOut = false;

  3. String:

    a string is a text value that is enclosed in quotes (either single or double). For example:

    let name = "Harry Potter";

    let message = "Welcome to Hogwarts!";

  4. Undefined:

    this defines a variable that has no value assigned to it yet. Undefined basically means not defined(means the variable has been created but not been assigned a value). For example:

    let x; // the value of x is undefined

  5. BigInt:

    this data type is used to represent integers that are too large to be represented using the Numberdata type. For example:

    const bigNumber = BigInt(1234567890123456789n);

  6. Null:

    this data type is used to indicate the intentional omission of any object value.

    For example;

    let y = null;

    A use case may be that; you may create a function that searches for an object in an array and returns the object if it is found. If the object is not found, you can return null to indicate that there is no object matching that search criteria.

  7. Symbol:

    this data type was added in ECMAScript 2015. Symbols are used to create unique identifiers that cannot be changed or accessed by other parts of your code. For example:

    const id = Symbol()

    Symbols can be given an optional description which aids in easy debugging of code. For example:

    const sym2 = Symbol('foo');

    All symbols are unique, hence any symbol created is different from the others. They cannot be overridden, this is useful in situations where we need to ensure the uniqueness of object properties or other identifiers.

Reference Data Types

Non-primitive data types are more complex data types that are not directly stored in memory. Instead, they are references to values stored in memory.

  1. Object:

    this is a complex data type that represents a collection of key-value pairs, where the key is always a string or aSymbol, and the value can be any data type, including another object. Objects are very powerful in JavaScript and are used extensively to represent real-world entities and relationships.

    For example:

     const person = {
         name: "Tommy Shelby", 
         age: 30,
         isStudent: true, 
         hobbies: ["reading", "traveling"], 
         address: { 
             street: "123 Main St", 
             city: "Small Heath", 
             state: "NY" 
         } 
     };
     console.log(person.name); // Output: "John"
     console.log(person.hobbies[0]); // Output: "reading"
     console.log(person.address.city); // Output: "Small Heath"
    

    Type Conversion and Type Coercion

    In JavaScript, type conversion and type coercion play an important role in working with different data types.

    Type Conversion

    Type conversion refers to manually converting a value from one data type to another, while type coercion is the automatic conversion of values from one data type to another by JavaScript itself to ensure that an operation is executed even if the data types are different.

    There are several ways to convert data types in JavaScript. One common way is to convert strings to numbers. This can be done using the Number() function.

    For example:

     let numString = "10"; 
     let num = Number(numString); 
     console.log(num); // the Output is: 10(which is a number)
    

    Similarly, converting a number to a boolean value can be done using the Boolean()function.

    For example:

     let num = 10; 
     let bool = Boolean(num); 
     console.log(bool); // the Output is: true(because 10 is a truthy value)
    

    Type Coercion

    Type coercion is done automatically by JavaScript, and it helps to ensure that an operation is executed even if the data types are different. This happens because Javascript has a dynamic typing system(I explain this below). For example, using the + operator to concatenate strings can also be used to add numbers if they are in string form.

    For example:

     const numString = "10"; 
     const num = 5; 
     const result = numString + num; 
     console.log(result); // the Output: "105"(due to concatenation)
    

    In the example above, javascript automatically converted `5` to a string and then concatenated it with the variable; numString.

    Type coercion can sometimes lead to unexpected output, such as the NaN value. NaN stands for "Not a Number" which is the result of an operation that could not produce a new number.

    For example:

     const result = "hello world!" * 5;
     console.log(result); // Output: NaN
    

    Note: used the typeof operator to check the data type of a value, including the NaN value. Interestingly, the typeof NaN is actually number (C'mon try it out!)

Dynamic Typing System

JavaScript has a dynamic typing system, which means that a variable can hold any data type, and its type can be changed at runtime. This allows for greater flexibility in programming and makes it easier to write and modify code.

For example:

let x = 10; 
console.log(typeof x); // Output: "number"
x = "hello"; 
console.log(typeof x); // Output: "string"

However, dynamic typing can also lead to errors if not used properly. It is important to be aware of the data types being used and to handle them appropriately in the code.

Conclusion

We discussed;

  1. data types,

  2. categories of data types,

  3. type conversion and type coercion

  4. dynamic typing system in javascript

Understanding data types in JavaScript is crucial for writing effective and efficient code. By mastering data types in JavaScript, developers can write more robust and error-free code. Keep practicing and improving your skills!!!