0

I'm currently doing this:

var 1 = "http://www";
var 2 = ".google.";
var 3 = "com/";

then concatenating them together like this

var link = 1+2+3;

Is there an easier and more proficient way of doing this?

7
  • It sounds like you want an array. Commented Nov 16, 2014 at 3:43
  • 1
    I don't think you can get much more basic. And what do you mean by proficient? Commented Nov 16, 2014 at 3:43
  • 1
    IS declaring 1,2 as var valid ? Commented Nov 16, 2014 at 3:48
  • @yobro: link would have value as 6, and not "google.com" Commented Nov 16, 2014 at 4:10
  • @yobro: Interesting! you are using the shortcut operator for concatenation, and asking for an easier way :) Commented Nov 16, 2014 at 4:12

1 Answer 1

1

I don't know what would be much easier than simple concatenation like you show, but you could put them in an Array and join it together.

(I fixed your variable names to make them valid.)

var first = "http://www";
var second = ".google.";
var third = "com/";

var link = [first, second, third].join("");

Or you could use the .concat() method.

var link = first.concat(second, third);

But both of these are longer than your original so I don't know if that's what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

cheers for the answer mate, as you said, your method is slightly longer. So i think i'll just stick with the method im using. Just thought there was an easier way to do it. cause adding each one together like that looks so amateur :P
There are some browsers where join is faster, but in most (especially modern) browsers + is hugely faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.