This forum is now read-only. Please use our new forums! Go to forums
Type Errors - learn how to fix these
You will encounter several categories of error messages as you learn to write JavaScript code.
Reference Errors: you will get these when you try to use (reference) a non-existant variable
Syntax Errors: you will get one of these when the structure of one of your JavaScript statements violates the syntactic rules - think of these as bad grammar
Type Errors: these show up when a value is not the expected type
In the sections below you will find the different Type Errors you may encounter.
If you want to talk about concepts or “next steps” or anything general, head over to the Groups page and join in one of the already existing discussions or start your own. Checkout the “Guidelines” post that is pinned in most groups to see if you’ve found a group that appeals to you.
Answer 52349d2c80ff339599004ad3
TypeError related to console.log()
Error message:
- “TypeError: Property ‘log’ of object # (Chrome)
- “TypeError: console.log is not a function” (Firefox)
- “TypeError: ‘your string’ is not a function (evaluating ‘console.log(“your string”)’)” (Safari)
- “TypeError: Function expected” (IE)
What has gone wrong?
You have changed console.log() from a function into a string or a number.
Explanation:
console.log() is a function, it will log/print on the console/screen the message or value you put in the ( )
.
console.log() is not a variable - it is not expected that you will want to make console.log() EQUAL to something. The interpreter won’t stop you from doing so but you should understand the consequences.
At some point recently, you ran your code with a statement resembling this:
console.log = "You are allowed to play.";
console.log = 18;
It may not have been one of those exact values but somewhere you told the interpreter to make console.log
equal to something. You assigned a new value to console.log(). The result of doing so was that the usual code that is invoked when you use the console.log()
function was overwritten by whatever it was you put on the right hand side of the =
sign.
Normally console.log() is a function but now you have turned it into a string or a number.
If you are curious you can check to see if that is true. You can run these commands in the console to see how you have changed console.log() from a function to a string:
Normal:
typeof(console.log)
=> 'function'
Changed to a string:
console.log = "Hello?"
=> 'Hello?'
typeof(console.log)
=> 'string'
Changed to a number:
console.log = 18;
=> 18
typeof(console.log)
=> 'number'
How to fix this:
- Correct your syntax by making sure you don’t have
console.log =
anywhere in your code. - Then reload/refresh the webpage. That is the only way to restore
console.log()
to its normal state.
Answer 5234b523548c35f25f004ecf
TypeError related to prompt()
Error message:
- “TypeError: Property ‘prompt’ of object [object Object] is not a function” (Chrome)
- “TypeError: prompt is not a function” (Firefox)
- “TypeError: ‘a string, this could vary’ is not a function (evaluating ‘prompt(“your question”)’)” (Safari)
- “TypeError: Function expected” (IE)
What has gone wrong?
You have changed prompt() from a function into a string or a number.
Explanation:
prompt() is a function, it is waiting for you to put something inside those ( )
. It will then put a popup on the screen with the message you put in the ( )
.
prompt() is not a variable - it is not expected that you will want to make prompt() EQUAL to something. The interpreter won’t stop you from doing so but you should understand the consequences.
At some point recently, you ran your code with a statement resembling this:
prompt = "What is your age?";
prompt = 18;
It may not have been those exact values but somewhere you told the interpreter to make prompt
equal to something. You assigned a new value to prompt(). The result of doing so was that the usual code that is invoked when you use the prompt()
function was overwritten by whatever it was you put on the right hand side of the =
sign.
Normally prompt() is a function but now you have turned it into a string or a number.
If you are curious you can check to see if that is true by running this code that checks what prompt()
is currently set at:
console.log("Before I messed up prompt() it looked like this: \n " + prompt + "\n");
prompt = "What is your age?";
console.log("Now prompt() looks like this: " + prompt);
How to fix this:
- Correct your syntax by making sure you don’t have
prompt =
anywhere in your code. - Then reload/refresh the webpage. That is the only way to restore
prompt()
to its normal state.
Popup Blockers
You may also get the error if you are running a strict popup blocker. Check to see if your popup blocker is blocking codecademy.com
Answer 5234b9ae80ff33b198004fe5
TypeError related to confirm()
Error message:
- “TypeError: Property ‘confirm’ of object [object Object] is not a function” (Chrome)
- “TypeError: confirm is not a function” (Firefox)
- “TypeError: ‘a string, this could vary’ is not a function (evaluating ‘confirm(“your question”)’)” (Safari)
- “TypeError: Function expected” (IE)
What has gone wrong?
You have changed prompt() from a function into a string or a number.
Explanation:
confirm() is a function, it is waiting for you to put something inside those ( )
. It will then put a popup on the screen with the message you put in the ( )
.
confirm() is not a variable - it is not expected that you will want to make confirm() EQUAL to something. The interpreter won’t stop you from doing so but you should understand the consequences.
At some point recently, you ran your code with a statement resembling this:
confirm = "Ready to play?";
It may not have been that exact string but somewhere you told the interpreter to make confirm
equal to something. The effect of doing so was that the usual code that is invoked when you use the confirm()
function was overwritten by whatever it was you put on the right hand side of the =
sign. If you are curious you can check to see if that is true by running this code that checks what confirm()
is currently set at:
console.log("Before I messed up confirm() it looked like this: " + confirm);
confirm = "Ready to play?";
console.log("Now confirm() looks like this: " + confirm);
How to fix this:
- Correct your syntax by making sure you don’t have
confirm =
anywhere in your code. - Reload the webpage. That is the only way to restore
confirm()
to its normal state.
Popup Blockers
You may also get the error if you are running a strict popup blocker. Check to see if your popup blocker is blocking codecademy.com
Answer 525361a080ff33767e003156
Thank you so much for posting this, being a person completely new to coding all of this helped a ton!
9 comments
Must every part of the code be encapsulated by quotation marks?
@mikisaw, I’d like to answer but I don’t understand what you are getting at there. I see you are just starting out here - Welcome to the JavaScript Track! Are you getting a Type Error already?
This worked. Thank you so much! :D
I keep getting this error on 8/9 of the rock, paper, scissors build. I know it relates to line 1 of my code but I am not using prompt as a variable. Any What am I doing wrong? Here’s the first 6 lines of my code - var userChoice = prompt(“Do you choose rock, paper or scissors?”); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = “rock”; } else if (computerChoice <= 0.67) { computerChoice = “paper”; Apologies for the loss of formatting
@Rita, have you tried reloading/refreshing the web page yet?
Tried that before asking the question, still got the error. Just tried it again now and although I’m been congratulated and prompted to move to the next course, the string is not a function type error is still appearing. This is even more confusing.
@Rita, is what you’ve shown there all of your code? I can’t see anything there that would cause that message.
This was the entire code: var userChoice = prompt(“Do you choose rock, paper or scissors?”); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = “rock”; } else if (computerChoice <= 0.67) { computerChoice = “paper”; } else { computerChoice = “scissors”; } console.log(“Computer: “ + computerChoice); console.log(“user: “ + userChoice);
var compare = function (choice1,choice2) { if (choice1 === choice2) { return “The result is a tie!”; } else if (choice1 === “rock”) { if (choice2 === “scissors”) { return “rock wins”; } else { return “paper wins”; } } else if (choice1 === “paper”) { if (choice2 === “rock”) { return “paper wins”; } else { return “scissors wins”; } } else if (choice1 === “scissors”) { if (choice2 === “rock”) { return “rock wins”; } else { return “paper wins”; } } }; userChoice(); computerChoice();
Rita: userChoice(); computerChoice(); is the problem. userChoice and computerChoice are strings, they are either “rock”, “paper” or “scissors”. The problem is that you have put () after them which is what we do to invoke a function. Your function’s name is compare. To invoke that you do this: compare(userChoice, computerChoice)
Answer 52b081838c1ccce134002925
TypeError: ‘undefined’ is not an object
TypeError: Cannot call method ‘substring’ of undefined
I guess I’ll go here for answers. I’m having trouble with substrings.
Here is my code for exercise 23:
console.log("january").substring(0,3);
TypeError: ‘undefined’ is not an object (evaluating ‘console.log(“january”).substring’)
I have no idea what I’m doing wrong, and I don’t see this in any of your examples above.
–
I figured it out, here is the working code if anyone needs it.
console.log("January".substring(0,3));
3 comments
Nice one Quin, thanks for posting it. The problem is because you are asking for the substring of console.log() instead of the substring of “January”.
Okay, I figured it out. Thanks for the help!
You’re welcome, I’m glad to see this documented. I’ve put a “title” on this so others can spot it and I’d like to whittle down the code a bit so we aren’t giving away all the fun. How about just limiting it to the January example?
Answer 53cf0e1980ff3389af000611
TypeError: Cannot read property ‘firstName’ of undefined
I am doing the “Building an Address Book” Exercise 6/6. Here is the code:
var add = function(firstName,lastName,email,phoneNumber) {
contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
};
};
add("John","Doe","[email protected]","999-999-9999");
list();
1 comments
Your add function looks perfect. The error must be elsewhere in your code.
Answer 541b2e16631fe9f65a000f8d
Answer 54d7954c76b8feeb2e00734f
“yourName”.length will not work for me. Help please!
1 comments
I’m not sure how you ended up in this TypeError thread with that question. If yourName is a variable you shouldn’t have quotes on it. If you post it in a new thread in the Q&A for the exercise that you are doing then you’ll probably get a more useful answer.
Answer 54da24de51b887186700d199
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
Thanks…I had the correct code & reloading the page works.