Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The advantages of object and array literals over using the respective constructors are:</p> <ul> <li>Shorter and more readable</li> <li>Safer: literals will still work when the <code>Array</code> or <code>Object</code> constructors have been overridden</li> <li>Possibly faster, though it's unlikely to be a major consideration (any bottlenecks will almost certainly originate elsewhere in code)</li> </ul> <p>In the case of arrays, there's an additional advantage of a literal: it's impossible to create an array with a single member using the <code>Array</code> constructor alone. For example, <code>[3]</code> will create an array with one element which is the number 3, while <code>new Array(3)</code> creates an array of length 3.</p> <p><strong>Update: the following paragraph is no longer relevant now the question has been edited.</strong></p> <p>Regarding Booleans, you seem to have a misconception: <code>new Boolean(false)</code> is not the same as <code>false</code>. The <code>Boolean()</code> constructor creates a Boolean object whereas <code>false</code> and <code>true</code> are Boolean primitives. In fact, <code>new Boolean(false)</code> evaluates to <code>true</code> when coerced into a Boolean in, for example, an <code>if</code> statement. In short, there's very rarely a reason to use the <code>Boolean()</code> constructor. Use <code>true</code> and <code>false</code> instead. Similarly, other primitives such as strings and numbers have corresponding <code>String()</code> and <code>Number()</code> constructors that produce <code>String</code> and <code>Number</code> objects that are different to primitive strings and numbers and should generally be avoided.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload