
Variables: More examples (JavaScript programming)

Upgrade for more content
True or false? To assign a value to the variable, we write an equals sign.
Pseudocode shows what we want a program to do. Now we will translate pseudocode into the programming language JavaScript. This is what the pseudocode looks like: First we want to note name and age for two users. Then we want to calculate the age difference and output the result. Here's what we do: First let’s translate the row “Note name one”.
We do this by creating - declaring - a variable with the JavaScript-instruction var, and give the variable the name “n1” To assign a value to this variable, we write an equals sign. It’s the user who will input the value, and therefore the JavaScript-instruction prompt is used, with two parentheses. We are asking the user for “first person’s name”. The question is a text to be displayed on the screen. We tell the computer that by putting quotation marks around the text.
Now, we have finished writing this instruction, and we show that by writing a semicolon at the end of the row. Now to the next row in the pseudocode. "Note age one" We note the age of the first person by creating a new variable "var age1" and an equals sign to assign a value to this variable. Like before, it is the user who inputs the value. Therefore, we use the JavaScript-instruction prompt again. We ask for the first person's age by inputting this text and putting quotation marks around it.
End the instruction as before, with a semicolon. Now we are going to translate the pseudocode "Note name two" and "Note age two". Therefore we write the same as before and note the second person's name as variable n2 and the second person's age as variable age2 And we always remember the semicolon and quotation marks. The next row in the pseudocode should calculate age one minus age two and save the result. We create the variable "result" to note the answer.
Assign a value to the variable result by using an equals sign. The value is equal to age1 minus age2 End the instruction with a semicolon. Finally, the program will output the results on the screen. We use the JavaScript instruction: 'console' dot 'log' and then two parentheses. The pseudocode says that we should write: "Name one is result years older than name two" The value for name one is “n1”, the value for result is "result" and the value for name two is “n2”.
We will put together a sentence with variables and words. To do that, we use plus signs. When we input words to be written on the screen, we need to put quotation marks around them. And then we have to insert spaces between words and quotation marks. Otherwise, the computer outputs everything into a single long word.
End the instruction with a semicolon, as always in JavaScript. Now let’s test the code. The program asks the user to put in the first person’s name Lina and age 16 The program asks for the second person’s name. Maria and age 15 Then the computer will output: Lina is one years older than Maria. And that's right!
Lina is 16 years old, and Maria is 15. It worked. Now you can ask other questions and calculate new things yourself.