Split a number
Last Updated: 29-01-2020
Split a number into individual digits using JavaScript
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.
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
Reference : https://www.geeksforgeeks.org/split-a-number-into-individual-digits-using-javascript/?ref=rp
Last updated