How to create a JavaScript associative array with negative integer keys?

Sometimes, we want to create a JavaScript associative array with negative integer keys.

In this article, we’ll look at how to create a JavaScript associative array with negative integer keys.

How to create a JavaScript associative array with negative integer keys?

To create a JavaScript associative array with negative integer keys, we can wrap the negative numbers in quotes.

For instance, we write:

const o = {
  '-1': 'apple',
  '-2': 'orange'
}
console.log(o)

to define object o which has negative integers as keys.

Therefore, o is {-1: 'apple', -2: 'orange'} according to the console log.

Conclusion

To create a JavaScript associative array with negative integer keys, we can wrap the negative numbers in quotes.