Check for null values in JavaScript

How do I check for null values in JavaScript?

In this article, we will learn how to check the null values in JavaScript. The null value indicates the intentional absence of any object value. It is a JavaScript primitive value that is false when used in Boolean operations. This distinguishes null from the related primitive value undefined, which is an unintended absence of any object value. This is because a variable that has been declared but not given a value is undefined rather than null.

You may visualize a variable as a box by using a real-world analogy. The box can hold things like a teapot, just like a variable can. However, when you open a box you receive and find nothing inside! You received an empty package because someone made a mistake. The box has a null value, which is another way of stating it contains nothing.

Читайте также:  Install python numpy on windows

There are different ways to check whether a value is null in JavaScript.

  • Using the Strict equality Operator (===)
  • Using the Object.is() Function
  • Using the typeof Operator

Using the Strict equality Operator (===)

In this approach, we will learn how to use the (===) operator to check for null values. This test will only pass for null and not for «», undefined, false, 0, or NaN.

Syntax

Following is the syntax to use strict equality operator −

Return values

  • v === null − This parameter checks whether the value stored in variable “v” is equal to null or not. Accordingly, it returns true or false.

Algorithm

  • STEP 1 − We see a variable v is declared and initialized to a null value.
  • STEP 2 − Then we use an if-statement to check if the variable is null and if true an appropriate message is displayed on the user’s screen.

Example

The below example demonstrates how to use the strict equality operator (===) to check for null values in JavaScript.

      

Check for null values using the strict equality operator.

let output = document.getElementById("output"); var v = null; if (v === null) < output.innerHTML = "Value is null"; >else

In the above output, the variable is being checked for null and the value is being executed in the if-block that the variable contains a null value.

Using the Object.is() Function

The Object.is() function in JavaScript that compares two values to see whether they are the same. A boolean value indicates if the two parameters in the function have the same value. Two values could be the same if both the values are null.

Читайте также:  Магазин товаров для дома

Syntax

Following is the syntax for the Object.is() function −

Parameters

  • q − variable to be passed inside the function which is initialized with a null value.
  • null − this parameter is a null value.

Algorithm

  • STEP 1 − We declare a variable called q and set it to null.
  • STEP 2 − Then, given the inputs q and a null value, we check the Object.is() a method with an if-statement to determine whether both are the same.
  • STEP 3 − If they are the same values an appropriate message being displayed on the user’s screen.

Example

In the program below, we use the Object.is() function to check for null values.

     

Check for null values using the JavaScript Object.is() method.

let output = document.getElementById("output"); var q = null; if (Object.is(q, null)) < output.innerHTML = "Value is null"; >else

In the above output, the variable is being checked for null and the value is being printed with a true statement that the variable contains a null value.

Using the typeof Operator

The typeof operator may be used to determine the data type of a JavaScript variable. Here we use the typeof operator with the null operator. The (!variable) means the value is not null and if it is checked with the typeof operator variable which has the data type of an object then the value is null.

Syntax

Following is the syntax to typeof operator −

Here var is the variable whose data type is to be checked and is used as a parameter.

Algorithm

  • STEP 1 − We declare a variable called a set it to null.
  • STEP 2 − Then we check whether the complement of a variable and the variable’s data type is an object or not.
  • STEP 3 − If both conditions match, then the value is deemed equal to null.
  • STEP 4 − An appropriate message is displayed on the user’s screen.

Example

In this example, we use the JavaScript typeof operator to check whether the values of a given variable are equal to null or not.

     

Check for null values using the JavaScript typeOf operator.

let output = document.getElementById("output"); var a = null; if (!a && typeof a === "object") < output.innerHTML = "Value is null"; >else

The variable is tested for null in the above output, and the value is executed in the if-block that the variable has a null value.

Conclusion

In this tutorial, we used three approaches to check null values in JavaScript.

In the first approach, we used the strict equality operator and showed that two null values could be compared.

The second approach is beneficial as it uses the Object.is() function to check and compare two null values.

The third approach is by using the typeof operator which checks the value of the variable and its data type to return whether it is a null value.

All these approaches in the article sum up how to check for null values in JavaScript.

Источник

JS Check for Null – Null Checking in JavaScript Explained

Joel Olawanle

Joel Olawanle

JS Check for Null – Null Checking in JavaScript Explained

Null is a primitive type in JavaScript. This means you are supposed to be able to check if a variable is null with the typeof() method. But unfortunately, this returns “object” because of an historical bug that cannot be fixed.

let userName = null; console.log(typeof(userName)); // object 

So how can you now check for null? This article will teach you how to check for null, along with the difference between the JavaScript type null and undefined.

Null vs Undefined in JavaScript

Null and undefined are very similar in JavaScript and are both primitive types.

A variable has the type of null if it intentionally contains the value of null . In contrast, a variable has the type of undefined when you declare it without initiating a value.

// This is null let firstName = null; // This is undefined let lastName; 

Undefined works well because when you check the type using the typeof() method, it will return undefined :

let lastName; console.log(typeof(lastName)); // undefined 

Let’s now see the two major ways you can check for null and how it relates to undefined .

How to Check for Null in JavaScript with Equality Operators

The equality operators provide the best way to check for null . You can either use the loose/double equality operator ( == ) or the strict/triple equality operator ( === ).

How to use the loose equality operator to check for null

You can use the loose equality operator to check for null values:

let firstName = null; console.log(firstName == null); // true 

But, this can be tricky because if the variable is undefined, it will also return true because both null and undefined are loosely equal.

let firstName = null; let lastName; console.log(firstName == null); // true console.log(lastName == null); // true console.log(firstName == undefined); // true console.log(lastName == undefined); // true console.log(firstName == lastName); // true console.log(null == undefined); // true 

Note: This can be useful when you want to check if a variable has no value because when a variable has no value, it can either be null or undefined .

But suppose you only want to check for null – then you can use the strict equality operator.

How to use the strict equality operator to check for null

The strict equality operator, compared to the loose equality operator, will only return true when you have exactly a null value. Otherwise, it will return false (this includes undefined ).

let firstName = null; let lastName; console.log(firstName === null); // true console.log(lastName === null); // false console.log(firstName === undefined); // false console.log(lastName === undefined); // true console.log(firstName === lastName); // false console.log(null === undefined); // false 

As you can see, it only returns true when a null variable is compared with null , and an undefined variable is compared with undefined .

How to Check for Null in JavaScript with the Object.is() Method

Object.is() is an ES6 method that determines whether two values are the same. This works like the strict equality operator.

// Syntax Object.is(value1, value2) 

Let’s make use of the previous example to see if it works like the strict equality operator:

let firstName = null; let lastName; console.log(Object.is(firstName, null)); // true console.log(Object.is(lastName, null)); // false console.log(Object.is(firstName, undefined)); // false console.log(Object.is(lastName, undefined)); // true console.log(Object.is(firstName, lastName)); // false console.log(Object.is(null, undefined)); // false 

This happens because it only returns true when both values are the same. This means that it will only return true when a variable set to null is compared with null , and an undefined variable is compared with undefined .

Conclusion

Now you know how to check for null with confidence. You can also check whether a variable is set to null or undefined , and you know the difference between the loose and strict equality operators.

I hope this was helpful. Have fun coding!

Источник

How to Check for Null in JavaScript

In JavaScript null parameter is used to declare a variable without assigning a value. On a Web page, these simple null variables are used for declaring objects that can be defined afterwards. JavaScript also offers null as an initial type that contains a “null” value. Also, If the expected object is not created then a function or a variable will return null.

Null values cause problems in the calculation or the manipulation of an object. However, If you declare variables in your code and check for null values before execution, then there exists less chance of encountering errors.

In this post we will learn about how to check for null in JavaScript with the help of a suitable example. So, let’s start!

How to check for null in JavaScript

Now we will implement an example in JavaScript to check out the null value. For this purpose, we will define a function named “info()” that checks the passed argument “arg” for the null value with the help of the “if” statement:

function info ( arg ) {
if ( arg == null ) {
console. log ( ‘Passed argument is null’ ) ;
}
else {
console. log ( ‘Passed argument is not null’ ) ;
}
}

In case, if the value of the passed argument “arg” is null, “if” statement will execute and display the specified message “Passed argument is null”, otherwise the control will move towards the “else” statement.

We will now invoke the “info()” function three times while passing the values: “linuxhint”, “94”, and “null”:

As the passed first two values are not of null type, the message added in the “else” block of the “info()” function will be displayed on the console window. Whereas, when “null” is passed as “arg”, you will see a message stating that “Passed argument is null”:

The above-given output signifies that our created “info()” function is successfully checking for the null values.

Conclusion

In JavaScript, the condition “arg == null” can be used to check passed arguments for null values. In your program, you can create a function named “info()” that accepts any values as an argument. Then add an “if” statement and specify “arg == null” as its condition. The added condition will be executed if the passed value is null otherwise, the execution control will move towards the “else” statement. This post discussed the method to check for null in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.

Источник

Оцените статью