How to use Prompt function in JavaScript ?

In this tutorial we’re going to learn how to use Prompt function using JavaScript.

  • let age = prompt("What is your age?", 18); – This line of code uses the prompt() function to prompt the user for their age. The second argument, 18, is the default age if the user does not enter anything.
  • let welcome = (age < 18) ? () => alert('Hello!') : () => alert("Greetings!"); – This line of code creates a variable called welcome and assigns it an anonymous function. The anonymous function takes one argument, age, and returns a function that displays a message. The age < 18 expression inside the ? ternary operator checks if the user’s age is less than 18. If it is, the first function is returned. Otherwise, the second function is returned.
  • welcome(); – This line of code calls the welcome() function. This function will display the message “Hello!” if the user’s age is less than 18, or the message “Greetings!” if the user’s age is 18 or older.

Just use below code and put put inside the script tag

let age = prompt("What is your age?", 18);
let welcome = (age < 18) ?
    () => alert('Hello!') :
    () => alert("Greetings!");

welcome();

Output:-

I hope its helpful for you šŸ‘

Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

How to print Null value Using JavaScript ?

In JavaScript, if you want to print the string “null” when a variable is null, you can do so add in !== operator. Benefits of Using if-else…

JavaScript Comparison and Logical Operators

In this tutorial I’m going to learn how to use Comparison and logical operators in laravel. Comparison operators compare two values and give back a boolean value:…

How to use JavaScript Constructor Function ?

A JavaScript constructor function is a function that is used to create new objects. It is called a constructor because it constructs new objects. To create a…

How to use Let and Const in JavaScript ?

In this tutorial we’re going to learn how to use Const and let function in JavaScript. JavaScriptĀ letĀ is used to declare variables. The variables declared usingĀ letĀ areĀ block-scoped. This means…

Top 10 IT Certification in The World ?

In this tutorial I’m going to share top 10 IT Certification in the world who help to grow your carrier in fast way also we ‘having covering…

Phantom installation failed TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string.

Error:- Solutions:- Run this Output :-

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x