You are currently viewing Concatenate String and Variable with Examples in JavaScript

Concatenate String and Variable with Examples in JavaScript

 Javascript Concatenate String and Variable

Concatenate operator

A valid combination of operators and operands is known as an expression. You can perform  Javascript Concatenate String and Variable.  An expression that contains two elements, they are operators and operands. javascript (+) operator is an overloaded operator, which means it acts differently in different situations.

Javascript (+) operator accepts 2 operands:

  • If one of the given operands is of string type, then the (+) operator acts as a concatenation operator
  • If both operands are of number type, then the (+) operator acts as an addition operator.

Ex:

Operand1 Operator (+) Operand2 Result
String + (concatenation) String String
String + (concatenation) Number String
Number + (concatenation) String String
Number + (addition) Number Number

Ex:

document.write("Wonder"+"Develop"); // WonderDevelop
document.write("number"+10);        // number10   
document.write(10+"column");        //10column
document.write(15+15);              //30

Concatenate string and variable

let fav="Peacock";
document.write("My favourite bird is "+fav);
// My favourite bird is Peacock;

In the above code, the string is stored in the “fav” variable And the string fav is Concatenated by giving some text in the print statement.

Leave a Reply