What does the "+=" operator do when used with a string variable?

QuestionsQuestions8 SkillsProShell ArraysDec, 07 2025
056

The += operator is a shorthand for concatenation and assignment in JavaScript (and many other programming languages). When used with a string variable, it appends the right-hand operand to the left-hand operand and assigns the result back to the left-hand variable.

Here's an example:

let greeting = "Hello";
greeting += " World"; // This is equivalent to greeting = greeting + " World";

console.log(greeting); // Output: "Hello World"

In this example, the += operator takes the current value of greeting, appends " World" to it, and then updates greeting with the new value.

0 Comments

no data
Be the first to share your comment!