这种方式比较简单,推荐使用
字符串转数组,反转数组,数组转字符串。
- split(""):根据空字符串拆分数组
- reverse():数组反转元素位置
- join(""):数组转回字符串,且不带分隔符
"use strict"
console.log('hello,world!');
var str = 'wuyingzhen'
console.log("str:",str);
var tempStr = str.split('').reverse().join('');
console.log("change str:",str); //源字符串并不会被改变,返回值为翻转后的字符串
console.log("tempStr:",tempStr);
console.log('happy ending!')
执行结果: