Type Conversion






? Number () viene convertito in numero, String () viene convertito in stringa, booleano () viene convertito in booleano.
? Tipi di dati JavaScript
? In JavaScript ci sono 5 diversi tipi di dati che possono contenere valori:
?string
?number
?boolean
?object
?function
?Esistono 6 tipi di oggetti:
?Object
?Date
?Array
?String
?Number
?Boolean
?E 2 tipi di dati che non possono contenere valori:
?null
?undefined
?Il tipo di operatore
? ? possibile utilizzare l? typeofoperatore per trovare il tipo di dati di una variabile JavaScript.

https://www.w3schools.com/js/tryit.asp?filename=tryjs_typeof_all

<!DOCTYPE html>
<html>
<body>

<h2>The JavaScript typeof Operator</h2>

<p>The typeof operator returns the type of a variable, object, function or expression.</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof NaN + "<br>" +
typeof false + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof {name:?john?, age:34} + "<br>" +
typeof new Date() + "<br>" +
typeof function () {} + "<br>" +
typeof myCar + "<br>" +
typeof null;
</script>

</body>
</html>

?Il tipo di dati di typeof
?L? typeof operatore non ? una variabile. ? un operatore. Gli operatori (+ - * /) non hanno alcun tipo di dati.
?Ma l? typeof operatore restituisce sempre una stringa (contenente il tipo di operando).
?La propriet? del costruttore
?La constructor propriet? restituisce la funzione di costruzione per tutte le variabili JavaScript.

https://www.w3schools.com/js/tryit.asp?filename=tryjs_constructor_all

<!DOCTYPE html>
<html>
<body>

<h2>The JavaScript constructor Property</h2>

<p>The constructor property returns the constructor function for a variable or an
object.</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
"john".constructor + "<br>" +
(3.14).constructor + "<br>" +
false.constructor + "<br>" +
[1,2,3,4].constructor + "<br>" +
{name:?john?, age:34}.constructor + "<br>" +
new Date().constructor + "<br>" +
function () {}.constructor;
</script>

</body>
</html>

? ? possibile controllare la propriet? del costruttore per scoprire se un oggetto ? un Array (contiene la parola "Array"):
https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_isarray

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>

<p>This "home made" isArray() function returns true when used on an array:</p>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = isArray(fruits);

function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
</script>

</body>
</html>

? O ancora pi? semplice, puoi verificare se l?oggetto ? una funzione Array
https://www.w3schools.com/js/tryit.asp?filename=tryjs_type_isarray

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Object</h2>
<p>This "home made" isArray() function returns true when used on an array:</p>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = isArray(fruits);

function isArray(myArray) {
return myArray.constructor === Array;
}
</script>

</body>
</html>

' ? possibile controllare la propriet? del costruttore per scoprire se un oggetto ? un Date(contiene la parola "Data"):

https://www.w3schools.com/js/tryit.asp?filename=tryjs_date_isdate

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Date Object</h2>
<p>This "home made" isDate() function returns true when used on an date:</p>

<p id="demo"></p>

<script>
var myDate = new Date();
document.getElementById("demo").innerHTML = isDate(myDate);

function isDate(myDate) {
return myDate.constructor.toString().indexOf("Date") > -1;
}
</script>

</body>
</html>

' O ancora pi? semplice, puoi verificare se l'oggetto ? una funzione Date :

https://www.w3schools.com/js/tryit.asp?filename=tryjs_type_isdate

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Date Object</h2>
<p>This "home made" isDate() function returns true when used on an date:</p>

<p id="demo"></p>

<script>
var myDate = new Date();
document.getElementById("demo").innerHTML = isDate(myDate);

function isDate(myDate) {
return myDate.constructor === Date;
}
</script>

</body>
</html>













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