[Solved] What does ‘+variable+’ mean? [closed]


What is this operator? ‘+variable+’

That isn’t an operator.

' ends a string literal

+ is a concatenation operator.

variable is a string variable

+ is another concatenation operator.

' starts a new string literal.

And why should we use the weird notation "'+variable+'"?

The two string literals have " characters in their data.

The object is to construct this:

'<element attribute="value">'

When the value is a variable

var myValue = "value";
'<element attribute="' + value + '">'

Generating code from code by string concatenation always gives ugly code, which is relatively hard to maintain. I’d approach the problem with a more verbose approach:

var content = $("<div>");
var button = $("<button>");
button.val(window_value);
button.on('click', function() { reload_to_canvas(this.value); });
var img = $('<img>');
img.attr('id', 'w' + window_value);
img.attr('src', 'white_data_URL');
// width and height can be handled in CSS
button.append(img)
content.append(button)
$('#story_pages').append(content)

15

solved What does ‘+variable+’ mean? [closed]