Skip to content

Latest commit

 

History

History
98 lines (90 loc) · 1.46 KB

3. JavaScript Take Input.md

File metadata and controls

98 lines (90 loc) · 1.46 KB

3. JavaScript Take Input

Take Input in JavaScript

const name = prompt("Enter your name:");
console.log(`Name: ${name}`);

Output

Enter your name: Punit
Name: Punit

JavaScript number input

const age = prompt("Enter your age:");
console.log(age);

Output

Enter your age: 28
28

Find the type

const age = prompt("Enter your age");
console.log(age);
console.log(typeof(age));

Output

Enter your age : 28
Hello, Your age is 28
"string"

const intValue = 5;
console.log(typeof(intValue));

const floatValue = 5.5;
console.log(typeof(floatValue));

Output

"number"
"number"

const age = parseInt(prompt("Enter your age:"));
console.log(age);
console.log(typeof(age));

Output

Enter your age: 28
"number"

Float Input

const height = parseFloat(prompt('Enter your height '));
console.log(height);
console.log(typeof(height));

Output

Enter a number: 5.65
5.65
"number"

Error When Using paresInt() and parseFloat()

let name = parseInt(prompt('Enter your name: '));
console.log(name);

Output

Enter your name: Punit
NaN

Programiz Quiz

What is the type of the ‘number’ variable?

const number = prompt(‘Enter a random number’);
console.log(typeof(number));
  1. number
  2. string
  3. NaN
  4. undefined

Answer: 2