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

JavaScript - Parenthesis leading to no need to 'escape' quotes

Hi ! The following is a snippet of code from an exercise in the JavaScript Loops, Objects and Arrays section.

I have singled out the code that is within the for loop of which is under great scrutiny !! Hahahaha, i'm simply wondering if anyone could explain or better yet point to a resource explaining the lack of escaping the inner quotes?

From my understanding this -- let Jim = " He say\'s 'hello' "; -- is perfectly acceptable.

var html = ' '; var red; var green; var blue; var rgbColor;

for (var i = 0; i < 30; i++) { red = Math.floor(Math.random() * 256 ); green = Math.floor(Math.random() * 256 ); blue = Math.floor(Math.random() * 256 );

rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';

html = '<div style="background-color:' + rgbColor + '"></div>'; document.write(html); }

1 Answer

Steven Parker
Steven Parker
243,215 Points

Escaping is only needed when the inner quotes are the same kind as the outer ones. Since you have three kinds to choose from, it's often possible to avoid needing to do this.

Any quotes of a different kind within the string are treated as ordinary characters.