-
Original : type MDN
-
Fonction : L'
typeof
opérateur renvoie une chaîne indiquant le type de l'opérande non évalué. -
Méthode :
typeof operand
outypeof(operand)
operand
: Une expression représentant un objet ou une valeur primitive, dont le type sera renvoyé.
-
Description :
Voici les typeof
valeurs de retour possibles:
Types de | résultat |
---|---|
Indéfini | 'indéfini' |
Nul | 'objet' |
Booléen | 'boolead' |
Nombre | 'nombre' |
BigInt | «bigint» |
Chaîne | 'chaîne' |
symbole | 'symbole' |
Fonction | 'fonction' |
Tout autre objet | 'objet' |
- Code :
/**
* @name typeof测试
* @description 通过 typeof 检测各个数据类型的返回
*/
const test = {
testUndefined: undefined,
testNull: null,
testBoolean: true,
testNumber: 123,
testBigInt: BigInt(1234), // 大于 2 的 53 次方算 BigInt
testString: '123',
testSymbol: Symbol(),
testFunction: function() {
console.log('function');
},
testObject: {
obj: 'yes',
},
testObjectString: new String('String'),
testObjectNumber: new Number(123),
}
console.log(typeof(test.testUndefined)); // undefined
console.log(typeof(test.testNull)); // object
console.log(typeof(test.testBoolean)); // boolean
console.log(typeof(test.testNumber)); // number
console.log(typeof(test.testBigInt)); // bigint
console.log(typeof(test.testString)); // string
console.log(typeof(test.testSymbol)); // symbol
console.log(typeof(test.testFunction)); // function
console.log(typeof(test.testObject)); // object
console.log(typeof(test.testObjectString)); // object
console.log(typeof(test.testObjectNumber)); // object