How to useHTML Form and JavaScript in Javascript
You can also create an HTML form to accept user input and use JavaScript to display the entered integer.
HTML
<!DOCTYPE html> < html > < body > Enter an integer: < input type = "text" id = "integerInput" > < button onclick = "printInteger()" >Submit</ button > < script > function printInteger() { let userInteger = parseInt( document.getElementById("integerInput").value); alert(`You entered: ${userInteger}`); } </ script > </ body > </ html > |
Output
In this approach, the user’s input is captured from an HTML input element, and the printInteger()
function is called when the user clicks the “Submit” button. The function retrieves the value from the input field, converts it to an integer, and displays it using an alert box.
JavaScript Program to Print an Integer Entered by user
Creating a JavaScript program to accept an integer from the user and then print it is a fundamental exercise that helps in understanding user input and output operations. In this article, we’ll explore different approaches to achieve this task in a JavaScript environment.