JavaScript: Basic data type 1: Null, Undefined, Boolean, Number

  • There are currently six basic data types in JavaScript :Boolean, String, Number, NULL, undefind, and Symbol

Null and undefined are special data types in JS that have only one value and are the simplest data types in JS.

  • Undefined means that no value is defined (initialized)
    • Declaring that a variable is not initialized using var or let is equivalent to assigning undpay to the variable
  • Null is an Object with a null pointer, which is why Typeof NULL returns “Object”
    • When defining the variable to be saved as an object value, it is recommended to use unLL for initialization
    • Undefined is derived from null, which is defined to indicate equality
        console.log(null == undefined) // true
        console.log(null === undefined) // false
    Copy the code

Boolean has two values true and false

  • You can use Boolean() to convert any value to a Boolean value
  • False: +0, -0, null, undefined,NaN, empty string (“”)
  • True: All but the above initial values are true, including empty objects, empty arrays, the string “false”, non-zero values (including positive and negative infinity)

Number, Bigint,

The number types in JS use IEEE754’s 64-bit floating-point type: the largest safe integer, 2 to the 53th power -1

  • Use:
    new Number(value);
    var a = new Number('123'); // a === 123 is false
    var b = Number('123'); // b === 123 is true
    a instanceof Number; // is true
    b instanceof Number; // is false
Copy the code
  • Floating point value

    • The decimal point is not necessarily preceded by an integer, but it is recommended
    Let floatNum1 = 1.1 let floatNum2 = 0.1 Let floatNum3 =.1 // Valid single not recommended Let floatNum4 = 1 Treat as integerCopy the code
    • Floating-point numbers are as accurate as 17 as a decimal but far less accurate in arithmetic than integers

    The rounding error is due to the use of IEEE754 values and is not unique to JS

    // 0.1+0.2 does not equal 0.3 0.1+0.2=0.300 000 000 000 000 04Copy the code
  • Scope of worth

    • MIN_VALUE In most browsers, the value is 5E-324-infinity
    • Maximum value: number. MAX_VALUE Converts to Infinity beyond this range
    • Check whether isFinite() is used between maximum and minimum values
  • NaN

    • Any operation that involves NaN always returns NaN
    • NaN is not equal to any value including NaN. You can use the isNaN function to determine if a value isNaN
    console.log(0/0)  // NaN
    console.log(-0/+0) // NaN
    console.log(5/0) // Infinity
    console.log(5/-0) // - Infinity
    Copy the code
  • Conversion of values

    • There are three methods for converting numeric types: Number(), parseInt(), parseFloat()
    • Number() executes the transformation rule
      • Boolean true -> 1; false -> 0
      • Return value directly
      • Null, returns 0
      • undefined -> NaN
      • String:
        • Contains numeric values (including plus and minus signs) to decimal values ignoring the preceding 0
        • Contains valid floating-point numbers converted to corresponding integers, and the decimal point is not a valid value
        • Contains hexadecimal formats such as “0xf” converted to a hexadecimal integer
        • An empty string returns 0
        • In other cases NaN is returned
      • Object that calls valueOf() and then converts as described above, or toString() if NaN is returned
    • ParseInt () is more focused on whether the string contains a numeric pattern
      • Ignore the preceding whitespace, starting with the first non-null character: not a numeric value (which can contain a plus or minus sign) returns NaN
      • An empty string returns NaN
      • If the first character is a numeric value (which can contain a plus or minus sign), the sequence is matched, returning until a non-numeric character is encountered
      • You can pass in a second argument to specify base (base)
      ParseInt (" 1234blue") // returns 1234 parseInt("22.5") // Returns 22 decimal point is not a valid number parseInt("") // NaN parseInt("0xA") // 10 is interpreted in hexadecimal ParseInt ("AF", 16) // Hexadecimal 175 parseInt("AF") // NaN parseInt("10", 2) // parseInt in binaryCopy the code
    • ParseFloat () is similar to parseInt() but only parses decimal
    ParseFloat ("0.43Bul") // returns 0.43 parseFloat("22.34.5") // returns 22.34Copy the code

reference

  • MDN: developer.mozilla.org/zh-CN/docs/…
  • Books: The Little Red Book 3rd & 4th editions – javascript Advanced Programming