SmokingMan Posted January 31, 2006 Share Posted January 31, 2006 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 More sharing options...
Chocolate570 Posted January 31, 2006 Share Posted January 31, 2006 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 More sharing options...
SmokingMan Posted January 31, 2006 Author Share Posted January 31, 2006 "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 More sharing options...
aspnetguy Posted January 31, 2006 Share Posted January 31, 2006 You've got it! yes the value is now 10. Link to comment Share on other sites More sharing options...
Chocolate570 Posted January 31, 2006 Share Posted January 31, 2006 "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.<{POST_SNAPBACK}> 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now