0

So I want to add content like a string or another variable to a variable that already exists. I know this isn't right, but this is what I have.

var variable = "Variable";

function dispVar1() {
    let p = document.createElement("p");

    p.innerHTML = variable;
}

function dispVar2() {
    let p = document.createElement("p");
    let alteredVar = variable + " that has been altered";
    /* Also, could I do "" + variable to make string go before the variable? */

    p.innerHTML = alteredVar;
}
<input type="button" value="Original" onclick="dispVar1();">
<input type="button" value="Altered" onclick="dispVar2();">
6
  • Just use += to concatenate more onto the value. Commented Feb 18, 2020 at 20:44
  • Of course you can use "something" + variable. Why would the order make a difference? Commented Feb 18, 2020 at 20:49
  • See Stack Snippet to make an executable snippet. Commented Feb 18, 2020 at 20:51
  • Basically, click on the [<>] tool in the toolbar to open the snippet editor. Commented Feb 18, 2020 at 20:51
  • plot twist, this question is actually about innerHTML and you should use p.textContent if you just add some text to it. Its more performant. Commented Feb 18, 2020 at 20:53

2 Answers 2

1

You can simply use the + assignment operator to concatenate strings. You can see in the example I am creating a variable newValue that concats your original variable plus some additional text:

var variable = "Variable";
var targetEl = document.querySelector('#target');

function dispVar1() {
  target.innerHTML = '';
  let p = document.createElement("p");

  p.innerHTML = variable;
  target.appendChild(p);
}

function dispVar2() {
  target.innerHTML = '';
  let p = document.createElement("p");

  const newValue = variable + ' (here\'s some added text)';
  p.innerHTML = newValue;
  target.appendChild(p);
}
<input type="button" value="Original" onclick="dispVar1();">
<input type="button" value="Altered" onclick="dispVar2();">
<div id="target"></div>

Alternatively, you can take advantage of String.prototype.concat to achieve the same thing - but MDN doesn't recommend it:

It is strongly recommended that the assignment operators (+, +=) are used instead of the concat() method.

According to this performance test, the assignment operators are several times faster.

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

Comments

0

Your initial code works:

let alteredVar = variable + " that has been altered";

An alternative using template literals with placeholders is more fashionable than concatenation because it removes the necessity of escaping certain characters, is more flexible, and arguably more readable:

let alteredVar = `${variable} that has been altered`;

Comments

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.