Cannot read property "x" of undefined
This error appears when you try to access a property of a variable but that variable is undefined.
The most common scenario for this is trying to access a property inside an object that is also inside an object.
Consider this object:
let user = {
name: {
first: 'Dan',
last: 'Hanson',
},
};
The following line of code would not work:
user.names.first;
//names is not a property of user (it should be name, singular), so .first would be trying to access an undefined variable
Solutions:
- Double check your syntax to make sure you don't have any typing errors
- Make sure you are acessing things in the correct order and scope
Cannot read property length of "x"
Only strings and arrays have the property length, so if you're getting this error the variable you're trying to use might not be of the type you think.
Solutions:
- In case of doubt console log the variable to see if it's of the desired type (string or array)
ReferenceError: "x" is not defined
This error occurs when you try to use a variable that doesn't exist.
Solutions:
- Make sure the variable you are trying to use exists in your code
- Double check your syntax to make sure you typed the variable name correctly
Uncaught Syntax error
This error occurs when you have a typing mistake, for example:
console.log("Hello)
//SyntaxError: Invalid or unexpected token
//Since we didn't close the double quotes ("") the software doesn't know where the string ends
console.log("Hello"):
//SyntaxError: Unexpected token ':'
//Here we used a colon instead of a semicolon, which isn't the correct way to end a line of code
Solutions:
- Check your syntax to make sure you typed the variable name correctly
TypeError: Assignment to constant variable
This error occurs when you try to assign a new value to a variable declared with const
, for example:
const course = 'Webdev';
course = 'UXUI';
Solutions:
- Either create a new variable OR declare a variable with
let
if you will need to change its value in the future.
"x" is not a function
This error occurs when you try to invoke a function that doesn't exist. It can happen mainly for three reasons:
When we mispell the name of a function:
console.lg('Hello');
//console.log is mispelled here
When you try to invoke a function that wasn't created:
randomFunction();
//if we didn't create a function with the name randomFunction, we would get the error
When we try to call/invoke something that isn't a function:
let name = 'George';
name();
//name is not a function, it's a string
Solutions:
- Make sure that what you are invoking exists and if so, check if it's correctly typed.
RangeError - Maximum Callstack Exceeded
This error occurs when we don't stop an infinite loop or a recursion from happening: For example, if we have a function calling itself and no way of stoping it:
function recursive() {
recursive();
//the function is calling itself with no condition telling it when to stop, so it creates an infinite recursion loop
}
recursive();
Solutions:
- Make sure that when you use recursion or you have loops, they have conditions that make them eventually stop
What does deprecated mean?
You will sometimes see that a function, package or technology is marked as "deprecated". Deprecated just means that something is available BUT it's not recommended that you use it, either because it might be outdated or it'sm not currently supported.
For example in javascript we have a string method called substr
which does the same as the substring
method.
While we can still use the substr
method, it is not recommended because it won't be updated anymore and in the future it might even stop working with some browsers.