A structure (struct) groups related variables under a single name, each reachable by a named key instead of a numeric position. Where an array is a list ordered by position, a struct is a lookup table ordered by name — a user's profile, a product's details, or configuration settings all fit naturally into a struct.
Learning Objectives
After completing this lesson, you'll be able to:
- Create a struct using literal syntax and structNew().
- Access and modify struct values with dot notation and bracket notation.
- Understand the difference between an ordered and an unordered struct.
- Build nested structs.
- Loop over a struct's keys.
What Is a Struct?
A struct's keys are strings, and its values can be anything — a string, a number, an array, or another struct. Unlike an array, a struct's default iteration order is not guaranteed to match the order you added keys in.
Creating a Struct
Dot Notation vs. Bracket Notation
Dot notation (user.name) is the common case, for keys you know ahead of time. Bracket notation (user["name"]) is required when the key has spaces or special characters, or when the key itself is stored in a variable.
user[fieldName] only works with bracket notation — dot notation can't take a dynamic key from a variable.
Ordered Structs
By default, looping over a struct doesn't guarantee the keys come back in the order you added them. An ordered struct fixes that, preserving insertion order.
[:] is a shorthand literal for an ordered struct, equivalent to structNew("ordered").
Case-Sensitive Structs
Struct keys are case-insensitive by default — name and NAME refer to the same key. structNew("casesensitive") changes that.
In the regular struct, the second assignment overwrites the first, since name and NAME are the same key — leaving only 1 entry.
Nested Structs
Looping Over a Struct
Covered in the Loops module — a quick recap, since it's fundamental to working with structs.
Real-World Example: Building a Config Object
Common Beginner Mistakes
Expecting a plain struct to preserve insertion order
Looping over a regular struct doesn't guarantee keys come back in the order you added them. Use structNew("ordered") when order matters.
Using dot notation with a dynamic key
user.fieldName looks for a literal key named "fieldName", not the value stored in a variable called fieldName. Use bracket notation — user[fieldName] — for dynamic keys.
Assuming struct keys are case-sensitive by default
They're not — user.name and user.NAME refer to the same key unless the struct was created with structNew("casesensitive").
Best Practices
- Use dot notation for known, fixed keys — it's more readable than bracket notation.
- Use an ordered struct whenever the order you display or process keys in actually matters.
- Keep struct nesting reasonably shallow — three or more levels deep usually reads more clearly as separate named variables or a different structure.
Interview Questions
What data type are struct keys?
Strings.
What's the difference between a struct and an array?
An array is ordered and accessed by numeric position; a struct is accessed by named string keys and, by default, has no guaranteed iteration order.
How do you create a struct that preserves insertion order?
structNew("ordered"), or the [:] literal shorthand.
Are struct keys case-sensitive by default?
No — name and NAME refer to the same key unless the struct was created with structNew("casesensitive").
Summary
In this lesson, you learned what a struct is, how to create one with literal syntax or structNew(), the difference between dot and bracket notation, ordered vs. unordered (and case-sensitive) structs, and how to build nested structs.
What's Next?
The next lesson covers adding, removing, and checking struct keys — structInsert, structDelete, structKeyExists, and merging structs together.