Alert & Prompt

Alert & Prompt

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 use parseInt.
    The purpose of parseInt 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

  1. Write the code:

     let firstname = prompt("please enter your name: ");
     console.log(firstname);
    
  2. Enter your name.

  3. 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);

show in web page

output