This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Judy
over 10 years

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

1 vote

Permalink


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:

  1. Correct your syntax by making sure you don’t have console.log = anywhere in your code.
  2. Then reload/refresh the webpage. That is the only way to restore console.log() to its normal state.
points
Submitted by Judy
over 10 years

1 comments

CJ McCollum over 10 years

Thanks…I had the correct code & reloading the page works.

Answer 5234b523548c35f25f004ecf

1 vote

Permalink


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:

  1. Correct your syntax by making sure you don’t have prompt = anywhere in your code.
  2. 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

points
Submitted by Judy
over 10 years

Answer 5234b9ae80ff33b198004fe5

1 vote

Permalink


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:

  1. Correct your syntax by making sure you don’t have confirm = anywhere in your code.
  2. 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

points
Submitted by Judy
over 10 years

2 comments

ido.kt over 9 years

for me it was the popup blocker!! drove me crazy! thanks!

imantas2 over 9 years

lol u guyze r hax0rz

Answer 525361a080ff33767e003156

0 votes

Permalink

Thank you so much for posting this, being a person completely new to coding all of this helped a ton!

points
Submitted by rockius
over 10 years

9 comments

mikisaw357 over 10 years

Must every part of the code be encapsulated by quotation marks?

Judy over 10 years

@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?

Prateek U Keshari about 10 years

This worked. Thank you so much! :D

Rita Usanga almost 10 years

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

Judy almost 10 years

@Rita, have you tried reloading/refreshing the web page yet?

Rita Usanga almost 10 years

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.

Judy almost 10 years

@Rita, is what you’ve shown there all of your code? I can’t see anything there that would cause that message.

Rita Usanga almost 10 years

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();

Judy almost 10 years

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

0 votes

Permalink

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));
points
Submitted by Quin
over 10 years

3 comments

Judy over 10 years

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”.

Quin over 10 years

Okay, I figured it out. Thanks for the help!

Judy over 10 years

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

0 votes

Permalink

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();
points
Submitted by UltimatePAC
over 9 years

1 comments

Judy over 9 years

Your add function looks perfect. The error must be elsewhere in your code.

Answer 541b2e16631fe9f65a000f8d

0 votes

Permalink

If somewhere along the way you assigned prompt or confirm to a var, an easy solution to this is to type:

delete prompt;
delete confirm;

somewhere in the CodeAcademy console. You only have to do this once, and that should clear up the problem.

points
Submitted by jwetherb
over 9 years

Answer 54d7954c76b8feeb2e00734f

0 votes

Permalink

“yourName”.length will not work for me. Help please!

points
Submitted by daybeforenight
about 9 years

1 comments

Judy about 9 years

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

0 votes

Permalink

I had a similar problem. The error is in the ‘printPerson’ function. Check that you have the following code: function printPerson(person) { console.log(person.firstName + “ “ + person.lastName); }

points
Submitted by IngWARr
about 9 years

Answer 55c99f3a937676336b000062

0 votes

Permalink

I had a syntac problem

points
Submitted by Jessy
over 8 years