typeof certains tableaux renverront toujours un objet . string ?
<script> var x = [typeof x, typeof y][1]; console.log(typeof x); </script>
3 Réponses :
typeof est évalué à une chaîne, donc
[<someString>, <someString>]
quel que soit le code x et y > are, se résoudra en un tableau comme
[typeof x, typeof y]
L'accès au [1] ième index de ce tableau vous donnera l'une de ces chaînes, donc typeof donne 'string'.
[typeof x, typeof y] donne la valeur ["undefined", "undefined"] . [typeof x, typeof y] [1] donne la valeur "indéfini" .
Eh bien, typeof renvoie une chaîne, donc tout typeof typeof sera également une chaîne:
var x = [typeof x, typeof y]; console.log(x); //returns ["undefined", "undefined"] var myVar = x[1]; console.log(myVar); //returns "undefined" console.log(typeof myVar); //returns string, because "undefined" is a string
Dans le code ci-dessus, [typeof x, typeof y] est égal à ["undefined", "undefined"] .
Voici une démonstration simple:
var x = [typeof x, typeof y][1]; //x is undefined when this is run console.log(typeof x); //typeof "undefined" == string