Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Side Effect Javascript

I'm having trouble understanding side effects in javascript. What line in the following code is a side-effect of calling the function kittenHug? Thanks.

var bob = {
  name: "bob",
  smiling: false
};

function kittenHug(person) {
  person.smiling = true;
  return (person.name + "was hugged by a kitten");
}

kittenHug(bob);

2 Answers

Jason Anders
Jason Anders
145,864 Points

I think the 'side-effect' would be changing the condition of Bob smiling. Bob is NOT smiling, but when you call the function with 'bob' as the argument, the function forces a change in Bob and makes him smile (even though he is not). Therefore, Bob is now "hugged by a kitten" even though he really isn't smiling. Make sense?

Yes that makes sense...