Aggiungere numeri e stringhe




' Aggiungere numeri e stringhe

' AVVERTIMENTO !!


' JavaScript utilizza l'operatore + sia per l'aggiunta che per la concatenazione.


' I numeri sono aggiunti. Le stringhe sono concatenate.

' var x = 10;

' var y = 20;

' var z = x + y; // z will be 30 (a number)


'Se aggiungi due stringhe, il risultato sara' una concatenazione di stringhe:


'Esempio

'var x = "10";

'var y = "20";

'var z = x + y; // z will be 1020 (a string)



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

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Numbers</h2>

<p>If you add two numeric strings, the result will be a concatenated string:</p>

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

<script>
var x = "10";
var y = "20";
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>

' oppure

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

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Numbers</h2>

<p>If you add two numbers, the result will be a number:</p>

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

<script>
var x = 10;
var y = 20;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>


' oppure

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

' The result is: 1020


<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Numbers</h2>

<p>If you add a numeric string and a number, the result will be a concatenated string:</p>

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

<script>
var x = "10";
var y = 20;
document.getElementById("demo").innerHTML =
"The result is: " + x + y;
</script>

</body>
</html>



' Oppure

https://www.w3schools.com/js/tryit.asp?filename=tryjs_numbers_add_strings4
' A common mistake is to expect this result to be 102030:

' 3030


<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Numbers</h2>

<p>A common mistake is to expect this result to be 102030:</p>

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

<script>
var x = 10;
var y = 20;
var z = "30";
var result = x + y + z;
document.getElementById("demo").innerHTML = result;
</script>

</body>
</html>



' JavaScript provera' a convertire stringhe in numeri in tutte le operazioni numeriche:

' Questo funzionera':

'var x = "100";

'var y = "10";

'var z = x / y; // z will be 10


'Questo funzionera' anche:

'var x = "100";

'var y = "10";

'var z = x * y; // z will be 1000


'E questo funzionera':

'var x = "100";

'var y = "10";

'var z = x - y; // z will be 90


'Ma questo non funzionera':

'var x = "100";

'var y = "10";

'var z = x + y; // z will not be 110 (It will be 10010)












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