String concatenation is a fundamental concept in JavaScript that involves combining multiple strings into a single string. It is a process of joining two or more strings together, end-to-end, to create a new string. In JavaScript, strings are a sequence of characters enclosed within single quotes ('') or double quotes ("").
To perform string concatenation in JavaScript, the plus operator (+) is commonly used. When the plus operator is applied to two or more strings, it concatenates them by joining the characters sequentially. The resulting string is the combination of all the individual strings involved in the operation.
Here is an example to illustrate string concatenation in JavaScript:
javascript let firstName = "John"; let lastName = "Doe"; let fullName = firstName + " " + lastName; console.log(fullName); // Output: John Doe
In the above example, the variables `firstName` and `lastName` store the strings "John" and "Doe" respectively. The `fullName` variable is created by concatenating the `firstName`, a space (" "), and the `lastName` using the plus operator (+). The resulting string is "John Doe", which is then displayed in the console.
String concatenation is not limited to just variables. It can also involve combining string literals and variables, or even multiple string literals. Here are a few more examples:
javascript
let message = "Hello, " + "world!";
console.log(message); // Output: Hello, world!
let age = 25;
console.log("I am " + age + " years old."); // Output: I am 25 years old.
In the first example, two string literals "Hello, " and "world!" are concatenated using the plus operator (+), resulting in the string "Hello, world!". In the second example, the variable `age` is concatenated with the string literals "I am " and " years old." to create the string "I am 25 years old.".
It is important to note that when concatenating a string with a non-string value, JavaScript automatically converts the non-string value to a string before performing the concatenation. This is known as implicit type conversion or type coercion. For example:
javascript let x = 10; let y = "20"; let result = x + y; console.log(result); // Output: 1020
In the above example, the variable `x` stores the number 10, and the variable `y` stores the string "20". When the plus operator (+) is used to concatenate `x` and `y`, JavaScript converts the number `x` to a string before concatenating, resulting in the string "1020".
In addition to the plus operator (+), there is an alternative method for string concatenation in JavaScript using template literals. Template literals are enclosed within backticks (` `) and allow for easy string interpolation and multiline strings. Here is an example:
javascript
let name = "Alice";
let age = 30;
let message = `My name is ${name} and I am ${age} years old.`;
console.log(message); // Output: My name is Alice and I am 30 years old.
In the above example, the variables `name` and `age` are interpolated within the template literal using the `${}` syntax. The resulting string is "My name is Alice and I am 30 years old.".
String concatenation in JavaScript is the process of combining multiple strings into a single string using the plus operator (+) or template literals. It is a fundamental technique for manipulating and generating dynamic text in web development.
Other recent questions and answers regarding Examination review:
- Can the value of a constant variable be changed after it is assigned a value?
- How can you declare a constant variable in JavaScript?
- How are numbers and strings different in JavaScript?
- What are the two basic data types in JavaScript?

