Checking 'undefined' or 'null' of any Object

checking account definition
free checking account banks
best checking accounts
checking account balance
open checking account online instantly
free checking account no credit check no deposit
checking account example
checking account vs savings account

I am working on Angular project and time to time I used to have check undefined or null over Object or it's properties. Normally I use lodash _.isUndefined() see example below:

this.selectedItem.filter(i => {
    if(_.isUndefined(i.id)) {
      this.selectedItem.pop();
    }
})

I couldn't see any problem with it. But I had discussion with my colleague during review of above code. He was telling me that if i gets undefined before the if statement then it will throw the exception. Instead he suggested me to always check i or i.id like this:

if(!!i && !!i.id) {
      this.selectedItem.pop();
}

I am convinced what he was trying to say unlike his way of checking undefined in above code. But then I was thinking what is the purpose of lodash _.isUndefined?

Could anyone please let me know what is the best or clean way to do it. Because for me !!i && !!i.id is not readable at all.

Many thanks in advance.

You can use _.isNil() to detect undefined or null. Since you're using Array.filter(), you want to return the results of !_.isNil(). Since i is supposed to be an object, you can use !_.isNil(i && i.id).

Note: you are using Array.filter() as Array.forEach(). The callback of Array.filter() should return a boolean, and the result of the filter is a new array.

const selectedItem = [
  undefined,
  {},
  { id: 5 },
  undefined,
  { id: 7 },
];

const result = selectedItem.filter(i => !_.isNil(i && i.id));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

How to check if a bank account is current or savings, , automated teller machines, and electronic debits, among other methods. Synonyms for checking at Thesaurus.com with free online thesaurus, antonyms, and definitions. Find descriptive alternatives for checking.

Use typeof i.id === 'undefined' to check for undefined and i.id === null to check for null.

You could write your own helper functions to wrap any logic like what LoDash has. The condition with !!i && !!i.id is only looking for falsy values (empty string, 0, etc), not only null or undefined.

2 Steps: Open a Bank Account Online with No Deposit (& Bad Credit , has sufficient funds and the ATM is functional and has enough cash to dispense. Compare Chase checking accounts and select the one that fits your needs. Open a checking account and enjoy the ease of banking with Chase.

You could check for i and if it is not truthy or if the property is undefined or null, then do something.

if (!i || i.id === undefined || i.id === null) {
    this.selectedItem.pop();
}

What is a Checking Account and How Does it Work?, Open a checking account, and you'll bank with no monthly fees. Plus, your first Use our mobile app to transfer money, deposit checks and manage accounts. Checking Account: A checking account is a deposit account held at a financial institution that allows withdrawals and deposits . Also called demand accounts or transactional accounts, checking

Referring to a variable which has undefined as it's value won't throw any error. You get a ReferenceError for referring to variable that is not defined:

> i
Uncaught ReferenceError: i is not defined

If you pass a not-defined variable to a function a ReferenceError is thrown and the function won't be executed.

> _.isUndefined(i)
Uncaught ReferenceError: i is not defined

typeof operator should be used for safely checking whether a variable is defined or not:

> typeof i
'undefined'

In your code the i is defined (it's a function argument) so by referring to it you won't get any ReferenceError. The code will throw a TypeError when i is defined, has undefined value and you are treating it as an object:

> var i = undefined; i.id
Uncaught TypeError: Cannot read property 'id' of undefined

Online Checking Account | No-Fee 360 Checking, Compare free and low-fee checking accounts from hundreds of banks and credit unions nationwide. Find the best checking account for you and open an  Apply now for the Safe Debit Account. Learn more about the Safe Debit Account. Compare checking accounts. This handy comparison chart gives you a brief look at some features and benefits of our checking accounts, side by side. Compare checking accounts. Monthly maintenance fee. Easy Checking is.

Your friend is right. When you do, _.isUndefined(i.id) you're assuming i to not to be undefined. You're assuming i is an object which will have an id property which you're checking if it is falsey or not.

What happens when itself is undefined? So you will end up undefined.id which is an error. Therefore you could simply do this

if(i && i.id) {
  // You're good to go
}

The above will check for all falsey values, 0 and "" included. So if you want to be very specific, then you'll have to check the types of both using typeof operator.

Checking Accounts, All our checking accounts include: Branches in more than 25 states. One of the largest ATM networks in America. No surcharges at MoneyPass® Network ATMs. Free access to 13,000 ATMs, and 24/7 access to customer support. More Checking Benefits. Everyday Checking monthly service fee waived for all 17-24 years old primary owners! Checking Account Guide. Access saved application. Student Checking. GreenhouseSM app. Overdraft Protection. Overdraft Rewind® feature. Manage online payments.

Checking accounts | Open a Personal Checking Account, Essential Checking. Just what you need, right when you need it. No fees. Get your paycheck up to two days early with Direct Deposit Express. Unlimited ATM fee  360 Checking is a checking account that comes with everything you need and not everything you don’t. Pay your bills, get cash, make deposits, and transfer money–all without the extra monthly fees and extra trips.

Checking Accounts | Open a Bank Account Online, Open a Checking Account from Citizens Bank and enjoy secure banking, mobile check deposit, overdraft solutions and more. Enroll to get started today. Depending on your preference, you can apply for a Citizens checking account online, over the phone, or at any Citizens Bank branch. To apply online, just select the appropriate link from the top of this page and find the account you wish to open.

Online Checking Accounts | Open Yours Today, Compare Chase checking accounts and select the one that fits your needs. Open a checking account and enjoy the ease of banking with Chase. Bank of America Advantage Banking is a flexible checking account for your changing needs. It offers a variety of features from digital-only payments (checkless checking) to optional overdraft protection. Open a Bank of America Advantage Banking account online today.

Comments
  • Did you look at the function typeof() developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… ?
  • in your angular project, are you using typescript or just javascript?
  • if(!!i && !!i.id) does a different check than _.isUndefined!!! It will check if the property is truthy, not if it's undefined. It will also catch null, 0, and "" (empty string), for example. Also, the first implicit boolean conversion !!i is superfluous.
  • @Anouar Yes I am using TS
  • I'am usually use if (!variable). Well if variable is a string and is equal to "" or is a number and is equal to 0, the condition is true
  • I have suggested that in the comments but there is this one stupid thing with NaN eg. typeof(NaN) -> returns "number"
  • @tutorialfeed thats up to your standards.
  • Yep. This is the correct answer. (I was just about to write the exact same thing)
  • Answer straight from the source :P
  • @vlaz Yeah, that's pretty official :)
  • Yes I did this check in the console but my intention was to write more clean and readable code. :)
  • Unless i.id is zero. Or if it's an empty string. Both probably should not happen, but it might with some mock data or some synthetic records.