Open In App

JavaScript Equivalent of Python Input

Last Updated : 09 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, the input() function is commonly used to take user input from the console. However, JavaScript does not have a built-in input() function like Python. Instead, JavaScript provides different ways to achieve user input, mainly through browser-based interactions. In this article, we'll compare the Python input() function with JavaScript alternatives and explore the similarities in their approach.

Python Input

In Python, the input() function is a simple way to get input from the user via the console. It prompts the user with a message and waits for them to type something. Once the user presses Enter, the function returns the input as a string.

Python
# Taking user input in Python
n = input()
print(f"Hello, {n}")

Output:

Shivang
Hello, Shivang

Explanation:

  • The input() function prompts the user to enter their name. The message "Enter your name: " is displayed in the console.
  • Once the user types something and presses Enter, the input is stored in the variable name, which is then used in the print statement to greet the user.

JavaScript Input

In JavaScript, we can use the prompt() function to capture input from the user in a browser environment. The prompt() function is similar to Python’s input() and provides a text field for the user to enter a value.

JavaScript
// Taking user input in JavaScript
let n = prompt();
console.log(`Hello, ${n}!`);

Output:

Shivang
Hello, Shivang!

Explanation:

  • The prompt() function displays a dialog box with the message "Enter your name:", prompting the user to enter some text.
  • Once the user inputs their name and presses Enter, the value is stored in the name variable, and the message "Hello, {n}!" is logged to the console.

Next Article
Article Tags :
Practice Tags :

Similar Reads