0

Suppose you have a text field created in HTML. And you retrieve the user input through java script. What is the type when you retrieve the data? Is it automatically a string?

<input type="text" id="input">

document.form.name.value; // what is the type of this?
jssmkp
  • 261
  • 2
  • 4
  • 12

3 Answers3

1

If you input type is going to be <input type="text" ... then it is a string. You can check or refer here: JavaScript: how to get value of text input field? for more info.

Community
  • 1
  • 1
buggytuf
  • 69
  • 1
  • 10
0

It is always a string unless you convert it as haim770 and Andrew Ashton said. Think about it this way, if you were to assign a value to the input inside the html file it would like this value='whatever value' and as you can see the data is inside quotation marks which makes it a string. This is essentially what .value is retrieving.

brinegjr
  • 86
  • 6
0

Html Dom Input text value returns returns a string that represent the value of a text field. Javascript is a loosely typed language which means you do not declare the data types of variables explicitly. Javascript performs the conversion automatically. so this means you can use the data received almost any way you want. if the text field had a value of say value='10' this is a string "one and zero" but northing stops you from using that value - where an integer type is required from your code. Javascript converts data types based on context.

katwekibs
  • 1,342
  • 14
  • 17