正解:A,D
We want to guarantee that the result is of type string regardless of what var1 and var2 are (numbers, booleans, etc.).
Option A:
String(var1).concat(var2)
* String(var1) converts var1 explicitly to a string.
* .concat(var2) calls String.prototype.concat, which:
* Converts var2 to string if needed.
* Returns a new string that is the concatenation of the two.
* Result is always a string. So A is valid.
Option B:
String.concat(var1 + var2)
* String.concat (as a static method) does not exist. concat is an instance method on string objects, not on the String constructor.
* var1 + var2 might be a number (if both numeric) and used incorrectly here. So B is invalid.
Option C:
var1 + var2
* + can mean:
* String concatenation (if at least one operand is a string).
* Numeric addition (if both operands are numbers).
* If var1 and var2 are both numbers, var1 + var2 will be a number, not a string.
* This does not ensure the result is always a string. So C is not guaranteed to satisfy the requirement.
Option D:
var1.toString() + var2.toString()
* var1.toString() converts var1 to a string (for most primitive/object types).
* var2.toString() does the same.
* Now both operands of + are strings, so the result is string concatenation.
* Result is guaranteed to be a string. So D is valid.
Therefore, the two valid ways are A and D.
Study Guide / Concept References (no links):
* String() conversion vs .toString()
* String.prototype.concat
* + operator overload: numeric addition vs string concatenation
* Type coercion rules for primitives