Skip to content

Collections

It's common to have different values that describe an entity or that suggest a list of items. We'll look at two data structures in JavaScript that we can use to conveniently represent entities and lists.

SlidesPDF

Do these exercises in a JavaScript file. (From now on, we won’t be using the REPL so much.)

  1. What happens if you try to access a property that doesn’t exist in an object? What about popping an empty array? Can you spread an object into an array or vice versa? You may have your questions too. Try to get answers by experimenting.

  2. Pick any app or website (or some other software) of your choice and identify some entities and lists in it. Try representing those entities and lists using JavaScript objects and arrays.

  3. An object can have a property whose value is also an object. In this case, we say that the inner object is a nested object:

    const user = {
      name: 'Random User',
      email: 'user123@example.com',
      // The value of address is a nested object
      address: {
        city: 'Jabi',
        state: 'Abuja'
      }
    }
    

    How would you access the state property of the nested address object? Try changing the value of the city property to something else, like “Gwarinpa”.

  4. An array can contain nested arrays too. When an array contains only array elements, as in the following example, we call it a two-dimensional array or a matrix. (The regular array is a one-dimensional array.)

    const arr = [
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]
    ]
    

    How would you access and change the number 7 of the nested array?

We’ve discussed these types so far: number, boolean, string, undefined, null, and object (including array). All of them, except object, are primitive types, and one thing common to primitives is that they are immutable.

There are two more primitive types in JavaScript as of today: bigint and symbol.

The bigint type is used to represent very large integers that the number type cannot correctly represent. Bigints are written like regular integers, but they must end with an “n”, as in 123456789987654321n.

The symbol type is used to represent unique values. A symbol is created using the Symbol function, as in Symbol(). Symbols can also be used as keys in objects.

These two types aren’t used so often, so we may not even cover them during the meetings. Nonetheless, you should know that they exist.

We’ve seen one supposedly odd behaviour of typeof — it tells us that an array is an object. There’s yet another strange behaviour. If you run typeof null, you’ll get “object”. However, null is not an object. The reason for this is historical and we just have to live with it.

A final thing to note is that when you use typeof on an undeclared variable, the result is “undefined”:

console.log(typeof x)
// There's no variable x declared above,
// so the result is "undefined".