\r\n```\r\n\r\n今回は、string型になるかと思います。\r\nstring型になるinputのtypeは、\r\n- text\r\n- tel\r\n- url\r\n- password\r\n\r\nなどです。\r\nstring型が多いです。","dateModified":"2019-05-29T06:58:53.748Z","datePublished":"2019-05-29T06:56:31.938Z","upvoteCount":2,"url":"https://teratail.com/questions/192000#reply-284912"},"suggestedAnswer":[{"@type":"Answer","text":"数値しかあつかえないnumberであってもvalueの型はstringです\r\n```javascript\r\n\r\n\r\n```","dateModified":"2019-05-29T06:47:39.465Z","datePublished":"2019-05-29T06:47:39.465Z","upvoteCount":4,"url":"https://teratail.com/questions/192000#reply-284909","comment":[{"@type":"Comment","text":"yambejp様\r\n返信ありがとうございます。\r\n\r\njavascriptは変数の型が分かりにくいですね。\r\n今後も勉強に励みます。","datePublished":"2019-05-31T10:32:12.636Z","dateModified":"2019-05-31T10:32:12.636Z"}]},{"@type":"Answer","text":"数値型か文字列型かの判定であれば`typeof`を使うと判定できます。\r\n\r\n意図的に変換してから使いたいのであればNumber()やString()などを使えば良いでしょう。","dateModified":"2019-05-29T06:44:59.539Z","datePublished":"2019-05-29T06:44:59.539Z","upvoteCount":5,"url":"https://teratail.com/questions/192000#reply-284906","comment":[{"@type":"Comment","text":"dice142様\r\n返信ありがとうございます。\r\n\r\n具体的な解決方法を教えていただき、ありがとうございます。","datePublished":"2019-05-31T10:35:10.282Z","dateModified":"2019-05-31T10:35:10.282Z"}]},{"@type":"Answer","text":"実際にやってみれば明白ですが\r\n```JavaScript\r\nconsole.log(typeof paymentSelect.value); // string\r\nlet heightInput = document.getElementById(\"heightInput\");\r\nconsole.log(typeof heightInput.value); // string\r\n```\r\n[https://developer.mozilla.org/ja/docs/Web/API/HTMLInputElement](https://developer.mozilla.org/ja/docs/Web/API/HTMLInputElement)\r\n[https://html.spec.whatwg.org/multipage/input.html#htmlinputelement](https://html.spec.whatwg.org/multipage/input.html#htmlinputelement)\r\n\r\n[https://developer.mozilla.org/ja/docs/Web/API/HTMLSelectElement](https://developer.mozilla.org/ja/docs/Web/API/HTMLSelectElement)\r\n[https://html.spec.whatwg.org/multipage/form-elements.html#htmlselectelement](https://html.spec.whatwg.org/multipage/form-elements.html#htmlselectelement)","dateModified":"2019-05-29T06:41:44.633Z","datePublished":"2019-05-29T06:41:44.633Z","upvoteCount":6,"url":"https://teratail.com/questions/192000#reply-284905","comment":[{"@type":"Comment","text":"x_x様、今回も解説ありがとうございます。\r\nコンソールと「typeof」を使えば良かったのですね。\r\n具体的な解決方法を示して下さり、ありがとうございます。","datePublished":"2019-05-31T10:39:27.735Z","dateModified":"2019-05-31T10:39:27.735Z"}]}],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/JavaScript","name":"JavaScriptに関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/192000","name":"javascript input.valueの型の種類に対する質問"}}]}}}
input要素のvalueは数値型か文字列型なのかがよく分かりません。
(html)
<select id="paymentSelect" name="paymentSelect">
<option value="0">選択してください</option>
<option value="1">銀行振込</option>
<option value="2">クレジットカード</option>
</select>
paymentSelect.value:文字列型
(html)
<input type="text" id="heightInput" name="height" placeholder="身長">
(javascript)
let heightInput = document.getElementById("heightInput");
heightInput.value:数値型?
数値型か文字列型かの判定であればtypeofを使うと判定できます。
意図的に変換してから使いたいのであればNumber()やString()などを使えば良いでしょう。
数値しかあつかえないnumberであってもvalueの型はstringです
javascript
1<script>
2window.addEventListener('DOMContentLoaded', function(e){
3 document.querySelector('#num').addEventListener('change',function(e){
4 console.log(typeof e.target.value);
5 });
6});
7</script>
8<input type="number" id="num">
ベストアンサー
JavaScriptにおいて、型を確認するには、typeofキーワードを使えば行えるので、それで確認されるといいでしょう。
html
1<input type="text" id="heightInput" name="height" placeholder="身長">
2
3<script>
4let heightInput = document.getElementById("heightInput");
5console.log(typeof heightInput.value);
6</script>
今回は、string型になるかと思います。
string型になるinputのtypeは、
などです。
string型が多いです。
15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.29%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる
質問する