> front-end

Data Types

Created by: Kitman Yiu

Updated at: 1 day ago

  • Definition: More concise syntax for writing function
  • Benefits: does not have own "this" lexically bound
  • Examples: const sum = (a, b) => a + b;
1. Why we need data type?

The concept of data types in JavaScript originates from the need to categorize data in programming languages. It was developed to better manage and use data during programming.

Before the advent of JavaScript, other programming languages like C or Java already had their own data type systems. The emergence of JavaScript provided a set of data types more suited for web pages and web applications.

2. What is it?

JavaScript data types are mainly divided into two categories: primitive types and object types.

  • Primitive types include: String, Number, Boolean, Undefined, Null, Symbol (added in ES6), BigInt (large integer, added in ES2020).
  • Object types are a composite type, including: Object, Array, Function, etc.

Three principles for applying these data types:

  1. Choose the appropriate type for efficient data handling.
  2. Avoid unnecessary type conversions to reduce errors and improve performance.
  3. Understand the mechanisms of conversion and comparison between types.

Practical example:

  • Background: Collecting age information from users in a web form.
  • Application: The user's age input (a string) needs to be converted to a number type for comparison or calculation.
  • Code example:
    
    <script>
    let ageString = "30"; // User-entered age, string type
    let ageNumber = Number(ageString); // Convert to number type
    if (ageNumber >= 18) {
    console.log("Adult");
    }
    </script>
3. Where is it going?

Limitations: Certain JavaScript data types (like global variables) may lead to memory leaks. Additionally, type conversions can cause unexpected errors.

Current optimization direction: Enhancing the strictness and consistency of the type system, as seen with the introduction of TypeScript.

Future development: There might be the addition of new data types or optimization of existing types' memory and performance to suit more complex application scenarios.