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

Jess Cushnie
Jess Cushnie
3,782 Points

TypeError: firstName not a function

Hi, i'm really stuck on the JavaScript task where you add the variables: first name, last name and role together in one string. When i try to create another variable eg. let msg = firstName + lastName + role they are showing as functions. Which then gives me the error!

3 Answers

Hi Jess, I can't see the objectives for the task (it would be great if you posted the code!) but from what you've said, it sounds like the previous tasks made you write functions (called firstName, lastName and role) that return the string values. So what we need to do is call each of the functions and get the returned values.

So if you wanted to assign a string to the msg variable, you'd need to do something like this:

let msg = firstName() + lastName() + role()

Hope this helps

I might be way off base here, but your challenge description sounds a lot like the challenge where you're just supposed to be concatenating strings. If I'm wrong, please just ignore the rest of this message. I'm not sure what you would have typed to get the message, but since I'm not sure about the challenge, and I'm not sure what is in your code, I'll just post the answer so you can compare what you have:

let firstName = 'John';
let lastName = 'Doe';
let role = 'developer';
let msg = firstName + ' ' + lastName + ': ' + role;

If the issue is with the final task of this challenge, where you have to make the role uppercase, from your error, it sounds like you might be putting parentheses around role or something. All you need to do for the last task is convert role to uppercase by using the toUpperCase() method:

let firstName = 'John';
let lastName = 'Doe';
let role = 'developer';
let msg = firstName + ' ' + lastName + ': ' + role.toUpperCase();
Jess Cushnie
Jess Cushnie
3,782 Points

Hi Jason yes this is exactly what I am struggling with, I wrote the code exactly as you have but when I get to the

Let = msg firstName + β€˜ β€˜ + lastName + β€˜ : β€˜ + role;

The firstName, lastName and role are all in red not blue and not acting as a variable and that’s when I get the error.

In the code in the answer above, the variables are green, but in the challenge, they will be pink. Are you still getting the original error of TypeError: firstName not a function, or are you simply getting an error that your answer is not correct? The only way I can reproduce the error is by putting parentheses after firstName.

Also... you said in your last reply that you wrote the code exactly as I have it, but then you put in a line of code that is different. You said:

I wrote the code exactly as you have but when I get to the

Let = msg firstName + β€˜ β€˜ + lastName + β€˜ : β€˜ + role;

There are 4 problems with this line: first, 'Let' should be lowercase ( let ). Second, the equal sign goes between msg and firstName, not between let and msg. Third, there should not be a space between the first quote and the colon, only after the colon, before the final quote. The last thing is very subtle, and I'm honestly not even sure how you typed it, unless maybe you have a non-us keyboard, and that is the quotes that you are using. A single quote mark should look like a comma, just raised to the top of the line (or like a straight line that slants from upper right to lower left slightly), but your quote marks look like upside down commas. Note the difference in the code below (actually, in this code window, yours look more like backticks, but when I pasted yours into the challenge, they were definitely different):

//mine
let msg = firstName + ' ' + lastName + ': ' + role;

//yours
let msg = firstName + β€˜ β€˜ + lastName + β€˜ : β€˜ + role;

Make sure you are using single or double quotes when putting in strings (or you can use backticks for template literals). Having said that, none of those problems should be causing the error you're seeing. Also, make sure you aren't typing your code into some sort of word processor and then cutting and pasting it, as word processors can sometimes automatically replace characters, and that can cause your code to break.

Jess Cushnie
Jess Cushnie
3,782 Points

I just triple checked and didn't had a set of () in there for some reason, i don't know how i didn't realise earlier.. Thanks so much for your help!