简体   繁体   中英

Concatenating strings in Javascript?

With the code below, looking at the 2nd paragraph in particular...if var str = "sting equality test..." + strA, why do the 2nd, 3rd lines etc not output that same output plus their own line?

EDIT sorry for not explaining properly - I am actually just wondering why this code (once I have cleaned it up) DOESN'T produce the first line (of the 2nd paragraph) repeated, plus whatever I state in the 2nd and 3rd lines etc. I don't need it to, it's just an exercise, I just don't understand. It seems as though it should

function init()
{
    var strA = "Javascript" === "JAVASCRIPT" ;
    var strB = "Javascript" === "Javascript" ;
    var flt = 7.5 === 7.5 ;
    var intA = 8 !== 8 ;
    var intB = 24 > 12 ;
    var intC = 24 < 12 ;
    var intD = 24 <= 24 ;

    var str = "String equality test: " + strA ;
    str += "<br>String equality test 2: " + strB ;
    str += "<br>Float equality test: " ; + strC ;
    str += "<br>Integer inequality test: " + intA ;
    str += "<br>Greater than test: " + intB ;
    str += "<br>Less than test: " + intC ;
    str += "<br>Less than/Equal to test: " + intD ;

    document.getElementById( "panel" ).innerHTML = str ;
}
document.addEventListener("DOMContentLoaded" , init , false) ;

So the output I get is as follows;

String equality test: false
String equality test 2: true
Float equality test: true
Integer inequality test: false
Greater than test: true
Less than test: false
Less than/Equal to test: true

This is obviously correct, I just don't understand how something like the following isn't outputted because surely I am adding each line to the var str, which is "String equakity test: false

String equality test: false
String equality test: false String equality test 2: true
String equality test: false Float equality test: true
String equality test: false Integer inequality test: false
String equality test: false Greater than test: true
String equality test: false Less than test: false
String equality test: false Less than/Equal to test: true

There is a ; on the third line in front of the + . Removing that should solve the problem. The variable strC which you try to add after that strange + doesn't exist.

I just don't understand how something like the following isnt outputted because surely I am adding each line to the var str , which is "String equakity test: false"

No, the only time that str is equal to "String equality test: false" is immediately after the first line. Each line modifies the str variable by adding something to whatever it became after the line before. So after the first line:

var str = "String equality test: " + strA ;

...the variable str is now equal to

"String equality test: false"

Then after the second line:

str += "<br>String equality test 2: " + strB ;

...the variable str is now equal to

"String equality test: false<br>String equality test 2:"

The third line has an error, but assuming you remove the extra semicolon and fix the variable name:

str += "<br>Float equality test: " ; + strC ;
// should be
str += "<br>Float equality test: " + flt;

...then the variable str will be equal to

"String equality test: false<br>String equality test 2: true<br>Float equality test: true"

...and so forth.

You can see this clearly if you add a console.log(str); statement in between each line and open your browser's console before running the code. As shown here: http://jsfiddle.net/mfKy9/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM