Strings, Casting and User Input
Strings
Text is represented as a string, a sequence of characters: letters, digits, punctuation, spaces. You mark a value as a string by wrapping it in quotation marks, and JavaScript accepts either style. Single and double quotes mean exactly the same thing, so pick one and stay with it.Text written directly into your code this way is called a string literal, because the value is literally spelled out where you can see it.
Joining strings
The + operator concatenates two strings, joining them end to end with nothing in between. If you want a space between them, you have to supply that space yourself.Asking what type a value is
typeof reports the type of a value. It is an operator rather than a function, so it needs no parentheses, and what it hands back is itself a short string.| Expression | It hands back |
|---|---|
typeof "Ceres" | the text string |
typeof 400 | the text number |
typeof 12.5 | the text number, the same as 400 |
typeof false | the text boolean |
Casting
Turning a value of one type into a value of another type is called casting. Four calls do almost all of this work.| Call | Result |
|---|---|
Number("76") | the value 76, ready for arithmetic |
String(76) | the text 76, ready for joining |
parseInt("76 miles") | the value 76: it reads digits from the front and stops at the first character that is not one, so the trailing text is ignored |
Math.trunc(x) | whatever sits in front of the decimal point, with the rest dropped rather than rounded |
The program below joins strings with + and casts a number two ways. Run it, then change it and run it again:
• Delete the spaces inside
" is a ", so that it reads "isa".• Change
"hamster" to 'gerbil', with single quotes.• Change 93.7 to 59.99.
let pet = "Biscuit";
let kind = "hamster";
console.log(pet + " is a " + kind);
let weight = 93.7;
console.log("Whole grams:", Math.trunc(weight));
console.log("As text: " + String(weight));
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Reading input from the user
Program input is data sent to a program to be processed, and it arrives in many forms: a swipe on a tablet, a voice recording, an image, text typed at a keyboard. Program output is data travelling the other way, from the program out to a device.The prompt() function collects text input. The program stops, shows the message you pass it, and waits until the user answers.
Turning input into a value you can compute with
Because prompt() gives you text, you have to cast it before doing arithmetic. You can cast on a line of its own, as below, or fold both calls onto a single line. Folding them works because in a composition the inner call runs first and its result becomes the input to the outer one, the same way f(g(2)) works in your math class.The program below reads a bus stop and a number of minutes, then prints a sentence built with + and a sum. Run it, and type a stop name and a number of minutes when it asks. Then try these:
• Run it again and type 7.5 for the minutes.
• Change
wait + ride to reply + ride, run it, and type the same answers again.let stop = prompt("Your bus stop: ");
let reply = prompt("Minutes until the bus: ");
let wait = Number(reply);
let ride = 36;
console.log("Waiting at " + stop + " for " + String(wait) + " minutes");
console.log("Minutes until you arrive:", wait + ride);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.