Table of contents
Alert & Prompt
There are two ways to take input in JavaScript: Alert & Prompt.
Alert
An alert displays a message on the page.
alert("something is wrong");
Prompt
A prompt displays a dialog box that asks the user for some input.
prompt("please enter your roll no");
IMPORTANT
Function parseInt
- A prompt returns a string.
To convert the prompt's string to an integer, we useparseInt
.
The purpose ofparseInt
is to convert whatever we input into an integer.
Alert
An alert displays text on the website
Example
Whenever we need to create an alert on the screen in JavaScript, we write
alert
.
Syntax
alert("something is wrong");
Program
Code
alert("something is wrong");
Output
Console Types
console.log
console.error
console.warn
console.log
is used to print anything on the screen.
Example
console.log("This is a simple log");
console.error
is used to print any error on the screen.
Example
console.error("This is an error message");
console.warn
is used to print any warning on the screen.
Example
console.warn("This is a warning message");
Program
Code
console.log("This is a simple log");
console.error("This is an error msg");
console.warn("This is a warning msg");
Output
Prompt
Prompts are similar to alerts. However, a prompt contains a dialog box that can take any value from the user.
Example
Syntax
prompt("please enter your name: ");
HOW TO see prompt value
We can store the value from a prompt in a variable.
Explain Steps
Write the code:
let firstname = prompt("please enter your name: "); console.log(firstname);
Enter your name.
Check your console.
Create a Program
Q.1
let firstname = prompt("Enter first name");
let lastname = prompt("Enter last name");
console.log("Welcome", firstname, lastname, "!");
show in web page
console
Q.2
code
let firstname = prompt("Enter first name");
let lastname = prompt("Enter last name");
let msg = "Welcome " + firstname + " " + lastname + "!";
alert(msg);