var greeting = "Hello World." + " My name is Jennifer."Any mix of string literals and string variables may be concatenated, and you may concatenate as many literals and variables on the same line as you wish.
<a href="javascript:alert(greeting)">alert(greeting)</a>
var myName = "My name is Jennifer."
var myString = "Hello World. " + myName // myString is a concatenation of a literal and a variable
<a href="javascript:alert(myString)">alert(myString)</a>
var longGreeting = "Hello World." + " My name is Jennifer." + " What is your name?" // 3 literals concatenated
<a href="javascript:alert(longGreeting)">alert(longGreeting)</a>
var longString = "Hello World."The same strings can be concatenated in a much more efficient way by using the add by value operator to eliminate the second reference to the string variable:
alert(longString)
longString = longString + " My name is Jennifer."
alert(longString)
longString = longString + " What is your name?"
alert(longString)
var efficientString = "Hello World."
efficientString += " My name is Jennifer."
efficientString += " What is your name?"
<a href="javascript: alert(efficientString)">alert(efficientString)</a>
var easiest = "Hello World. " +Remember to place the operator outside the quotes, after each string segment except for the last. Also, do not follow the operator with a semicolon which indicates the end of a statement because your statement is not ending - it is continuing on the next line. Although the carriage return acts as a substitute for the semicolon in every other case, when it follows the string operator, it is not interpreted as the end of a statement.
"My name is Jennifer. " +
"What is your name?"
<a href="javascript alert(easiest)">alert(easiest)</a>
function alertName(){
var studentName = document.nameForm.nameField.value // store text field value in variable
if(studentName != "") {// if field is not empty
alert("Hello, " + studentName + "! So nice to meet you.") // concatenate text field value with this string
}
else{
alert("Please Enter Your Name!")
}
}