0

I am very new to programming, this is probably an easy task for most people, haha.

I want a variable (text) to be made from two other variable. For example:

var abc = "app"
var def = "les"

var ghi = (abc,def)

document.write(ghi)

And the end result would be:

apples

Unfortunately, the above code is not working for me.

Sorry if this is super simple stuff, thank you if you do end up helping me.

  • 2
    I do not know javascript, but have you tried var ghi = abc + def ? – Erik W May 31 '16 at 08:49
  • Duplicate: http://stackoverflow.com/questions/5922647/how-do-i-combine-2-javascript-variables-into-a-string – Marc Dix May 31 '16 at 08:51
  • I think what you are asking is about referencing other two variables by third one i.e if any of the two values changes then the changed contents should be reflected in third variable, is that right – Rajesh Jangid May 31 '16 at 09:01

6 Answers6

0

Try this:

  var ghi = (abc+def);

And you should end lines with semicolons:

var abc = "app";
var def = "les";

var ghi = (abc+def);

document.write(ghi);
grateful
  • 1,128
  • 13
  • 25
  • Duplicate: http://stackoverflow.com/questions/5922647/how-do-i-combine-2-javascript-variables-into-a-string – Marc Dix May 31 '16 at 08:51
0
var ghi = abc+def;

You should also put semicolons on the end of your statements, as this will help browsers run your script quicker

Pedram
  • 6,256
  • 10
  • 65
  • 87
Andrew Pay
  • 134
  • 7
  • Can you explain the logic behind both sides? I usually use semicolons but was too lazy for this example, is there actually a notable difference? – Gravy Chicken May 31 '16 at 09:20
  • They say to use semicolons to avoid bugs, however with many of the new js versions, javascript will automatically add semicolons at the end of a line. I've worked at various companies where semicolons were not used and we had no problems. Also, the reason it takes longer to parse is because there's more characters the compiler has to parse. Same with double quotes, spaces (vs tabs), etc. – Alex Cory May 31 '16 at 09:40
  • Assuming that browsers follow the ECMAScript spec to the letter (which they don't) the procedure for automatic insertion of Semicolons often involves encountering an error (i.e. a character that doesn't make sense as part of the preceding statement), and then going back and re-doing things, arguably with the insertion of a semicolon into the glyph stream. This is objectively slower than reading the additional character. If browsers want to optimise further then that's great, but as programmers we should be assuming that the spec is followed. – Andrew Pay May 31 '16 at 09:50
  • All other arguments as to whether semicolons should or shouldn't be used are completely subjective, or situational. For example, whether you're more or less likely to trip over a syntax problem by including or omitting them is entirely down to the code that you're writing and how well you understand the way statements are terminated in JS. From my standpoint, I use Semicolons because I believe we should be moving to a more sensible spec where whitespace is completely non-functional. I will also continue to encourage newer coders to do the same – Andrew Pay May 31 '16 at 09:57
  • And I will continue to give a reason that is both theoretically correct according to the specification, and easier for them to understand. Even if the actual performance impact in practice is irrelevant now, you must code for future browsers, and you must assume that they follow the specification to the letter – Andrew Pay May 31 '16 at 09:58
  • http://programmers.stackexchange.com/questions/142086/why-the-recent-shift-to-removing-omitting-semicolons-from-javascript – Alex Cory May 31 '16 at 18:53
0

The + operator concatenates (or in other words combines) strings.

Try

var abc = 'app'
var def = 'les'
var ghi = abc + def
console.log(ghi)
Alex Cory
  • 10,635
  • 10
  • 52
  • 62
  • Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone May 31 '16 at 09:38
0

Two ways:

Most simple:

var ghi = abc + def;

Or use string.concat MDN

var abc = "app"
var def = "les"

var ghi = abc.concat(def);
Pimmol
  • 1,871
  • 1
  • 8
  • 14
0

What is you are asking is about string concatenation. For string concatenation, you may use concat function.

var ghi = abc;
ghi.concat(def)

But it is highly recommended to use + or += operators for string concatenation.

var ghi = abc + def;

For more information see MDN

niyasc
  • 4,440
  • 1
  • 23
  • 50
0

Use + plus sign instead of , comma

var abc="app"
var def = "les"
var ghi = (abc+def);
document.write(ghi) ;
Sunil Rajput
  • 960
  • 9
  • 19