Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 4 - Basic Interaction

4.3 Confirm

The confirm method is used to ask the user to confirm or cancel an action, displaying a dialog box with a message and OK and Cancel buttons.

let result = confirm("Are you sure you want to exit?");
if (result) {
    console.log("Action confirmed.");
} else {
    console.log("Action cancelled.");
}

Now, let's create a program that takes two inputs from the user (first name and last name) and then displays the complete name.

let firstName = prompt("Enter first name:");
let lastName = prompt("Enter last name:");
alert(firstName + " " + lastName);

Note the usage of two variables, firstName and lastName, with the prompt method to collect user input. The complete name is then displayed using the alert() method.