This is the third day of my participation in the August More Text Challenge. For details, see: August More Text Challenge;

preface

The type of javaScript is the most familiar concept for the pre-end friends. We should know that every value in THE JS language belongs to one of the data types.

Here, let’s take a quick look at the classification of data types. In the body, I’ll focus on data types that you might have overlooked.

  • Basic data types (also called “value types”)
    • String
    • Number
    • boolean
    • undefined
    • null
    • Symbol
  • Reference data types (also called “object types”)
    • Object
    • Function
    • Array

The body of the

The Number type

The Number type is the same as the Number type.

    console.log( 0.1 + 0.2= =0.3); // "false"
Copy the code

If false is printed, then the two sides of the equation are not equal; There is a loss of precision in floating-point operations, where the left and right sides of the equation are not exactly equal, but slightly off.

Therefore, we need to know that for non-integer Number type, we cannot use == (or ===) to compare. The correct way to compare is to use the minimum precision value provided by javaScript: as long as the absolute value of the difference between the two sides of the equation is less than the minimum precision value, true will be returned.

    console.log( Math.abs(0.1 + 0.2 - 0.3) < =Number.EPSILON); // "true"
Copy the code

About the understanding of floating point numbers is not much here, small friends can refer to this article: How to understand floating point numbers

Object and Symbol types

Symbol, which is a collection of all non-string object keys, can be used to create a unique identifier for an object. Without further discussion, let’s look at an example where a full YMBOL function cannot be called using new 🌰

    let myBoolean = new Boolean(a);console.log(typeof myBoolean); // "object" 
    let myString = new String(a);console.log(typeof myString); // "object" 
    let myNumber = new Number(a);console.log(typeof myNumber); // "object"  
Copy the code
    let mySymbol = new Symbol(a);// TypeError: Symbol is not a constructor
Copy the code

The Symbol() function cannot be used as a constructor with the new keyword. This is done to avoid creating a symbol-wrapped object, which in general means that it does not overwrite existing object properties; Like using Boolean, String, or Number, they all support constructors and can be used to initialize a wrapper object that contains the original value.

Type conversion

Js is a weakly typed language, so conversions are very frequent. A lot of familiar arithmetic rules will be cast first, if not cast or cast wrong, it is easy to cause some error in code judgment.

  • String conversion

    • String conversions are used when we want a value of string type to be.

      For example, let’s say we need to print out a value of string type. How do we do that?
          let value = true; 
          console.log(typeof value); // "boolean"
          value = String(value); // Now, the value is a string of "true"
          console.log(typeof value); // string*
      Copy the code

    In this case, the value is converted to a String using String(value)

  • To digital conversion

    • We’re familiar with parseInt, parseFloat, Number. In fact, in most cases, Number is our better choice. The conversion rules for the Number type are as follows:

      value Converted result
      undefined NaN
      False and true 0 s and 1 s
      string The number contained in a pure numeric string with the first and last Spaces removed. If the remaining string is empty, the result of the conversion is0. Otherwise, the number is “read” from the remaining string. Returns when a cast error occursNaN.

      Interested partners can according to the following example, try.

          console.log( Number("123"));/ / "123"
          console.log( Number("123z"));// NaN (" read "number from string, error reading "z")
          console.log( Number(true));/ / "1"
          console.log( Number(false));/ / "0"
      Copy the code
  • Boolean conversion

    • Boolean conversion, relatively simple, summarized the following points for your reference:
      • like0.null.undefined.NaN."", the result of these transformations isfalse; Other values fortrue;
      • right"0"And whitespace only strings (for example:""), the conversion result istrue;

Ask questions

Question: What if we use JS code to convert String to Number instead of the native Number and parseInt?

summary

Welcome passing friends, leave their own ideas or code, we communicate together, progress together

The next article “Implicit conversions” mainly introduces some implicit conversions in js in detail, interested partners can take a look.

Front long long distance, we are on the road, continue to update ING…..