swap a string in Javascript
Code snippet on how to swap a string in Javascript
let str1 = "Hello";
let str2 = "World";
let tempStr = str1;
str1 = str2;
str2 = tempStr;
This code is swapping two strings, “Hello” and “World”. The first two lines declare two variables, str1 and str2, and store the strings inside of them. The third line declares a third variable, tempStr, and stores the value of str1 inside of it. This is necessary in order to save str1 so that we can use it later when swapping the strings. The fourth line sets str1 equal to the value of str2, which is now “World”. Finally, the fifth line sets str2 equal to the value of tempStr, which is the original value of “Hello”. This has effectively swapped the strings from their original values to the opposite values.