Tipi di dati JavaScript
- Pagina precedente Assegnazione JS
- Pagina successiva Funzioni JS
valori di stringa, valori numerici, valori booleani, array, oggetti.
Tipi di dati JavaScript
Le variabili JavaScript possono salvare molti tipi di datiTipi di dati:valori numerici, valori di stringa, array, oggetti, eccetera:
var length = 7; // Numero var lastName = "Gates"; // Stringa var cars = ["Porsche", "Volvo", "BMW"]; // Array var x = {firstName:"Bill", lastName:"Gates"}; // Oggetto
Il concetto di tipo di dati
Nel processo di programmazione, il concetto di tipo di dati è fondamentale.
Per poter manipolare le variabili, è importante comprendere i tipi di dati.
Senza un tipo di dati, il computer non può risolvere questo problema in modo sicuro:
var x = 911 + \
Ha senso aggiungere 911 a "Volvo"? Questo comportamento produrrà un errore o un risultato?
JavaScript gestisce così l'esempio sopra:
var x = "911" + "Porsche";
Quando si aggiunge un numero a una stringa, JavaScript considera il numero come stringa.
Esempio
var x = 911 + \
Esempio
var x = \
JavaScript calcola l'espressione da sinistra a destra. Diversi ordini possono produrre risultati diversi:
JavaScript:
var x = 911 + 7 + \
Risultato:
918Porsche
JavaScript:
var x = \
Risultato:
Porsche9117
Nel primo esempio, JavaScript considera 911 e 7 come numeri fino a quando incontra \
Nel secondo esempio, poiché il primo operando è una stringa, tutti gli operandi vengono trattati come stringhe.
JavaScript ha un tipo di dati dinamico
JavaScript ha un tipo di dati dinamico. Questo significa che la stessa variabile può essere usata per diversi tipi:
Esempio
var x; // Ora x è undefined var x = 7; // Ora x è un numero var x = \
Valori di stringa JavaScript
Una stringa (o stringa di testo) è una serie di caratteri (ad esempio \
Le stringhe sono circondate da virgolette. Puoi usare virgolette singole o doppi:
Esempio
var carName = \ var carName = 'Porsche 911'; // Usa gli apici singoli
Puoi usare virgolette all'interno di una stringa, purché non corrispondano alle virgolette che circondano la stringa:
Esempio
var answer = \ var answer = \ var answer = 'He is called \
Imparerai di più sulle stringhe in questo tutorial.
Numeri JavaScript
JavaScript ha un solo tipo di numero.
Puoi scrivere numeri con o senza punto decimale:
Esempio
var x1 = 34.00; // Con punto decimale var x2 = 34; // Senza punto decimale
Valori estremamente grandi o piccoli possono essere scritti in notazione scientifica:
Esempio
var y = 123e5; // 12300000 var y = 123e5; // 12300000
var z = 123e-5; // 0.00123
You will learn more about numbers in this tutorial.
JavaScript boolean valuesBoolean values have only two values:
true or
.
Esempio
false var x = true;
var y = false;
Boolean values are often used in conditional tests.
Array JavaScript
You will learn more about conditional tests in this tutorial.
JavaScript arrays are written using square brackets.
Array items are separated by commas.
Esempio
The following code declares (creates) an array named cars containing three items (car brands):
var cars = ["Porsche", "Volvo", "BMW"];
Array indices are based on zero, which means the first item is [0], the second item is [1], and so on.
Oggetti JavaScript
You will learn more about arrays in this tutorial.
JavaScript objects are written using curly braces. Object properties arename: value
Esempio
Yes, separated by commas.
var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"};
In the example above, the object (person) has four properties: firstName, lastName, age, and eyeColor.
typeof operator
You will learn more about JavaScript in this tutorial. typeof
To determine the type of a JavaScript variable:
The typeof operator returns the type of a variable or expression:
Esempio
typeof "" // Returns "string" typeof "Bill" // restituisce "string" typeof "Bill Gates" // Returns "string"
Esempio
typeof 0 // Returns "number" typeof 314 // Returns "number" typeof 3.14 // restituisce "number" typeof (7) // Returns "number" typeof (7 + 8) // Returns "number"
The typeof operator returns "object" for arrays because in JavaScript arrays are objects.
Undefined
In JavaScript, a variable without a value has the value undefined
。typeof restituisce anche undefined
.
Esempio
var person; // il valore è undefined, il tipo è undefined.
Ogni variabile può impostare il valore su undefined
svuotare. Il tipo sarà anche undefined
.
Esempio
person = undefined; // il valore è undefined, il tipo è undefined.
Valore vuoto
Valore vuoto e undefined
Non è la stessa cosa.
Una variabile di stringa vuota ha sia un valore che un tipo.
Esempio
var car = ""; // il valore è "", il tipo è "string"
Null
In JavaScript,null
è "nothing". È considerato un'entità inesistente.
Sfortunatamente, in JavaScript,null
i dati sono di tipo oggetto.
Puoi considerare null
In JavaScript, la comprensione di un oggetto è considerata un bug. Dovrebbe essere null
.
Puoi impostare il valore su null
svuotare un oggetto:
Esempio
var person = null; // il valore è null, ma il tipo è ancora oggetto
Puoi anche impostare il valore su undefined
svuotare un oggetto:
Esempio
var person = undefined; // il valore è undefined, il tipo è undefined.
La differenza tra Undefined e Null
Undefined
con null
hanno valori uguali ma tipi diversi:
typeof undefined // undefined typeof null // object null === undefined // false null == undefined // true
Dati primitivi
Un valore di dati primitivi è un valore semplice e singolo senza proprietà o metodi aggiuntivi.
typeof
un operatore può restituire uno dei seguenti tipi primitivi:
- string
- number
- boolean
- undefined
Esempio
typeof "Bill" // restituisce "string" typeof 3.14 // restituisce "number" typeof true // restituisce "boolean" typeof false // restituisce "boolean" typeof x // Restituisce "undefined" (se x non ha un valore)
Dati complessi
typeof
L'operatore può restituire uno dei due tipi seguenti:
- funzione
- Oggetto
typeof
L'operatore restituisce oggetti, array o null
Restituzione Oggetto
.
typeof
L'operatore non restituisce la funzione Oggetto
.
Esempio
typeof {name:'Bill', age:62} // Restituisce "object" typeof [1,2,3,4] // Restituisce "object" (non "array", vedere i commenti seguenti) typeof null // Restituisce "object" typeof function myFunc(){} // Restituisce "function"
typeof
L'operatore restituisce l'array come "Oggetto
Perché in JavaScript l'array è un oggetto.
Lettura extra
Tutorial avanzato JavaScript:
- Pagina precedente Assegnazione JS
- Pagina successiva Funzioni JS