JavaScript is a programming language that is commonly used to add functionality and logic to websites. Some of the things it allows you to do are create things like pop-up alerts, prompts for user input, and interactive content on your website.
To create a pop-up alert in JavaScript, you can use the alert() function. For example:
alert("Hello, world!");
This will display a pop-up window with the message “Hello, world!”.
Another way to output information in JavaScript is with the console.log() function. This will print the provided message to the “console” in your web browser, which is a tool that developers use to see information about the website they are working on. For example:
console.log("Hello, world!");
This will display
Hello, world!
in your browser console
You can also output information to the web page itself using the document.write() function. For example:
document.write("Hello, world!");
This will insert the provided content into the web page at the location where the function is called.
In addition to outputting information, JavaScript also allows you to get input from the user. One way to do this is with the prompt() function, which will display a pop-up window with a text field for the user to enter a value. For example:
let userInput = prompt("Please enter your name:");
The prompt() function returns the value that the user entered, which can be stored in a variable for later use.
Another way to get input from the user is with the confirm() function, which displays a pop-up window with a “Confirm” or “Cancel” button. This can be used to ask the user a yes or no question. For example:
let userConfirmed = confirm("Are you sure you want to do this?");
The confirm() function returns true if the user clicked “Confirm”, and false if they clicked “Cancel”. This value can also be stored in a variable for later use.
These are just a few examples of the basics of JavaScript and its input, output functions.
References: