The example is below:
1) Here, we declared a variable called
GreetStr and at the same assigned a string value to it. The string contains a space in the tail end as we want to append some dynamic content to it.
2) The function
prompt asks the user to enter his name. The
PersonName variable holds the data entered by the user. This string is a dynamic content as the user is entering it in the input box.
3) Here, we perform String concatenation of two variables
PersonName using the + operator. After joining the string, the result is stored back in the
GreetStr variable. Note the order of the join. The left side of the + is
4) Now, we have result string in the
GreetStr. Here, we use the
document object, which represents the html document getting rendered in the browser. The
writeln method will write the string content directly into the document content. We passed the greeting string as an argument to this method. One can see two more string-join done on the fly. We enclose the greeting string between the
<h1> tags via string join and + operator.
4. Running the Example
Running the above example in the browser produces the following result:
5. Code Reference
//Sample 01: Initial String
//Sample 02: Get String From User
var PersonName = prompt("What is your Name?");
//Sample 03: Append String
GreetStr = GreetStr + PersonName;
//Sample 04: Write String to Html Document
document.writeln("<h1>" + GreetStr + "</h1>");
Do you like this Example? Please comment about it for others!!