JavaScript - remove empty values from object

0 points
Created by:

In this article, we would like to show you how to remove empty values from object in JavaScript.

Quick solution:

delete objectName[propertyName];


Practical example

In this example, we use delete operator inside for loop to check each object property and delete it if it's empty.

// ONLINE-RUNNER:browser;

var student = {
  name: 'John',
  age: undefined,
  city: null,
  hobby: ''

function removeEmptyValues(obj) {
  for (var property in obj) {
    if (obj[property] === null || obj[property] === undefined || obj[property] === '') {
      delete obj[property];
  return obj

// before
console.log(JSON.stringify(student));  // {"name":"John","city":null,"hobby":""}
const result = removeEmptyValues(student);

// after
console.log(JSON.stringify(result));  // {"name":"John"}


  1. delete operator - JavaScript | MDN 
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂