typeof ng JavaScript

typeof ng 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 operator to determine the data type of JavaScript variables.

Example

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"

Try It Yourself

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

Original data value refers to a single simple data value without any additional properties or methods.

typeof Operator may return one of the following original types:

  • string
  • number
  • boolean
  • undefined

Example

typeof "Bill"              // Nangyari ang "string"
typeof 3.14                // Nangyari ang "number"
typeof true                // Nangyari ang "boolean"
typeof false               // Nangyari ang "boolean"
typeof x                   // Nangyari ang "undefined" (kung walang halaga ang x)

Try It Yourself

Mga kompleksong data

typeof Ang operator ay maaaring ibigay ang isa sa dalawang kompleksong uri ng data:

  • function
  • object

typeof Ang operator ay magbibigay ng "object" para sa object, array at null.

typeof Hindi magbibigay ang operator ng "object" para sa function.

Example

typeof {name:'Bill', age:19} // Nangyari ang "object"
typeof [1,2,3,4]             // Nangyari ang "object"( hindi "array", mangyaring pagsiyasat ang halimbawa sa ibaba )
typeof null                  // Nangyari ang "object"
typeof function myFunc(){}   // Nangyari ang "function"

Try It Yourself

typeof Ang operator ay magbibigay ng "object" para sa array. "object", sapagkat ang array ay isang object sa JavaScript.

Ang uri ng data ng typeof

typeof Ang operator ay hindi variable. Ito ay isang operator lamang. Ang operator (+ - * /) ay walang uri ng data.

Ngunit,typeof Ang operator ay palagingIbibigay ang string( kasama ang uri ng operand )。

Property na constructor

constructor Ang property ay ibibigay ang constructor ng lahat ng variable ng JavaScript.

Example

"Bill".constructor                // Nangyari ang function String()  {[native code]}
(3.14).constructor                // Nangyari ang function Number()  {[native code]}
false.constructor                 // Nangyari ang function Boolean() {[native code]}
[1,2,3,4].constructor             // ibibigay ang function Array()   {[native code]}
{name:'Bill',age:19}.constructor  // ibibigay ang function Object()  {[native code]}
new Date().constructor            // ibibigay ang function Date()    {[native code]}
function () {}.constructor        // ibibigay ang function Function(){[native code]}

Try It Yourself

Maaari mong suriin ang property na constructor upang matukoy kung ang bagay ay array (kasama ang "Array" isang salita):

Example

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

Try It Yourself

o mas madali, maaari mong suriin kung ang bagay ay isang object na may uriang function ng array:

Example

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

Try It Yourself

Maaari mong suriin ang property na constructor upang matukoy kung ang bagay ay petsa (kasama ang "Date" isang salita):

Example

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

Try It Yourself

o mas madali, maaari mong suriin kung ang bagay ay isang object na may uriang function ng petsa:

Example

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

Try It Yourself

Undefined

Sa JavaScript, ang halaga ng variable na walang halaga ay: undefined. Ang uri din ay: undefined.

Example

let car;    // Ang halaga ay undefined, ang uri din ay undefined.

Try It Yourself

sa pamamagitan ng pagtatalaga ng halagang undefinedmaaaaring magpahina ang anumang variable. Ang uri din ay: undefined.

Example

car = undefined;    // Ang halaga ay undefined, ang uri din ay undefined.

Try It Yourself

walang laman na halaga

Ang walang laman na halaga ay: undefined walang kaugnayan.

Ang walang laman na string ay may lehitimong halaga at uri.

Example

let car = "";    // Ang halaga ay "", ang uri ay 'string'

Try It Yourself

Null

sa JavaScript: null ay 'wala'. Dapat ito ay isang wala sa eksistensya.

Sa kasamaan, sa JavaScript:null Ang uri ng kategorya ay isang bagay.

You can think of it as a bug in JavaScript.typeof null is an object. The type should be null.

You can set an object to null To clear an object:

Example

let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"};
person = null;    // Now the value is null, but the type is still object

Try It Yourself

You can also clear an object by setting it to undefined To clear an object:

Example

let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"};
person = undefined;   // Now the value and type are both undefined

Try It Yourself

Difference between undefined and null

undefined and null Values are equal but types are different:

typeof undefined           // undefined
typeof null                // object
null === undefined         // false
null == undefined          // true

Try It Yourself