JavaScript Basic: Three Useful B
In the last post, I shared five primitive data types. Even though I could write some basic variables and do some math in the console, I don’t see any action from the website.
How to let website take action?
We will need to know three built-in methods:
Alert
It pops up a message to the user, and it alerts the user.
Console.log
What console.log will do is print something to the JavaScript console.
Prompt
Prompt is very useful for us because it lets us get input from a user. The second part of prompt is that we can store it in a variable.
Now I am going to apply those three built-in methods in HTML, so you can see how they work.
Firstly, I create a simple HTML page, and I only add the title. Then I add a <script> tag and link to a separate file named “script.js”.
I want my site to pop up a message when you open it. In the “script. js” file, I name my variable as “userName”, and it equal to prompt(“What is your name?”).
It allows users to enter their name in the pop up window.
Then I create an alert message which is alert (“Nice to meet you,” + userName).
Once I enter the name “Marry”, the userName variable value will be stored. After I enter the name and click OK, the site pops up a new alert message “Nice to meet you, Marry”.
For the console.log part, we can’t see it on the site, but we can find it out by inspecting the site. It is located on the “console” place.