2

I need to check whether enter parameter is empty or not like php empty function works.

For example, if I have function with name isEmpty and I passed string as parameter to it then function will return true if string is empty else false and similar for other datatypes like Object, Array and Number.

1
  • did you try something so far ? Commented Jul 26, 2018 at 13:27

4 Answers 4

3

let isEmpty = param => {
	let isAnObject = (obj) => {
		if (obj == null) return false;
		return obj.constructor.name.toLowerCase() === "object"
	}
	if (Array.isArray(param)) {
		return !param.length;
	}
	if (isAnObject(param)) {
		return !Object.keys(param).length;
	}
	return !param;
}
console.log('Is empty Array: ',isEmpty([]));
console.log('Is empty Array: ',isEmpty([1,2,3]));
console.log('Is empty Object: ',isEmpty({}));
console.log('Is empty Object: ',isEmpty({a: 'I am not empty'}));
console.log('Is empty String: ',isEmpty(''));
console.log('Is empty String: ',isEmpty('I am string'));
console.log('Is empty Number: ',isEmpty(NaN));
console.log('Is empty Number: ',isEmpty(100));
console.log('Is empty String parse as number: ',isEmpty(parseInt('')));

I have created a function and named it as isEmpty. This function will return true if passed parameter is empty else false.

Sign up to request clarification or add additional context in comments.

7 Comments

what is this ? part of your question ?
All of those conditions/approaches are actually in Stackoverflow, so this is unnecessary.
php have inbuild empty function that will check the enter parameter is empty or not irrespective of it's datatype but JavaScript do not have such function that will works like php empty function works. But above provided function will works for all datatypes as developer required such type of code that will help or say reduced their work. So I have created this function. Thanks
The first two tests could be replaced with a single == comparison to null or undefined.
In javascript "typeof" function is there,using this you can get which type of data is as parameter.
|
2

Most of the "empty" things are falsy in JS, so it can be simplified to:

const isEmpty = thing => typeof thing === "object" ? !thing || !Object.keys(thing).length : !thing && thing !== 0;

Comments

1

Below solution works for me.

let code = (function(){
    return {
        isEmpty: function(param){
            return typeof param === "object" ? !param || !Object.keys(param).length : !param && param !== 0;
        }
    }
})();

Comments

0

let jqcc = (function(){
	var checkEmptyValues = function(param) {
		if (param == null) {
			return false;
		}
		if (Array.isArray(param)) {
			return !param.length;
		}
		if (param.constructor.name.toLowerCase() === "object") {
			return !Object.keys(param).length;
		}
		return !param;
	}	
	return {
		isEmpty: function(param){
			return checkEmptyValues(param);
		}
	}
})();

console.log(jqcc.isEmpty([]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.