This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

ECMAScript provides three special reference types: Boolean, Number, and String to facilitate manipulation of raw values. Let’s start with an example:

let my = "Sam";
let you = my.substring(2)

console.log(you); // "m"
Copy the code

A primitive value string, my, is created, followed by a call to the substring() method, and the result is saved to you. But the original value is not an object, so where does the substring method come from? Actually do because background processing, so as to realize the above operations, in particular, when the second line to visit my in read-only mode access, the background will perform the following three steps: 1, 2, create an instance of type String, call instance specific methods of 3, destroy instance Equivalent to execute the following code:

let my = new String("Sam");
let you = my.substring(2);
my = null;
Copy the code

The same is true for Boolean and Number. The above three steps are performed in the background, but using the wrapper types for Boolean and Number.

Boolean

Boolean is the reference type of the corresponding Boolean value. To create a Boolean object, use the Boolean constructor and pass true or false, as shown in the following example:

let booleanObject = new Boolean(true);
Copy the code

Boolean instances override the valueOf() method, returning a primitive valueOf true or false. The toString() method is also overridden when called, returning the string “true” or “false”. However, Boolean objects are rarely used in ECMAScript. Not only that, but they can be misleading, especially when using Boolean objects in Boolean expressions, such as:

let falseObject = new Boolean(false);
let result = falseObject && true;
console.log(result); // true

let falseValue = false;
result = falseValue && true;
console.log(result); // false
Copy the code

In this code, we create a Boolean object with a value of false. The object is then combined with the original value true in a Boolean expression using the && operation. In Boolean arithmetic, false && true equals false. However, this expression evaluates the falseObject object rather than the value it represents (false). As mentioned earlier, all objects are automatically converted to True in a Boolean expression, so falseObject actually represents a true value in this expression. So true && true is of course true. In addition, there are several differences between raw values and reference values (Boolean objects). First, the Typeof operator returns “Boolean” for the original value but “object” for the reference value. Similarly, Boolean objects are instances of type Boolean that return true when using the instaceof operator, but false for raw values. For example:

console.log(typeof falseObject);             // object
console.log(typeof falseValue);              // boolea
nconsole.log(falseObject instanceof Boolean); // true
console.log(falseValue instanceof Boolean);  // false
Copy the code

It is important to understand the difference between raw Boolean values and Boolean objects, and it is strongly recommended that you never use the latter.

Write in the last

Writing is not easy. I hope I can get a “like” from you. If the article is useful to you, you can select “bookmark”. If there are any mistakes or suggestions in the article, please comment on it. Thank you. ❤ ️

Welcome to other articles

  • Internationalizing your website with Vite2+Vue3 | More challenges in August
  • Actual: Front-end interface request parameter confusion
  • Actual: Implement a Message component with Vue3
  • Actual: Vue3 to implement the Image component, by the way, support lazy loading
  • One Piece, Vue.js 3.0 brings what updates
  • This article digests the major new features of ES7, ES8, and ES9
  • Technical team’s common problems and solutions
  • 10 new features commonly used in ES6
  • Vue3’s Script setup grammar sugar is really cool