DEV Community

Cover image for 3 easy ways to interact with users in JS

Posted on


3 easy ways to interact with users in JS

JS provides you 3 easy ways to interact with users without asking anything much.


Alert pops-up dialog box on screen with a button "ok" on it. The button is by default on this dialog box.


 alert('HI there'); // with specified content
 alert(); // without any specified content

Enter fullscreen mode Exit fullscreen mode


Prompt is another user-interface function which normally contains two arguments.

The text is basically what you want to show the user and the default value argument is optional though it acts like a placeholder inside a text field. It is the most used interface as with it you can ask the user to input something and then use that input to build something.

Example:(with default parameter)

// prompt example
let age = prompt('How old are you?', 50);

alert(`You are ${age} years old!`); 
Enter fullscreen mode Exit fullscreen mode


The confirm function basically outputs a modal window with a question and two button ‘OK’ and ‘CANCEL’.



// confirm example 
let isHappy  = confirm('Are you Happy?');
alert(`You are ${isHappy}`); 

Enter fullscreen mode Exit fullscreen mode

It will print true or false based on your choice of clicking the ‘OK’ button or ‘CANCEL’ button respectively.

alert('THanks for rreaaddingg <3');

Top comments (2)

lukeshiru profile image
Luke Shiru

Worth mentioning that ideally you shouldn't use any of these, and instead use elements like dialog which wont block the entire page for a single interaction like alert, prompt and confirm do.

bellatrix profile image

Agreed @lukeshiru

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!