// 先转化为字符串 var str = 12345678 + '' // 封装一个反转字符串的方法 function res(str){ // split(转换为数组) reverse(反转数组) join(转化为字符串) return str = str.split('').reverse().join('') } // 调用反转数组的方法 str=res(str) //每三位字符加一个 ','的方法 function toRMB(str){ var rmb = '' for(var i = 1; i <= str.length; i++){ rmb += str[i - 1] // 添加字符串的每一项 if( i % 3 == 0 && i != str.length ){ 判断每三个字符串添加一个',' 索引是从零开始的所以 i != str.length rmb += ',' } } return rmb } // 调用函数 str = res(toRMB(str)) console.log(str) // 12,345,678