
Variables: Introduction (JavaScript programming)

Upgrade for more content
True or false? JavaScript is a programming language.
Pseudocode describes what a program should do. Humans understand pseudocode, but computers don’t. We need to translate the code to a language that the computer understands: a programming language. One of them is JavaScript. Here's what Lina's pseudocode looks like: “Ask the user to enter their name” “Note name” “Say hello name” Lina keeps the pseudocode on the screen, to refer to when she writes the JavaScript-code.
To show that these are not instructions for the computer, she writes a slash first and then a star, at the start of the pseudocode. and ends the last row with a star first and then a slash. Now Lina will translate the first row of the pseudocode: “Ask the user to enter their name” Lina writes the word ”prompt” - which is an instruction in JavaScript, then some parentheses. Within these parentheses, she writes the text she wants to be shown on the screen: “Input your name” “colon” Then she must write quotation marks around the text. The quotation marks are important.
They show that “Input your name” is text. Now, Lina has finished writing the instruction, and shows this by writing a semicolon at the end of the row. Lina is testing her code. A new window opens, and there is the text on the screen. And now the program waits for the user to write their name.
That was the first row in the pseudocode. The second row is ‘note name’. Which means: Tell the program to save what the user writes. Lina does that using something called a variable. She creates a variable called ’name’, by writing: ’var name’ and an equal sign in front of the instruction 'prompt'.
Every time a user enters their name it is saved as the variable: ’name’. Now, how should Lina translate the next line into JavaScript the row “Say hello name”? She uses the instruction ‘console’ dot ’log’, and then parentheses. Inside the parentheses she writes: Quotation mark, Hello, and leaves a space before another quotation mark and then a plus sign, to show that she will add something: the variable ’name’. She ends the instruction with a semicolon, after the parentheses.
But wait! Recently, Lina used the ’prompt’ instruction to write text on the screen. Now she is using ’console’ dot ’log’? What’s the difference? Well, Lina wanted the user to write something into the program.
Then she used ‘prompt’. Then Lina wanted the program to write something on the screen, to the user. Then she used ’console’ dot ’log’ Now she tests the code. Lina writes her own name, and clicks okay then the computer outputs: “Hello Lina”. It worked!
Lina has translated the pseudocode into JavaScript-code. If Lina wants the program to ask more questions, such as, “What is the user's favourite subject in school?” How would she do that? Pause the video and think. Have you figured it out? She uses a similar instruction as when asking for the user’s name.
Lina creates another variable, which she calls, ’subject’ She assigns it a value using this code: equals ’prompt’ parenthesis quotation mark And the text: What is your favorite subject in school? quotation mark end parenthesis and semicolon. Now she adds yet another instruction: ’console’ dot ’log’ Within these parentheses she writes: quotation mark, “Oh you like” a space a quotation mark, a plus sign, and the variable “subject”. After the parentheses, she puts a semicolon. Now Lina is going to test the program.
She writes her name. She clicks ‘okay’. And writes her favourite subject 'Maths' She clicks ‘okay’ again. And the program outputs: “Hello Lina! Oh, you like Maths.” Good it works.
Now you can try getting the program to ask even more questions.