Skip to content Skip to sidebar Skip to footer

Javascript Suppress A Specific Error

I have the following line in my JavaScript file, which creates an error message on the console: myObject.width = 315; I want to hide this error message on the console. I mean that

Solution 1:

Generally speaking this shouldn't need to yield an error. What's the error you're getting? That myObject isn't defined? If so, just add a safeguard and check if it's defined. Such as:

if (myObject) {
    myObject.width = 315
}

That said, you can surpress it by wrapping it in a try-catch

try {
    myObject.width = 315;
}
catch(err) {
    // do nothing
}

To see what's happening, try running the following code and see what's happening when you're removing the var myObject = {} line.

var myObject = {}

try {
    myObject.width = 315;
} catch(err) {
    console.log('error');
}

console.log(myObject)

Post a Comment for "Javascript Suppress A Specific Error"