We can add two or more strings by using concat string function in JavaScript. These function will add strings but we have other different ways to add strings.
We will discuss different ways, before that we will start with syntax of concat function.
variable.concat("string1","string2","string3","string4");
We will use it in a different ways here. First will try to add three strings like this.
var my_var1=" Hello Welcome"
var my_var2=" to plus2net.com "
var my_var3= " JavaScript Section "
var my_var=my_var1.concat(my_var2, my_var3)
document.write(my_var);
The output of above code will be
Hello Welcome to plus2net.com JavaScript Section
We can add three strings directly like this.
var new_var="";
new_var= new_var.concat("First one", " Second One", " Third one ");
document.write(new_var)
By using + operator
We can add strings by using + operation also like this
var my_var1=" Hello Welcome"
var my_var2=" to plus2net.com "
var my_var3= " JavaScript Section "
var my_var=my_var1 + my_var2 + my_var3
document.write(my_var);
Same result we can achieve by using single variable this way
var my_var=""
my_var +=" Hello Welcome"
my_var +=" to plus2net.com "
my_var += " JavaScript Section "
document.write(my_var);
← Subscribe to our YouTube Channel here