Warning: session_start(): open(/tmp/sess_fa998aa6972c6b48d49c6e49f155fd3d, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check If A Variable Is Null or Undefined In JavaScript - LearnShareIT

How To Check If A Variable Is Null or Undefined In JavaScript

check if a variable is null or undefined in JavaScript

There are some ways to check if a variable is null or undefined in JavaScript. Let’s learn about it with the explanation and examples below.

Check If Variable Is Null or Undefined In JavaScript

What are variables in JavaScript?

As many other languages, JavaScript also has variables. Variable means anything that can vary. A variable is a container to store data value.

The keywords var or let or const are used to declare a variable in JavaScript.

Look at the example below to learn more about variables in JavaScript.

var num = 10 
var str = "Learn share IT"
var fl = 10.5
var x = true
var y

let num1 = 10
let str = "Learn to share IT"
let x1 = true;
const fl = 10.5

What are null value and undefined value?

The value null represents the intentional absence of any object value, and it is a primitive value.

Look at the example below.

var x= null // value is a null value
console.log(x)

Output

null

Undefined is a primitive value in JavaScript. 

A variable that has not been assigned a value is of type undefined. 

Look at the example below.

var x // variable x is a undefined value
console.log(x)

Output

undefined

What is the difference between null value and undefined value?

We often get confused about what the difference between UNDEFINED value and NULL value is.

Null and Undefined are also primitive values. But there are many differences between them.

Null represents a missing object, while an undefined value means lack of value.
We can assign the value null to the variable when you want it to be empty. Undefined is a variable that value did not have before.

Look at the example below to know more about it.

var a = null // variable a is null value
var b // variable b is undefined value
console.log(a)
console.log(b)

Output

null
undefined

Solution 1: Using equality operator

We can use == operator to check if a variable is null or undefined.

Syntax

variable_check == null / undefined

Parameters

  • variable_check: The variable you want to check.

Return Value: true or false

Look at the example below to learn about it.

function check(variable_check) {
  // check if value is null or not.
	if (variable_check == null ) {
		console.log("The variable is null");		
	}
	else if (variable_check == undefined) {
    // check if value is undefined or not.
		console.log("The variable is undefined");		
	}
	else {
		console.log("The variable is not null and not undefined");
	}
}

var a = null
var b 
var c = 10
var d = 0.5
var e = true 
var f = undefined

check(a)
check(b)
check(c)
check(d)
check(e)
check(f)

Output

The variable is null
The variable is null
The variable is not null and not undefined
The variable is not null and not undefined
The variable is not null and not undefined
The variable is null

Solution 2: Using typeof operator

Syntax

typeof variable_check ==’null’ / ‘undefined’

Parameters

  • variable_check: The variable you want to check.

Return Value: true or false.

Look at the example below to know more about this solution.

function check(variable_check) {
  // check if value is null or not.
	if (typeof variable_check == 'null' ) {
		console.log("The variable is null");		
	}
	else if (typeof variable_check == 'undefined') {
    // check if value is undefined or not.
		console.log("The variable is undefined");		
	}
	else {
		console.log("The variable is not null and not undefined");
	}
}

var a = null
var b 
var c = 10
var d = 0.5
var e = true 
var f = undefined

check(a)
check(b)
check(c)
check(d)
check(e)
check(f)

Output

The variable is null
The variable is null
The variable is not null and not undefined
The variable is not null and not undefined
The variable is not null and not undefined
The variable is null

Summary

You can use the equality operator or typeof operator to check if a variable is null or undefined in JavaScript. We hope this guide is helpful to you. Thanks!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *