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 use Global Variable using JavaScript in Laravel ?

What is Global Variable ? A global variable is a variable that is defined and accessible throughout the entire program or a significant portion of it, rather…

How to print Object Value using Array in Javascript ?

In this tutorial i’m going to print the object value using javascript ? First go to controller function and put below code. Next go to blade page…

How to Count the Vowels value in array objects ?

Ini this tutorial we’re going to learn how to get the value form array object and count only vowels value. 1st step create below file name Put…

How to print multidimensional array in Console ?

In this tutorial we’re going to learn how to print multidimensional array in console.log Using this I want to print all data whatever coming in log file…

How to remove fake path from url in laravel ?

In this tutorial we’re going to learn how to remove fake path from url in laravel using javascript. Now fakepath removing successfully form url 👍

List of My Blogs Written from May to October 2023

List of My Blogs Written in May 2023 1 How to Show Notification using Sweet Alert in Laravel 9 2 How to use Soft Delete and Restore…

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