Skip to content

Object Literals & Constructors

Most built-in types in JavaScript can be created in two ways: with a literal, which writes the value out directly, such as {}, [] or 'text', or with a constructor, such as new Object(), new Array() or new String(). For objects and arrays the two give the same result and the literal is shorter, so literals are the normal choice. For strings, numbers and booleans the constructors give something different, which is a common source of bugs.

An object literal and new Object() both make a plain object whose prototype is Object.prototype:

const fromLiteral = { item: 'milk', qty: 2 };
const fromConstructor = new Object();
fromConstructor.item = 'milk';
fromConstructor.qty = 2;
console.log(fromLiteral, fromConstructor);
console.log(fromLiteral.constructor === Object, fromConstructor.constructor === Object);

This prints:

{ item: 'milk', qty: 2 } { item: 'milk', qty: 2 }
true true

The literal can set all the properties at once, so there is no reason to prefer new Object() for a plain object.

new Object(value) does not always make a new plain object. What it returns depends on the value it is given:

  • an object or array is returned unchanged, so no new object is made
  • null or undefined gives a new empty object
  • a string, number or boolean is wrapped in a String, Number or Boolean object
const list = ['eggs', 'flour'];
const recipe = { serves: 4 };
console.log(new Object(list) === list, Array.isArray(new Object(list)));
console.log(new Object(recipe) === recipe);
console.log(new Object(null), new Object(undefined));
console.log(new Object('flour'), new Object(12), new Object(true));

This prints:

true true
true
{} {}
[String: 'flour'] [Number: 12] [Boolean: true]

A string written in quotes is a primitive value, not an object. Calling String() without new converts a value to a primitive string, which is useful. Calling new String() makes a wrapper object instead, which behaves differently in comparisons:

const a = 'flour';
const b = String(12);
const c = new String('flour');
console.log(typeof a, typeof b, typeof c);
console.log(a === 'flour', c === 'flour', c == 'flour');
console.log(a.toUpperCase(), a.length);

This prints:

string string object
true false true
FLOUR 5

c === 'flour' is false because an object is never strictly equal to a primitive. A primitive string can still use methods such as toUpperCase(), because JavaScript wraps it in a temporary String object for the call, so there is nothing to gain by creating the wrapper yourself.

The same applies to new Number() and new Boolean(), and new Boolean() has a particular trap, because every object is truthy, including one that wraps false:

const flag = new Boolean(false);
if (flag) {
console.log('A Boolean object is always truthy');
}
console.log(typeof flag, flag.valueOf());

This prints:

A Boolean object is always truthy
object false

Use literals for primitive values, and use String(), Number() and Boolean() without new when a value needs converting. Symbol() and BigInt() cannot be called with new at all. Truthy and Falsey Values lists the values that count as false.

typeof reports 'object' for arrays, for plain objects and also for null, so it cannot tell them apart. instanceof checks the prototype chain, and since every array is also an object, it answers yes to both. Array.isArray() is the reliable test for an array:

console.log([] instanceof Array, [] instanceof Object);
console.log(typeof [], typeof {}, typeof null, typeof function () {});
console.log(Array.isArray([]), Array.isArray({ length: 0 }));

This prints:

true true
object object object function
true false

The typeof Operator covers typeof in more detail.

Steve Griffith covers this topic in his video Object Literals and Constructors.