Mutation

Today I learned that line 3 of the following does not work if you want x[0] to be “ab”:

var x = [];
x.push("a");
x[0].concat("b");

what I did was:

var x = [];
x.push("a");
x[0] = x[0].concat("b");

(See it here)
I don’t know if it is good style.

That made me think about Python, but Python does not have a string concatenation operation like that leaving no room for misunderstandings. The corresponding Python code would be:

x = []
x.append("a")
x[0] += "b"

Hey! That works in javascript too:

var x = [];
x.push("a");
x[0] += "b";
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s