In this article, we will get some input from user by using <input> element and the task is to split the given number into the individual digits with the help of JavaScript. There two approaches that are discussed below:
Approach 1: First take the element from input element in string format (No need to convert it to Number) and declare an empty array(var res). Visit every character of the string in a loop on the length of the string and push the character in the array(res) by using push() method.
- Example: This example implements the above approach.
<!DOCTYPE HTML><html><head><title>Split a number into individualdigits using JavaScript</title><style>body {text-align: center;}h1 {color: green;}#geeks {color: green;font-size: 29px;font-weight: bold;}</style></head><body><h1>GeeksforGeeks</h1><p>Type the number and click on the buttonto perform the operation.</p>Type here:<inputid="input"/><br><br><buttononclick="GFG_Fun();">click here</button><pid="geeks"></p><script>var down = document.getElementById('geeks');function GFG_Fun() {var str = document.getElementById('input').value;res = [];for (var i = 0, len = str.length; i <len; i += 1) {res.push(+str.charAt(i));}down.innerHTML="["+ res + "]";}</script></body></html>chevron_rightfilter_none - Output:

Approach 2: First take the element from input element in string format (No need to convert it to Number) and declare an empty array(var res). Split the string by using split() method on (”) and store the spitted result in the array(str).
- Example: This example implements the above approach.
<!DOCTYPE HTML><html><head><title>Split a number into individual digits</title><style>body {text-align: center;}h1 {color: green;}#geeks {color: green;font-size: 29px;font-weight: bold;}</style></head><body><h1>GeeksforGeeks</h1><p>Type the number and click on thebutton to perform the operation.</p>Type here: <inputid="input"/><br><br><buttononclick="GFG_Fun();">click here</button><pid="geeks"></p><script>var down = document.getElementById('geeks');function GFG_Fun() {var n = document.getElementById('input').value;var str = n.split('');down.innerHTML = "[" + str + "]";}</script></body></html>chevron_rightfilter_none - Output:

Recommended Posts:
- Split an array into chunks in JavaScript
- Split a comma delimited string into an array in PHP
- JavaScript String split() Method
- JavaScript | Split a string with multiple separators
- Why split the <script> tag when writing it within document.write() method in JavaScript ?
- JavaScript | Symbol.split Property
- How to check a webpage is loaded inside an iframe or into the browser window using JavaScript?
- Convert string into date using JavaScript
- How to serialize an object into a list of URL query parameters using JavaScript ?
- How to convert a plain object into ES6 Map using JavaScript ?
- How to unpack array elements into separate variables using JavaScript ?
- How to convert JSON results into a date using JavaScript ?
- How to Combine multiple elements and append the result into a div using JavaScript ?
- Convert boolean result into number/integer in JavaScript
- What is the Efficient way to insert a number into a sorted array of numbers in JavaScript?
- How to convert long number into abbreviated string in JavaScript ?
- How to split a column of list in different rows using bootstrap?
- How to split text horizontally on mouse move over using CSS ?
- How to insert an item into array at specific index in JavaScript?
- How to convert string into float in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


