Data Types - potatoscript/javascript GitHub Wiki

πŸ“Š Data Types in JavaScript πŸ”’

In JavaScript, data types refer to the kinds of values that can be stored in variables. Understanding data types is essential for writing effective and error-free code, as it allows you to manipulate and work with different kinds of data in your programs.

There are two main categories of data types in JavaScript:

  • Primitive Data Types: These are basic data types that hold a single value.
  • Reference Data Types: These are more complex data types that refer to objects, arrays, and functions.

Let’s break down each data type in detail:


🌟 1. Primitive Data Types πŸ”€

Primitive data types are simple and represent a single value. In JavaScript, there are seven primitive data types:

  1. String πŸ’¬

    • A string is used to represent text in JavaScript. Strings are enclosed in either single quotes (' ') or double quotes (" ").

    • Example:

      let greeting = "Hello, world!";
      let name = 'Lucy';
      
    • Important: Strings are immutable, meaning once you create a string, you can’t change the characters inside it. You can, however, create a new string by modifying the original.

  2. Number πŸ”’

    • Number is used to represent both integer and floating-point numbers (decimals).

    • Example:

      let age = 25;        // An integer
      let price = 19.99;   // A decimal number (float)
      let temperature = -10;  // Negative number
      
    • Important: JavaScript has only one type of number β€” Number β€” for both integers and floating-point numbers.

  3. Boolean πŸ”˜

    • Boolean is a simple data type that can only be one of two values: true or false. Booleans are typically used to represent the truth or falsehood of a condition.

    • Example:

      let isAdult = true;  // True
      let isOnline = false; // False
      
    • Important: Booleans are often used in conditional statements like if statements.

  4. Null 🚫

    • Null is a special value that represents the intentional absence of any object value. It is a non-existent or empty value.

    • Example:

      let person = null;
      
    • Important: null is a deliberate assignment, indicating that no value is assigned.

  5. Undefined πŸ”„

    • Undefined means a variable has been declared but has not been assigned a value. It is JavaScript’s default value for uninitialized variables.

    • Example:

      let car; // Variable is declared, but not initialized
      console.log(car); // undefined
      
    • Important: undefined is different from null because undefined means the variable is not yet assigned a value, while null explicitly means there is no value.

  6. Symbol πŸ”‘

    • Symbol is a unique and immutable primitive value that is often used to create unique identifiers for object properties.

    • Example:

      const uniqueID = Symbol('id');
      console.log(uniqueID);  // Symbol(id)
      
    • Important: Symbols are often used in scenarios where uniqueness is required, like object property keys.

  7. BigInt πŸ’₯

    • BigInt is a relatively new data type used to represent large integers that cannot be stored in the regular Number type. It is used for numbers beyond the range of the Number type.

    • Example:

      const bigNumber = 9007199254740991n;  // BigInt is created by appending "n" to the number
      console.log(bigNumber);  // 9007199254740991n
      
    • Important: BigInt is useful for scenarios that need precise large integer calculations.


🌟 2. Reference Data Types πŸ“‘

Reference data types are more complex and can hold multiple values or properties. They are stored by reference, meaning the variable stores a reference (memory address) to the data.

  1. Object πŸ—‚οΈ

    • An object is a collection of properties and methods, where each property is a key-value pair. Objects are used to store more complex data, like multiple related pieces of information.

    • Example:

      let person = {
          name: "Lucy",
          age: 25,
          isStudent: true
      };
      
    • Important: Objects can store multiple values, and you access them using the key names (e.g., person.name).

  2. Array πŸ‡

    • An array is a special variable that holds a list of values. Arrays can hold values of any data type and can be indexed using numbers (starting from 0).

    • Example:

      let fruits = ["apple", "banana", "cherry"];
      let numbers = [10, 20, 30, 40];
      
    • Important: Arrays allow you to store collections of similar or different values, and you can access them using their index number (e.g., fruits[1] gives "banana").

  3. Function πŸ§‘β€πŸ’»

    • A function is a block of code designed to perform a particular task. Functions can accept parameters (input), perform some operations, and return a result.

    • Example:

      function greet(name) {
          return "Hello, " + name + "!";
      }
      
      let message = greet("Lucy");
      console.log(message);  // "Hello, Lucy!"
      
    • Important: Functions are first-class objects, meaning they can be stored in variables, passed as arguments, and returned from other functions.


🌍 Data Type Conversion πŸ”„

JavaScript automatically converts between data types when needed, but you can also manually convert between types.

  • String to Number: You can convert a string to a number using the Number() function.

    let str = "123";
    let num = Number(str);  // 123 (as a number)
    
  • Number to String: You can convert a number to a string by using the String() function or concatenating with an empty string.

    let num = 123;
    let str = String(num);  // "123" (as a string)
    
  • Boolean to String: Convert a boolean to a string using the String() function.

    let bool = true;
    let str = String(bool);  // "true"
    
  • String to Boolean: You can convert strings to booleans using the Boolean() function.

    let str = "hello";
    let bool = Boolean(str);  // true (non-empty strings are truthy)