JavascriptSkills.com Uncategorized How to get Input and Output- the sample way

How to get Input and Output- the sample way

HTML
<input id="num">Please enter number

<button id="sub">
submit
</button>

Javascript


let num = document.querySelector("#num");
//user input

let sub = document.querySelector("#sub");
//on click


sub.addEventListener("click", ()=>
{

//output user input

console.log(num.value);



})

https://jsfiddle.net/gkarxnoq/

How to convert input strings into numbers only.

let num = document.querySelector("#num");
//user input

let sub = document.querySelector("#sub");
//on click


sub.addEventListener("click", ()=>
{

let n = +num.value;
//output user input
//if you enter a non number will produce different errors;
//if you enter space will produce 0
//if you enter a letter will produce a NAN

console.log(n);



})

https://jsfiddle.net/gkarxnoq/1/

If the user enters a non number, then will be using the !num and return statement to return invalid.

Note: Nothing happens when a user enters Strings or any non digit number.

let num = document.querySelector("#num");
//user input

let sub = document.querySelector("#sub");
//on click


sub.addEventListener("click", ()=>
{

let n = +num.value;

//output user input
if(!n)
{
return;
}


console.log(n);



})
https://jsfiddle.net/gkarxnoq/2/

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post