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 Using String Methods

Brian Diggs
Brian Diggs
4,794 Points

TypeError: 'undefined' where am i going wrong?

what does it mean by undefined

app.js
var id = "23188xtr";
var lastName = "Smith";

var userName;
 id.toUpperCase();
userName.toUpperCase();
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>

You attempt to use the toUpperCase() method on username when userName contains no value. If you want username to be assigned "id.toUpperCase()" you need to tell javascript that as soon as you declare it. var username = id.toUpperCase();

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! JavaScript is a little special. It has something it calls "type inference". In many languages, to use a variable at all, you have to first tell the language what data type that variable is going to be storing. JavaScript, on the other hand, makes a best guess as to what type of data that variable is holding by the way you try to use it later.

So any variable that is declared without an initial value is considered "undefined", because it has no idea yet what kind of data it is holding or will hold in the future. You were on the right track with the id.toUpperCase(). And it does actually perform that method on the id. But because you haven't assigned the results anywhere, they're sort of floating out in limbo now. What we want is to assign the results to userName and thus, the variable will become defined.

Take a look at the line you need:

var userName = id.toUpperCase();

This takes the string stored in id and runs the toUpperCase method on it which turns it all into an upper case variant of that string. The result is then assigned to the userName variable.

Hope this helps! :sparkles: