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 Basics (Retired) Storing and Tracking Information with Variables Introducing Variables

Sufiyaan Haroon
seal-mask
.a{fill-rule:evenodd;}techdegree
Sufiyaan Haroon
Full Stack JavaScript Techdegree Student 9,558 Points

What is the difference between: alert("My message"); and: message = "My message":

What is the difference between:

alert("My message");

and:

message = "My message";

It gives you the same result

This one uses a variable. You are alerting the VALUE of the variable:

message = "My message"; alert(message);

message = "My NEW message"; alert(message);

Unlike alert("My message"); -- this one simply alerts the string "My message".

Using variables is a good way to organize your code and stash useful information in memory that you might need to use more than once.

Say you have 20 alerts with the same exact message throughout your entire program and you decide to change that message. You'd have to find and edit ALL those alerts in order to make those modifications.

Now if you have this message stored in a variable, you only need to change the VALUE of that variable.

2 Answers

Hi Sufyaan. It probably seems like they are the same but they are not. alert(message) will print the message to an alert box when called. message = "my_message" is is saving "my_message" in the variable message for later use. At this point it may not be apparent, but saving information in variables is a huge part of programming. That is probably what you are supposed to get out of that bit of lesson. Have fun :D

anish talwar
anish talwar
1,362 Points

alert("My message"); will print "My message" and message = "My message"; // where message is a variable so you are storing the string to that variable.

to pass this we can do alert(message); //this will help you to print "My message" in your alert box in the browser

hope this clears the doubt :D anything else would happy to answer