首页 > 文章列表 > js中字符串连接方式

js中字符串连接方式

js字符串连接
298 2022-08-06

python中,有字符串的使用,在JavaScript中也有。可以说字符串是编程中必不可少的数据类型。本文小编向大家介绍JavaScript中字字符串的操作手法连接字符串:1、使用连接符“+”连接字符串(最简便的方法);2、以数组作为中介,使用jion函数连接字符串(针对数组中的字符连接);3、使用字符串 concat() 方法连接字符串。

方式一:使用连接符“+”连接字符串(最简便的方法)

str="a";
str+="b";

方式二:以数组作为中介,使用jion函数连接字符串(针对数组中的字符连接

        var arr = [1, 2, 3, 4, 5];
        !function () {
            console.log(arr.join(''));//字符串‘12345’;
            console.log(arr);//[1,2,3,4,5]原数组不变;
        }();

方式三:使用字符串 concat() 方法连接字符串

var str1 = "hello ";
var str2 = "world!";
console.log(str1.concat(str2)); // hello world!