String concatenation?

  • 0
    for example, there is this. expression
    let I = 'b' + 'a'
    is it possible to somehow refer to a and b? (except for working with a ready-made line i)
    for example by array index or something else
    in a real example, these are strings of different content and length
    JavaScript Anonymous, Aug 3, 2020

  • 5 Answers
  • 0
    I don't know if I understood the question correctly:

    var a= 'a', b= 'b';
    b= b.toUpperCase();
    var i= a+b;
    Anonymous

  • 0
    let I = 'b' + 'a'

    separately, you will not be able to access a and b , because the binary operator '+' concatenates two strings, and the variable is assigned the result of the concatenation itself.

    but, you can do the following and use an array:

    let arr = ["a", "b"];

    let output = "";

    const changeElements = (first, second) => {
    if (first === undefined) {
    arr[1] = second;
    output = arr[0] + arr[1]
    } else if (second === undefined) {
    arr[0] = first;
    output = arr[0] + arr[1]
    } else {
    arr[0] = first;
    arr[1] = second;
    output = arr[0] + arr[1]
    }
    }

    changeElements("c")

    console.log(output) // "cb"

    changeElements("o", "f")

    console.log(output) // "of"
    Anonymous

  • 0
    no



    PS. but you can save these 'a' and 'b' in separate variables, concatenate them, but the original lines will remain in these variables
    Anonymous

  • 0
    If I understood the question correctly, then something like this

    const a = 'Mary';

    const b = 'Hello';

    console.log (`$ {a}, how are you? $ {b}`);

    console.log (a + b);

    console.log (a);

    console.log (b);
    Anonymous

  • 0
    I didn't understand the question. Where do these lines come from?

    After merging, it is no longer possible to split back, because the information about the merging boundary has been lost in the I variable. But it is possible if this information has been preserved somewhere - for example, in the form of the length of at least one of the original strings.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!