support@90-10.dev

JavaScript Built-in Objects (part 2)

This is a continuation of JavaScript Built-in Objects (part 1).

11. JSON

JSON (JavaScript Object Notation) is a lightweight data format used to exchange data between the client and the server. The JSON object in JavaScript is a built-in object that has methods for converting JavaScript objects to and from JSON format:

const person = {
   "name": "Charlie"
};

const personJSON = JSON.stringify(person);
const backToPersonAgain = JSON.parse(personJSON);

12. Promise

Promise is an object that represents the eventual completion (success or failure) of an asynchronous operation and its resulting value:

const myPromise = new Promise((resolve, reject) => {
  const result = someAction();
  if (result) {
    resolve(result);
  } else {
    reject(new Error('Something went wrong'));
  }
});

myPromise.then((result) => {
  // success
  console.log(result);
}).catch((error) => {
  // failure
  console.error(error);
});

13. Set

Represents a unordered collection of unique values:

const mathConstants = new Set();
mathConstants.add(3.14);
console.log(mathConstants);       // prints: Set [ 3.14 ]
console.log(mathConstants.size);  // prints: 1

14. WeakSet

The WeakSet object allows us to store weakly held objects in a collection - it is similar to a Set object but can only contain weakly-referenced objects and doesn't support iteration or have a size property.

const mathConstants = new WeakSet();

const pi = {
    name: 'PI',
    value: 3.14
}
mathConstants.add(pi);
console.log(mathConstants);  // prints: WeakSet [ { ... } ]

15. Map

The Map object is a built-in data structure that allows you to store key-value pairs - it is similar to Object but keys can be of any type, including objects:

const myMap = new Map();
const key1 = { name: 'key 1'};
myMap.set(key1, 'value1');

console.log(myMap);            // prints: Map { {…} → "value1" }
console.log(myMap.get(key1));  // prints: value1
console.log(myMap.size);       // prints: 1

16. WeakMap

The WeakMap object is similar to the Map object, but with some differences:

  • stores weakly held objects
  • keys are garbage collected if no references present
  • just like with WeakSet, it doesn't support iteration or have a size property

17. Symbol

A  symbol is a unique and immutable data type that represents an identifier:

let mySymbol = Symbol();

For more, read our JavaScript Symbols article.

18. BigInt

Allows the representation of integers with arbitrary precision with values much larger than the maximum number that can be represented by the Number data type. To create a BigInt value, you can append the letter n to the end of an integer literal or use the BigInt() constructor function:

const bigNumber = 1234567890123456789012345678901234567890n;
const anotherBig = BigInt('1234567890123456789012345678901234567890')

19. ArrayBuffer

Represents a fixed-length raw binary data buffer and is used to store binary data in a specific format and size. Here is how to create an ArrayBuffer object with a length of 16 bytes:

const buffer = new ArrayBuffer(16);

20. DataView

A low-level interface for reading and writing binary data - allows you to read and write data in different byte orders and allows you to specify the byte offset of the data you want to access. In the example below, buffer in an ArrayBuffer object:

new DataView(buffer [, byteOffset [, byteLength]]);

This was a quick run through 20 of the most useful predefined Objects in JavaScript.