For integers Simple version for integer arguments const average = (...nums) => nums.reduce((accum, curVal) => accum + curVal) / nums.length Same, but for arguments as primitives or in array, they can be numbers or strings. For reals and strings const average = (...nums) => { let arr = [] // may pass array or args separately if (!Array.isArray(nums)) arr = [...nums] // if primitive args supplied if (Array.isArray(nums)) arr = nums.flat(Infinity) // if array supplied const floatsArr = arr.map(el => parseFloat(el)) // in case numbers are passed as strings const average = floatsArr.reduce((accum, curVal) => accum + curVal) / arr.length const roundedNum = average.toFixed(2) return roundedNum } Full code function Component() { const [inputValState, setInputValState] = React.useState('1 2 3 4 5') const [averageState, setAverageState] = React.useState(average(inputValState.split(' '))) const onChangeHandler = (e) => { const inpVal = e.target.value setInputValState(inpVal) const numsArr = inpVal .match(/[-]{0,1}[\d]*[.]{0,1}[\d]+/g) // match numbers .filter(el => el) // remove empty elements setAverageState(average(numsArr)) } return ( <> <input value={inputValState} onChange={onChangeHandler}/> <div>Average: {averageState}</div> </> ) }