Create a web-page that asks for a name and outputs it.
<html>
<body>
<script>
let name = prompt("What is your name?", "");
alert(name);
</script>
</body>
</html>
Type Conversions
Most of the time, operators and functions automatically convert the values given to
them to the right type.
For example, alert automatically converts any value to a string to show it.
Mathematical operations convert values to numbers.
String Conversion
String conversion happens when we need the string form of a value.
For example, alert(value) does it to show the value.
We can also call the String(value) function to convert a value to a string:
let value = true;
alert(typeof value); // boolean
value = String(value); // now value is a string "true"
alert(typeof value); // string
Numeric Conversion
Numeric conversion in mathematical functions and expressions happens automatically.
For example, when division / is applied to non-numbers:
alert( "6" / "2" ); // 3, strings are converted to numbers
We can use the Number(value) function to explicitly convert a value to a number:
let str = "123";
alert(typeof str); // string
let num = Number(str); // becomes a number 123
alert(typeof num); // number
Basic operators, maths
The following math operations are supported:
Addition +,
Subtraction -,
Multiplication *,
Division /,
Remainder %,
Exponentiation **.
Remainder % The remainder operator %, despite its appearance, is not related to percents.
The result of a % b is the remainder of the integer division of a by b.
For instance:
alert( 5 % 2 ); // 1, the remainder of 5 divided by 2
alert( 8 % 3 ); // 2, the remainder of 8 divided by 3
Exponentiation ** The exponentiation operator a ** b raises a to the power of b.
In school maths, we write that as ab.
For instance:
alert( 2 ** 2 ); // 2² = 4
alert( 2 ** 3 ); // 2³ = 8
A code that asks the user for two numbers and shows their sum.
<html>
<head></head>
<body>
<script>
let x=prompt('Enter a number:','1');
let y=prompt('Enter another number:','2');
let z=Number(x)+Number(y);
alert(`Your value ${z}`);
</script>
</body>
</html>
String concatenation with binary +
If the binary + is applied to strings, it merges (concatenates) them:
let s = "my" + "string";
alert(s); // mystring
Note that if any of the operands is a string, then the other one is converted to a string too.
For example:
alert( '1' + 2 ); // "12"
alert( 2 + '1' ); // "21"
It doesn’t matter whether the first operand is a string or the second one.
Example:
alert(2 + 2 + '1' ); // "41" and not "221"
The binary + is the only operator that supports strings in such a way. Other arithmetic operators work
only with numbers and always convert their operands to numbers.
Here’s the subtraction and division:
alert( 6 - '2' ); // 4, converts '2' to a number
alert( '6' / '2' ); // 3, converts both operands to numbers
Task:
What are the final values of all variables a, b, c and d after the code below?
let a = 1, b = 1;
let c = ++a; // ?
let d = b++; // ?
What are the values of a and x after the code below?
let a = 2;
let x = 1 + (a *= 2);
What are results of these expressions?
1. "" + 1 + 0
2. "" - 1 + 0
3. true + false
4. 6 / "3"
5. "2" * "3"
6. 4 + 5 + "px"
7. "$" + 4 + 5
8. "4" - 2
9. "4px" - 2
10. " -9 " + 5
11. " -9 " - 5
12. null + 1
13. undefined + 1
14. " \t \n" - 2