Skip to main content

index

Index access allows you to retrieve and modify elements in arrays, lists, strings, and object properties using square bracket notation []. This is fundamental for working with collections and data structures in Soul.

Array Index Access

Access array elements by their numeric index:

Negative Index Access

Access elements from the end of arrays:

String Index Access

Access individual characters in strings:

Object Property Index Access

Access object properties using string keys:

Dynamic Index Access

Use variables as indices:

Multi-dimensional Arrays

Access nested arrays:

Nested Object Access

Access nested object properties:

Array of Objects Index Access

Access properties of objects in arrays:

Index with Expressions

Use complex expressions as indices:

Safe Index Access

Handle index bounds safely:

Index in Loops

Use indices in loop constructs:

Index Assignment Patterns

Different ways to assign values using indices:

Index with Error Handling

Handle index access errors:

Index with Object Methods

Use index access with object methods:

Index Performance Considerations

Optimize index access:

Index Validation

Validate indices before use:

Best Practices

  1. Check bounds: Always validate indices before use
  2. Use meaningful names: users[currentIndex] instead of users[i]
  3. Handle errors: Wrap index access in try-catch when needed
  4. Cache lengths: Store array lengths in variables for performance
  5. Use safe access: Create helper functions for safe index operations
Index access is fundamental to working with data structures in Soul. Use it carefully with proper bounds checking and error handling to create robust applications.