How to check undefined or null value of JavaScript's object or value

[Mobile version(QRCode)] Total views:65,961 / Applauds for blog:1
Welcome to my page. I am an adminisrator of this site.
If you are this db's user, please contact with me by private mail. If not, please contact with me by email or twitter or facebook
Access record[Graph / PV Info.(Past 1 day / Past 1 week) / Access from outside (Yesterday / Past 1 week) / Vistors's list]
ProfilePmail(Mail)
Inbox   /Send   /Sent
Reviews(List   /Limit)
Poll   /Agree:Got   /Sent
Fan
Works/Music
Blog
[Write]
Links
My Play List
<=Newer article Useful libraries of react
=>Older article How to create best 1week plan for sprint

1.
2017/08/06 (Updated 2017/11/05) "JavaScript(node.js) > How to check undefined or null value of JavaScript's object or value"
[Show only this article / Modify / Delete / Send trackback / Add to the shared category]

1. Problem of checking valid value of JavaScript's object
2. How to install

1. Problem of checking valid value of JavaScript's object

To check the validness of JavaScript's object, you have to write very log description.
Example.
if (typeof geoinfo !== 'undefined' && geoinfo !== null && typeof geoinfo.geometry !== 'undefined' && geoinfo.geometry !== null && typeof geoinfo.geometry.location_type !== 'undefined' && geoinfo.geometry.location_type !== null) {
....
}

You can use @hikarine3/is-defined to check valid value of Javascript's object with ease.
After installation of npm and npm's module of "@hikarine3/is-defined," you can rewrite above sentence like this.

import isDefined from '@hikarine3/is-defined';
if (isDefined(geoinfo, "geometry.location_type")) {
...
}

Quite Simple.
If the tier of object is only 0 or 1, you can write this way.

import isDefined from '@hikarine3/is-defined';
if (isDefined(tier0obj)) {
...
}

import isDefined from '@hikarine3/is-defined';
if (isDefined(tier0obj.tier1)) {
...
}

2. How to install

If you have already installed npm, you can do like this.

npm install @hikarine3/is-defined;

Here is npm and github page.
https://www.npmjs.com/package/@hikarine3/is-defined
https://github.com/hikarine3/is-defined

Add comment to this article


[Read other articles]
<=Newer article Useful libraries of react
=>Older article How to create best 1week plan for sprint


Articles categorized as "JavaScript(node.js) by this user"
All articles of this user
Subscribe to RSS
RSS
Term
Category
All
1.Japan
2.Atlassian's products
3.Self
4.Development of this site
5.Japanese comics
6.Japanese anime
7.Weekly hot news of Japanese culture
8.OP/ED/PV
9.Japanese game
10.Ranking
11.Japanese Comics (Manga)
12.Search Engine
13.Japanese drama
14.Japanese otaku culture
15.Programming
16.Ineternet world
17.Movie
18.C/C++
19.BerkeleyDB
20.Apache programming
21.Spam
22.Meteor
23.Marketing
24.Python
25.Scrum
26.JIRA
27.Git
28.CI
29.Jenkins
30.AWS
31.Operation
32.Singapore
33.Cloud
34.Mysql Cluster
35.DevOps
36.Bitbucket
37.Xamarin
38.Good and new
39.Monitoring
40.JavaScript(node.js)
41.React
42.Phillipines
43.Hiring
44.Python
45.SEO
46.Malaysia
47.Mongodb
48.Perl
49.Docker
50.Life hack
51.Dance
52.QA
53.Mysql
54.Digital Life Hack
55.Project management
56.Django
57.Gatsby
58.Administrator
Sayings from S-Cry-Ed

Rule in this world is speed. Even stupid person can write cool novel if he can spend 20 years for it.

If someone helped me, I will help him in return, which is my rule.

To become stronger, consider what is cowardliest thinking. And rebel against the thinking, which will make you stronger.



I am Japanese but working for some English sites.

Doctor Job Career
Nurse Job Career
Top Page top MetaSeachJP Works