Check empty Object in JavaScript

Check empty Object in JavaScript

Object.keys along with length can be used to check whether the object is empty or not in JavaScript. Please check the following code for reference.

Sample JavaScript:

function emptyObjectCheck( obj ) {

	if ( Object.keys( obj ).length > 0 ) {

		console.log(
			'Object is not empty'
		);
		
	} else {

		console.log(
			'Object is empty'
		);
		
	}

}

let car = {};

emptyObjectCheck( car );

car [ 'make' ] = 'Honda';
car [ 'year' ] = 2000;
car [ 'color' ] = 'white';

emptyObjectCheck( car );

Leave a Reply