简体   繁体   中英

How do I make a variable refer to other variables? [Javascript]

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.

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);
var ghi = abc+def;

您还应该在语句的末尾添加分号,因为这将有助于浏览器更快地运行脚本

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

Try

var abc = 'app'
var def = 'les'
var ghi = abc + def
console.log(ghi)

Two ways:

Most simple:

var ghi = abc + def;

Or use string.concat MDN

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

var ghi = abc.concat(def);

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

Use + plus sign instead of , comma

var abc="app"
var def = "les"
var ghi = (abc+def);
document.write(ghi) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM