Conversione del tipo JavaScript
- Conversione di stringhe in numeri
- Conversione di numeri in stringhe
- Conversione di date in numeri
- Conversione di numeri in date
- Conversione di booleani in numeri
- Conversione di numeri in booleani
Conversione del tipo JavaScript
Le variabili JavaScript possono essere convertite in una nuova variabile e in un altro tipo di dati:
- Con l'uso di una funzione JavaScript
- Automaticamente dallo stesso JavaScript
Conversione di stringhe in numeri
Il metodo globale Number()
può convertire le stringhe in numeri.
Le stringhe contenenti numeri (come "3.14") vengono convertite in numeri (come 3.14).
Le stringhe vuote vengono convertite in 0.
Qualsiasi altra cosa si converte in NaN
(non un numero).
Number("3.14") // returns 3.14
Number(" ") // returns 0
Number("") // returns 0
Number("99 88") // returns NaN
Metodi numerici
Nel capitolo Metodi numerici , troverai altri metodi che possono essere utilizzati per convertire le stringhe in numeri:
Method | Description |
---|---|
Number() | Returns a number, converted from its argument |
parseFloat() | Parses a string and returns a floating point number |
parseInt() | Parses a string and returns an integer |
L'operatore unario +
L' operatore unario + può essere utilizzato per convertire una variabile in un numero:
Esempio
let y = "5";
// y is a string
let x = + y;
// x is a number
Se la variabile non può essere convertita, diventerà comunque un numero, ma con il valore NaN
(Not a Number):
Esempio
let y = "John";
// y is a string
let x = + y; // x is a number (NaN)
Conversione di numeri in stringhe
Il metodo globale String()
può convertire i numeri in stringhe.
Può essere utilizzato su qualsiasi tipo di numeri, letterali, variabili o espressioni:
Esempio
String(x) // returns a string from a number variable x
String(123) // returns a string from a number literal 123
String(100 + 23) // returns a string from a number from an expression
Il metodo Numero toString()
fa lo stesso.
Esempio
x.toString()
(123).toString()
(100 + 23).toString()
Più metodi
Nel capitolo Metodi numerici , troverai altri metodi che possono essere utilizzati per convertire i numeri in stringhe:
Method | Description |
---|---|
toExponential() | Returns a string, with a number rounded and written using exponential notation. |
toFixed() | Returns a string, with a number rounded and written with a specified number of decimals. |
toPrecision() | Returns a string, with a number written with a specified length |
Conversione di date in numeri
Il metodo globale Number()
può essere utilizzato per convertire le date in numeri.
d = new Date();
Number(d) // returns 1404568027739
Il metodo della data getTime()
fa lo stesso.
d = new Date();
d.getTime() // returns 1404568027739
Conversione di date in stringhe
Il metodo globale String()
può convertire le date in stringhe.
String(Date()) // returns "Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"
Il metodo Date toString()
fa lo stesso.
Esempio
Date().toString() // returns "Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"
Nel capitolo Metodi di data , troverai altri metodi che possono essere usati per convertire le date in stringhe:
Method | Description |
---|---|
getDate() | Get the day as a number (1-31) |
getDay() | Get the weekday a number (0-6) |
getFullYear() | Get the four digit year (yyyy) |
getHours() | Get the hour (0-23) |
getMilliseconds() | Get the milliseconds (0-999) |
getMinutes() | Get the minutes (0-59) |
getMonth() | Get the month (0-11) |
getSeconds() | Get the seconds (0-59) |
getTime() | Get the time (milliseconds since January 1, 1970) |
Conversione di booleani in numeri
Il metodo globale Number()
può anche convertire i valori booleani in numeri.
Number(false) // returns 0
Number(true) // returns 1
Conversione di booleani in stringhe
Il metodo globale String()
può convertire booleani in stringhe.
String(false) // returns "false"
String(true) // returns "true"
Il metodo booleano toString()
fa lo stesso.
false.toString() // returns "false"
true.toString() // returns "true"
Conversione automatica del tipo
Quando JavaScript tenta di operare su un tipo di dati "sbagliato", proverà a convertire il valore in un tipo "giusto".
Il risultato non è sempre quello che ti aspetti:
5 + null // returns 5 because null is converted to 0
"5" + null // returns "5null" because null is converted to "null"
"5" + 2 // returns
"52" because 2 is converted to "2"
"5" - 2 // returns 3 because "5" is converted to 5
"5" * "2" // returns
10 because "5" and "2" are
converted to 5 and 2
Conversione automatica delle stringhe
JavaScript chiama automaticamente la toString()
funzione della variabile quando si tenta di "output" un oggetto o una variabile:
document.getElementById("demo").innerHTML = myVar;
// if myVar = {name:"Fjohn"} // toString converts to "[object Object]"
// if myVar = [1,2,3,4] // toString converts to "1,2,3,4"
// if myVar = new Date() // toString converts to "Fri Jul 18 2014 09:08:55 GMT+0200"
Anche i numeri e i booleani vengono convertiti, ma questo non è molto visibile:
// if myVar = 123 // toString converts to "123"
// if myVar = true // toString converts to "true"
// if myVar = false // toString converts to "false"
Tabella di conversione del tipo JavaScript
Questa tabella mostra il risultato della conversione di diversi valori JavaScript in Number, String e Boolean:
Original Value |
Converted to Number |
Converted to String |
Converted to Boolean |
Try it |
---|---|---|---|---|
false | 0 | "false" | false | |
true | 1 | "true" | true | |
0 | 0 | "0" | false | |
1 | 1 | "1" | true | |
"0" | 0 | "0" | true | |
"000" | 0 | "000" | true | |
"1" | 1 | "1" | true | |
NaN | NaN | "NaN" | false | |
Infinity | Infinity | "Infinity" | true | |
-Infinity | -Infinity | "-Infinity" | true | |
"" | 0 | "" | false | |
"20" | 20 | "20" | true | |
"twenty" | NaN | "twenty" | true | |
[ ] | 0 | "" | true | |
[20] | 20 | "20" | true | |
[10,20] | NaN | "10,20" | true | |
["twenty"] | NaN | "twenty" | true | |
["ten","twenty"] | NaN | "ten,twenty" | true | |
function(){} | NaN | "function(){}" | true | |
{ } | NaN | "[object Object]" | true | |
null | 0 | "null" | false | |
undefined | NaN | "undefined" | false |
I valori tra virgolette indicano valori di stringa.
I valori rossi indicano valori (alcuni) che i programmatori potrebbero non aspettarsi.