. Advertisement .
..3..
. Advertisement .
..4..
It is common to use the typeof operator to check a variable’s data type in Javascript. This unary operator often returns a string showing the unevaluated operand’s type. Here, the object value’s intended absence is well described with the null value.
This article offers three main methods to check if an object is null in Javascript.
How To Check If An Object Is Null In Javascript
Null is a Javascript object with a primitive value. These values include Number, undefined, String, Symbol, and Boolean. The result will be an object when you call the typeof operator on a null value.
Code:
data = null
if (data) {
console.log('The null value is not falsy')
}
else {
console.log('The null value is falsy')
}
Output:
The null value is falsy
Here are two methods to check the null value:
Method 1: Use The == Operator
Before checking, let’s create two classes, namely User1
and User2
. The first one comes with a variable name, which is retrieved by the Setter and Getter methods. On the other hand, the second class uses the getUser1Object
to return the class User1
’s instance.
Now, create the User2
class’s object named user and call the getUser1Object()
. In the if-else condition, you should use the ==
operator to check the User1
. If it returns not null, let’s call the Setter method and pass a string as parameter to it.
public class JavaCheckNullObject {
public static void main(String[] args) {
User2 user;
user = new User2();
User1 getUserObject = user.getUser1Object();
if (getUserObject == null) {
System.out.println("Object is Null");
} else {
System.out.println("Not Null");
getUserObject.setName("Sam");
System.out.println(getUserObject.getName());
}
}
}
class User2 {
User1 user;
public User1 getUser1Object() {
return user;
}
}
class User1 {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Output:
Object is Null
Method 2: Use java.utils.Objects
This class operates an object by static utility methods. One of them is isNull()
. When the reference is null, it will return a boolean. In this case, you need to create two classes like the first method.
In the next step, you need to create the User2
class’s object with the new keyword and getUser1Object()
function. The result will be an object belonging to the User1
class. You will store it in the getUser1Object
.
The isNull()
function needs to be called to check the null value. Before that, don’t forget to pass the getUserObject
object as a parameter. The object will be null if it returns true.
import java.util.Objects;
public class JavaCheckNullObject {
public static void main(String[] args) {
User2 user;
user = new User2();
User1 getUserObject = user.getUser1Object();
if (Objects.isNull(getUserObject) ){
System.out.println("Object is Null");
} else {
System.out.println("Not Null");
getUserObject.setName("Sam");
System.out.println(getUserObject.getName());
}
}
}
class User2 {
User1 user;
public User1 getUser1Object() {
return user;
}
}
class User1 {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Output:
Object is Null
Method 3: Use The Javascript Object.is() Function
The Javascript object.is()
function is a built-in option to check if two values have the same value. It is different from the loose ==
and strict ===
operators in the checking way.
This method checks for negative zero and NaN values. In checking null values, it works the same way as the ===
triple operator. This function is ideal for comparing null and undefined values.
Code:
data = null
console.log("The value of data is: ", data)
console.log('If data is strictly equal to undefined', Object.is(data, undefined))
console.log('If data is strictly equal to data', Object.is(data, data))
Output:
The value of data is: null
If data is strictly equal to undefined false
If data is strictly equal to data true
Conclusion
Without a shade of doubt, you are sometimes required to check if an object is null in Javascript. Check the above article to find the best ways to accomplish the task.
Leave a comment