** a ** data type

Value type (basic type) :

* 1 String (String) : Any String

  • 2 Number: any Number

NaN:

[Rookie tutorial] :

NaN attributes are special values that represent non-numeric values. This property is used to indicate that a value is not a number.

You can set the Number object to this value to indicate that it is not a numeric value.

[w3cschool] :

NaN attributes are used to reference special non-numeric values. NaN is not a constant and can be set to something else.

Tip:

Use isNaN() to determine if a value is a number. The reason is that NaN is not equal to all values, including itself.

— isNaN() returns a Boolean that is a number and returns false; Returns true for no number

In this example, we'll show what happens when a number exceeds the infinity limit: <script type="text/javascript"> var test1="300" var test2="Hello World!" console.log(Number(test1)); // 300 console.log(Number(test2)); // NaN console.log(isNaN(test1)); // false (number returns false) console.log(isNaN(test2)); </script>Copy the code

(Note: NaN is a special value of Number, not Number)

  • 3 Boolean (Boolean): true/false

* 4 Null: Null

You can empty a variable by setting its value to NULL.

* 5 Undefined: Undefined

The value of Undefined means that the variable has no value.

 * 6 Symbol 

[Rookie tutorial] :

**Symbol **Symbol is a new primitive data type introduced in ES6 that represents unique values.

[Shang Silicon Valley:] It is the seventh data type of JavaScript language, is a string like data type.

let s = Symbol(); console.log(s, typeof s); // Symbol() "Symbol" let s2 = Symbol(' Symbol '); Let s3 = Symbol(' s '); > < ==) let result = s + 100; Uncaught TypeError: Cannot convert a Symbol value to a number let result = s > 100; Uncaught TypeError: Cannot convert a Symbol value to a number let result = s > 100;Copy the code

Reference data types:

*7 Object: Any Object

:* Function: a special object (that can be executed);

* Array: a special type of object (numeric subscript, internal data is ordered)

Judgment of data type

Typeof (); typeof ();typeof A

: Returns a string representation of the data type (that is, a string is returned; Return all lowercase: ‘undefined’ ‘number’…)

It can be judged that:

1 determine the string

2 determine the number

3 determine Boolean

*4 Cannot determine null: Null returns object, so it cannot be determined

5 judgment undefined

6 judge object

Function (return ‘function’)

Inability to distinguish (judge) :

Null and object, array and object, because both null and array return ‘object’.

Var a // 5 undefined console.log(a, typeof a, typeof a==='undefined',a===undefined ) // undefined 'undefined' true true console.log(undefined == 'undefined'); // 1 Check string a= 'haixia' console.log(typeof a, typeof a==='string'); // string true // 2 Check number a = 4 console.log(typeof a, typeof a === 'number'); // string true // 2 Check number a = 4 console.log(typeof a, typeof a === 'number'); // number true // 3 Check Boolean a= false console.log(typeof a, typeof a===' Boolean '); A = null console.log(typeof a, typeof a==='null'); a= null console.log(typeof a, typeof a==='null') Var b = {} console.log(typeof b, typeof a==='object'); Function var c = function() {console.log(' console.log '); } c() console.log(typeof c, typeof c ==='function');; C = [1,2,3] console.log(typeof c, typeof c ==='object'); // function trueCopy the code

To expand, suppose we want to determine whether a variable exists:

if(!a) { console.log('error')} 
// 这时候,控制台会报错:Uncaught ReferenceError: a is not defined

// 所以我们可以使用 typeof 来判断:
if(typeof a === 'undefined') { 
  console.log('error');}
}
Copy the code

This avoids code exceptions and is a more rigorous approach.

2 instanceof 

A instanceof B is used to determine whether A is an instanceof b. the expression is: A instanceof B, true if A is an instanceof B, false otherwise.

instanceof (A,B) = { var L = A.__proto__; var R = B.prototype; If (L === R) {//A's internal attribute __proto__ points to B's prototype object return true; } return false; } when A's __proto__ refers to B's prototype, A is considered to be an instanceof B. //true ({}) instanceof Object; //true new Date() instanceof Date; [] instanceof Object (){}; new Person() instanceof Person; // true new Person instanceof Object; __proto__ === object.prototypeCopy the code

Extension:

Let’s analyze the relationship among [], Array and Object:

[]. Proto points to array. prototype and array. prototype.proto points to Object.prototype. Finally object.prototype. proto points to null, marking the end of the prototype chain.

Valley is:B1. B2 Instanceof ArrayArray Function Object is uppercase)

var b1 = {
  b2: [1, 'abc', console.log],
  b3: function () {
    console.log('b3')
    return function () {
      return 'xfzhang'
   }
  }
}

console.log(b1 instanceof Object, b1 instanceof Array); // true false
console.log(b1.b2 instanceof Array, b1.b2 instanceof Object); // true true
console.log(b1.b3 instanceof Function, b1.b2 instanceof Object); // true true
Copy the code