CONCATENATION

String addition is called concatenation . It uses the + operator. This operator is both the addition operator and the string concatenation operator.

When you add two strings, JavaScript groups the strings as one string, for example:

var fname = Joe;
var lname = Smith;
var full_name= fname + lname;

In this case, full_name now has the value JoeSmith. You can also include additional punctuation; for example, to include a space in between the first and last name:

var full_name = fname + " " + lname;

Now, full_name holds the value Joe Smith. The " " string is an empty string that holds a space.

Variable Conversion

As mentioned earlier in this lesson, JavaScript is a weakly typed language and will automatically do datatype conversions in context.

JavaScript automatically converts a string to a number if the string is used in a numerical calculation, for example:

var a = 2;
// a is a number
var b = "5";
// b is a string
var c = b - a; 
// b is automatically converted from a string 
to a number, which assigns the numerical value 3 to c

JavaScript also automatically converts a number to a string if the + operator is used; for example:

var a = 2;
// a is a number
var b = "5";
// b is a string
var c = b + a;
// a is automatically converted from a number 
to a string, and string concatenation is performed,
which assigns the string value of "52" to c