Il tipo di operatore




Č possibile utilizzare l' typeofoperatore JavaScript per trovare il tipo di una variabile JavaScript.

L' typeof operatore restituisce il tipo di una variabile o un'espressione:

Esempio
typeof "" // Returns "string"
typeof "John" // Returns "string"
typeof "John Doe" // Returns "string"

typeof 0 // Returns "number"
typeof 314 // Returns "number"
typeof 3.14 // Returns "number"
typeof (3) // Returns "number"
typeof (3 + 4) // Returns "number"

n JavaScript, una variabile senza valore ha il valore undefined. Il tipo e' anche undefined.

Esempio
var car; // Value is undefined, type is undefined

Qualsiasi variabile puo' essere svuotata, impostando il valore su undefined. Anche il tipo sara' undefined.

Esempio
car = undefined; // Value is undefined, type is undefined
Valori vuoti
Un valore vuoto non ha nulla a che fare con undefined.

Una stringa vuota ha sia un valore legale che un tipo.
Nullo
In JavaScript nulle' "niente". Dovrebbe essere qualcosa che non esiste.

Sfortunatamente, in JavaScript, il tipo di dati di nulle' un oggetto.

Puoi considerarlo un bug in JavaScript che typeof nulle' un oggetto. Dovrebbe essere null.

Puoi svuotare un oggetto impostandolo su null:

Esempio
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null; // Now value is null, but type is still an object
Esempio
var car = ""; // The value is "", the typeof is "string"










( iltipodioperatore.html )- by Paolo Puglisi - Modifica del 17/12/2023