Data Types Reference

This document provides a comprehensive overview of the fundamental data types available in our programming language. Understanding these types is crucial for writing efficient, correct, and maintainable code.

Primitive Data Types

Primitive data types are the most basic types. They are immutable, meaning their values cannot be changed after they are created.

1. Numbers

Represent both integer and floating-point values. Modern languages typically use IEEE 754 standard for floating-point representation.

let integerValue = 100;
let floatValue = 98.6;
let scientificNotation = 1.23e-4;

2. Strings

Represent sequences of characters. Strings are enclosed in single (') or double (") quotes. They are typically immutable.

let greeting = "Hello, World!";
let singleQuoted = 'This is a string.';
let message = `This is a template literal, useful for embedding variables: ${greeting}`;

3. Booleans

Represent logical values: true or false. They are fundamental for control flow.

let isComplete = true;
let hasError = false;

4. Null

Represents the intentional absence of any object value. It's a primitive value but of type 'object' in some older language specifications due to historical reasons.

let emptyValue = null;

5. Undefined

Represents a variable that has been declared but not yet assigned a value, or a function that does not explicitly return a value.

let declaredButNotAssigned; // value is undefined
function doNothing() {} // returns undefined implicitly

6. Symbols

Introduced to provide unique identifiers, often used as keys for object properties to avoid name collisions.

const id = Symbol('uniqueId');
const anotherId = Symbol('uniqueId');
console.log(id === anotherId); // false

7. BigInt

Represents integers with arbitrary precision. Useful for handling integers larger than what the standard Number type can safely represent.

const bigNumber = 123456789012345678901234567890n;
const anotherBigNumber = BigInt(9007199254740991);

Complex Data Types (Objects)

Complex data types are not primitive. They are often collections of other data types and can be more intricate.

1. Objects

A collection of key-value pairs. Keys are typically strings (or Symbols), and values can be any data type, including other objects.

let person = {
    name: "Alice",
    age: 30,
    isStudent: false,
    address: {
        street: "123 Main St",
        city: "Anytown"
    }
};

2. Arrays

An ordered list of values. Elements are accessed by their index (starting from 0).

let fruits = ["Apple", "Banana", "Cherry"];
let numbers = [1, 2, 3, 4, 5];
let mixedArray = ["Text", 123, true, { id: 1 }];

3. Functions

First-class citizens in many languages, meaning they can be treated like any other data type: assigned to variables, passed as arguments, and returned from other functions.

function greet(name) {
    return `Hello, ${name}!`;
}
let sayHi = greet; // Function assignment

Type Conversion

Often, you'll need to convert data from one type to another. This can happen implicitly (coercion) or explicitly (casting).

Be mindful of type coercion, as it can lead to unexpected behavior if not understood properly. Explicit conversion is generally preferred for clarity.

Implicit Conversion (Coercion)

The language automatically converts types in certain operations.

let numberString = "5";
let sum = 10 + numberString; // sum will be "105" (string concatenation)

Explicit Conversion

Using built-in functions or methods to convert types.

Conversion To Method/Function Example Result
String String() or .toString() String(123) or (123).toString() "123"
Number Number() or parseInt()/parseFloat() Number("45.6") or parseFloat("45.6") 45.6
Boolean Boolean() Boolean(0) false
BigInt BigInt() BigInt(100) 100n
Always check the documentation for specific methods and their behavior, especially regarding edge cases like parsing invalid number strings.