Jump to content

General var question on initializing


SmokingMan

Recommended Posts

I'm learning JS on my own and have run acroos an example that isn't explained clearly enough for me. A variable is being initialized with a long string of data that will be used to write an entire new document. The example shows it written like this:

var newContent = "lots of data & HTML tags"newContent += "lots more data & HTML tags"newContent += "yada yada yada"

Is the "+=" used to continue the variable initialization on a separate line? This may very well be covered later in the book, but it's bugging me now and I would like to know just to understand it better. :)

Link to comment
Share on other sites

Check out the operators page. What the += does is this. In this example, the two statements are the same.

//First Statement, using the +=var x;x+=2;//End of first. Starting second.x=x+2//End....

As you can see, += means the same thing as the variable is itself plus an integer or another variable. Do you get what I mean?

Link to comment
Share on other sites

"I can see clearly now the rain is gone" Sorry, couldn't help it :) I think I understand now:var x = 2;x += 8;which means x now = 10. It takes the previous value of "x" and adds the new value to the current value of "x"...right?If not, I guess I'll need more coffee to finish waking up.

Link to comment
Share on other sites

"I can see clearly now the rain is gone"  Sorry, couldn't help it  :) I think I understand now:var x = 2;x += 8;which means x now = 10.  It takes the previous value of "x" and adds the new value to the current value of "x"...right?If not, I guess I'll need more coffee to finish waking up.

Gosh darnit...I forgot to take out my new umbrella. Oh well, next time it comes around, i'll be waiting. :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...