How can i check if an object is undefined (javascript)? [duplicate] - javascript

This question already has answers here:
JS checking deep object property existence [duplicate]
(2 answers)
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
(47 answers)
Closed 5 years ago.
I have to check if a object is undefined but when i do
typeof myUnexistingObject.myUnexistingValue == 'undefined'
i get this error
Uncaught ReferenceError: myUnexistingObject is not defined
so, how can I check for undefined obects or properties?

You must check for each potentially defined property before using it:
function checkUnexistingObject(myUnexistingObject) {
if (myUnexistingObject !== undefined) {
if (myUnexistingObject.otherObject !== undefined) {
console.log("All is well");
}
}
}
checkUnexistingObject({});
checkUnexistingObject({otherObject: "hey"});

Related

in JavaScript toString method doesn't directly run an empty object, instead runs on a variable which is assigned to an empty object [duplicate]

This question already has answers here:
"{} === null" throws syntax error in developer console [duplicate]
(1 answer)
Why is 0 === {} okay, but {} === 0 throws an error? [duplicate]
(1 answer)
When does JS interpret {} as an empty block instead of an empty object?
(2 answers)
{} || [] is not valid JavaScript [duplicate]
(1 answer)
Is the curly brackets object notation valid in any expression?
(5 answers)
Closed 11 months ago.
I tried this today on my browser console and saw something weird. Please help. Can someone help me understand why It happens ?
Try 1:
{}.toString(); // It shows an error - Uncaught SyntaxError: Unexpected token '.'
Try 2:
const a = {};
a.toString(); // It returns '[object Object]'

Cannot read property 'cnt' of undefined [duplicate]

This question already has answers here:
What's the simplest approach to check existence of deeply-nested object property in JavaScript? [duplicate]
(7 answers)
Closed 4 years ago.
This is my code snippet
pastActions = res['data']['list']['names']['blk'].cnt
But I get to see below error
its throws error as Cannot read property 'cnt' of undefined
This is because the parent of cnt is undefined, it might even fail when any of these properties data or list or names or blk is undefined.
I have replaced it as below
res['data'] && res['data']['list] && res['data']['list']['names'] && res['data']['list']['names']['blk'] && res['data']['list']['names']['blk'].cnt ? res['data']['list']['names']['blk'].cnt : '';
and it works, but is there any optimized way to check the same?
you can use lodash _.get function. You have an option to return a default value when you have any of the property in the object as undefined.
check here: lodash.com/docs/4.17.10#get

Shortcut for accessing member of an object [, of an object...] [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 6 years ago.
Is there a short way for accessing a nested object without checking each parent object?
this will throw an exception if foo or bar is undefined:
var exists = (foo.bar.value !== undefined);
I would expect a check function like:
var exists = Object.exists(foo.bar.value);
Is there already something build-in javascript?
Use typeOf
if (typeof myObject!= "undefined") {
console.log('It exists')
}

check if a javascript function is exist or is defind [duplicate]

This question already has answers here:
How to tell if a JavaScript function is defined
(23 answers)
Closed 9 years ago.
I want to know whether a function is exist or is defined before i call it.
I have this line of code:
function renderView(){
// some code
}
You can check the typeof, it returns undefined if it's not defined or out of scope, and function if it's a function
if ( typeof renderView == 'function' ) {
// it exists
}

How can I check the variable whether it exists or not [duplicate]

This question already has answers here:
Detecting an undefined object property
(50 answers)
How to check a not-defined variable in JavaScript
(15 answers)
Closed 9 years ago.
var a;
typeof(a);
//undefined
typeof(c);
//undefined
if(c) {}
//throw error
How can I know that c doesn't exist without try catch.
Update after marked duplicate:
typeof initializedVariable and typeof notInitializedVariable both will show 'undefined'. My question is to know whether the variable exists(initialized) or not.
You can use the typeof operator.
if (typeof a === 'undefined') {
// variable is undefined
}

Categories

Resources