Learn Create Your Own Split Screen

Next Page

Sometimes variables really want to stay strings. Try the example below. Enter 5 when prompted for the number of oranges.

The answer keeps coming out as "35" even though we're using the parenthesis technique.

This is because the prompt function is unambiguously returning a string, and we need to force Javascript to convert oranges from a string to a number. Fortunately, there's a built-in function called Number().

Try altering the program below to use the Number() function on the oranges variable, and check it does the right thing.

alert("You have a total of " + (apples + Number(oranges)) + " pieces of fruit");