JS-22 字符串的方法_concat()

concat方法用于链接两个字符串,返回一个新字符串,不改变原字符串

var s1='zifuchuan'

var s2='tow'

s1.concat(s2)//"zifuchuantow"

s1//"zifuchuan"

 该方法可以接受多个参数

'two'.concat('zifuchuan','three')//"zifuchuanthreetwo"

如果参数不是字符串,concat方法会将其先转为字符串,然后再连接

var one=1;

var two=2;

var three='3';

''.concat(one,two,three)//"123"

 

 做字符串相加,是不需要使用concat的,可以使用+链接字符串

        var result=str1+str2+str3+num;
        console.log(result);

       
concat和+还是有区别的:
concat不管什么类型直接合并成字符串
+是遇到数字类型直接做运算,遇到字符串和字符串相连接

 

posted @ 2025-01-08 15:26  张筱菓  阅读(23)  评论(0)    收藏  举报