typeof στο JavaScript

typeof στο JavaScript

There are 5 different data types that can contain values in JavaScript:

  • string
  • number
  • boolean
  • object
  • function

There are 6 types of objects:

  • Object
  • Date
  • Array
  • String
  • Number
  • Boolean

and 2 data types that cannot contain values:

  • null
  • undefined

typeof operator

You can use typeof The operator to determine the data type of a JavaScript variable.

Παράδειγμα

typeof "Bill"                 // Returns "string"
typeof 3.14                   // Returns "number"
typeof NaN                    // Returns "number"
typeof false                  // Returns "boolean"
typeof [1,2,3,4]              // Returns "object"
typeof {name:'Bill', age:19}  // Returns "object"
typeof new Date()             // Returns "object"
typeof function () {}         // Returns "function"
typeof myCar                  // Returns "undefined" *
typeof null                   // Returns "object"

Δοκιμάστε το προσωπικά

Please note:

  • The data type of NaN is number
  • The data type of array is object
  • The data type of date is object
  • The data type of null is object
  • The data type of an undefined variable is undefined *
  • The data type of an uninitialized variable is also undefined *

You cannot use typeof to determine if a JavaScript object is an array (or date).

Original date

The value of original data refers to a single simple data value without any additional properties or methods.

typeof The operator can return one of the following primitive types:

  • string
  • number
  • boolean
  • undefined

Παράδειγμα

typeof "Bill"              // Returns "string"
typeof 3.14                // Returns "number"
typeof true                // Returns "boolean"
typeof false               // Returns "boolean"
typeof x                   // Returns "undefined" (if x has no value)

Δοκιμάστε το προσωπικά

Complex data

typeof The operator can return one of two complex types:

  • function
  • object

typeof The operator returns "object" for objects, arrays, and null.

typeof The operator does not return "object" for functions.

Παράδειγμα

typeof {name:'Bill', age:19} // Returns "object"
typeof [1,2,3,4]             // Returns "object" (not "array", please note the example below)
typeof null                  // Returns "object"
typeof function myFunc(){}   // Returns "function"

Δοκιμάστε το προσωπικά

typeof The operator returns for arrays "object"because arrays are objects in JavaScript.

typeof data types

typeof The operator is not a variable. It is just an operator. The operator (+ - * /) has no data type.

However,typeof The operator is alwaysReturns a stringincluding the type of the operand).

constructor attribute

constructor Properties return the constructor of all JavaScript variables.

Παράδειγμα

"Bill".constructor                // Returns function String()  {[native code]}
(3.14).constructor                // Returns function Number()  {[native code]}
false.constructor                 // Returns function Boolean() {[native code]}
[1,2,3,4].constructor             // Επιστρέφει function Array()   {[native code]}
{name:'Bill',age:19}.constructor  // Επιστρέφει function Object()  {[native code]}
new Date().constructor            // Επιστρέφει function Date()    {[native code]}
function () {}.constructor        // Επιστρέφει function Function(){[native code]}

Δοκιμάστε το προσωπικά

Μπορείτε να ελέγξετε την ιδιότητα constructor για να διαπιστώσετε αν το αντικείμενο είναι διάνυσμα (περιλαμβανομένων "Array" ή λέξη):

Παράδειγμα

function isArray(myArray) {
  return myArray.constructor.toString().indexOf("Array") > -1;
}

Δοκιμάστε το προσωπικά

ή πιο απλά, μπορείτε να ελέγξετε αν το αντικείμενο είναιΗ συνάρτηση διανύσματος:

Παράδειγμα

function isArray(myArray) {
  return myArray.constructor === Array;
}

Δοκιμάστε το προσωπικά

Μπορείτε να ελέγξετε την ιδιότητα constructor για να διαπιστώσετε αν το αντικείμενο είναι ημερομηνία (περιλαμβανομένων "Date" ή λέξη):

Παράδειγμα

function isDate(myDate) {
  return myDate.constructor.toString().indexOf("Date") > -1;
}

Δοκιμάστε το προσωπικά

ή πιο απλά, μπορείτε να ελέγξετε αν το αντικείμενο είναιΗ ημερομηνία συνάρτηση:

Παράδειγμα

function isDate(myDate) {
  return myDate.constructor === Date;
}

Δοκιμάστε το προσωπικά

Undefined

Στο JavaScript, η τιμή μιας μεταβλητής χωρίς τιμή είναι undefined· και ο τύπος είναι undefined

Παράδειγμα

let car;    // Η τιμή είναι undefined, και ο τύπος είναι undefined.

Δοκιμάστε το προσωπικά

αποθηκεύοντας την τιμή του undefinedμπορεί να καθαρίσει οποιαδήποτε μεταβλητή. Ο τύπος θα είναι undefined

Παράδειγμα

car = undefined;    // Η τιμή είναι undefined, και ο τύπος είναι undefined.

Δοκιμάστε το προσωπικά

Η κενή τιμή

Η κενή τιμή με undefined αδιάφορο.

Η κενή αλφαβητική αλληλουχία έχει νόμιμη τιμή και τύπο.

Παράδειγμα

let car = "";    // Η τιμή είναι "", ο τύπος είναι "string"

Δοκιμάστε το προσωπικά

Null

στο JavaScript null είναι το "μηδέν"· θα έπρεπε να είναι κάτι που δεν υπάρχει.

Δυστυχώς, στο JavaScriptnull Η τύπος των δεδομένων είναι ένα αντικείμενο.

θα μπορούσατε να πείτε ότι είναι ένα bug του JavaScripttypeof null είναι ένα αντικείμενο. Ο τύπος θα πρέπει να είναι null

Μπορείτε να ρυθμίσετε το αντικείμενο ως null Για να καθαρίσετε το αντικείμενο:

Παράδειγμα

let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"};
person = null;    // τώρα η τιμή είναι null αλλά ο τύπος είναι εξακολουθεί να είναι αντικείμενο

Δοκιμάστε το προσωπικά

Μπορείτε επίσης να ρυθμίσετε το αντικείμενο ως undefined Για να καθαρίσετε το αντικείμενο:

Παράδειγμα

let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"};
person = undefined;   // τώρα η τιμή και ο τύπος είναι undefined

Δοκιμάστε το προσωπικά

Η διαφορά μεταξύ undefined και null

undefined και null Τύπος ισότιμος αλλά διαφορετικός:

typeof undefined           // undefined
typeof null                // αντικείμενο
null === undefined         // ψευδές
null == undefined          // αληθές

Δοκιμάστε το προσωπικά