javascript backtick and jquery not working [duplicate] - html
How do I determine if variable is undefined or null?
My code is as follows:
var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
// DO SOMETHING
};
<div id="esd-names">
<div id="name"></div>
</div>
But if I do this, the JavaScript interpreter halts execution.
You can use the qualities of the abstract equality operator to do this:
if (variable == null){
// your code here.
}
Because null == undefined is true, the above code will catch both null and undefined.
The standard way to catch null and undefined simultaneously is this:
if (variable == null) {
// do something
}
--which is 100% equivalent to the more explicit but less concise:
if (variable === undefined || variable === null) {
// do something
}
When writing professional JS, it's taken for granted that type equality and the behavior of == vs === is understood. Therefore we use == and only compare to null.
Edit again
The comments suggesting the use of typeof are simply wrong. Yes, my solution above will cause a ReferenceError if the variable doesn't exist. This is a good thing. This ReferenceError is desirable: it will help you find your mistakes and fix them before you ship your code, just like compiler errors would in other languages. Use try/catch if you are working with input you don't have control over.
You should not have any references to undeclared variables in your code.
Combining the above answers, it seems the most complete answer would be:
if( typeof variable === 'undefined' || variable === null ){
// Do stuff
}
This should work for any variable that is either undeclared or declared and explicitly set to null or undefined. The boolean expression should evaluate to false for any declared variable that has an actual non-null value.
if (variable == null) {
// Do stuff, will only match null or undefined, this won't match false
}
if (typeof EmpName != 'undefined' && EmpName) {
will evaluate to true if value is not:
null
undefined
NaN
empty string ("")
0
false
Probably the shortest way to do this is:
if(EmpName == null) { /* DO SOMETHING */ };
Here is proof:
function check(EmpName) {
if(EmpName == null) { return true; };
return false;
}
var log = (t,a) => console.log(`${t} -> ${check(a)}`);
log('null', null);
log('undefined', undefined);
log('NaN', NaN);
log('""', "");
log('{}', {});
log('[]', []);
log('[1]', [1]);
log('[0]', [0]);
log('[[]]', [[]]);
log('true', true);
log('false', false);
log('"true"', "true");
log('"false"', "false");
log('Infinity', Infinity);
log('-Infinity', -Infinity);
log('1', 1);
log('0', 0);
log('-1', -1);
log('"1"', "1");
log('"0"', "0");
log('"-1"', "-1");
// "void 0" case
console.log('---\n"true" is:', true);
console.log('"void 0" is:', void 0);
log(void 0,void 0); // "void 0" is "undefined"
And here are more details about == (source here)
BONUS: reason why === is more clear than == (look on agc answer)
jQuery attr() function returns either a blank string or the actual value (and never null or undefined). The only time it returns undefined is when your selector didn't return any element.
So you may want to test against a blank string. Alternatively, since blank strings, null and undefined are false-y, you can just do this:
if (!EmpName) { //do something }
Edited answer: In my opinion, you shouldn't use the function from my below old answer. Instead, you should probably know the type of your variable and use the according to check directly (for example, wondering if an array is empty? just do if(arr.length===0){} etc.). This answer doesn't even answer OP's question.
I've come to write my own function for this. JavaScript is weird.
It is usable on literally anything. (Note that this also checks if the variable contains any usable values. But since this information is usually also needed, I think it's worth posting). Please consider leaving a note.
function empty(v) {
let type = typeof v;
if (type === 'undefined') {
return true;
}
if (type === 'boolean') {
return !v;
}
if (v === null) {
return true;
}
if (v === undefined) {
return true;
}
if (v instanceof Array) {
if (v.length < 1) {
return true;
}
} else if (type === 'string') {
if (v.length < 1) {
return true;
}
if (v === '0') {
return true;
}
} else if (type === 'object') {
if (Object.keys(v).length < 1) {
return true;
}
} else if (type === 'number') {
if (v === 0) {
return true;
}
}
return false;
}
TypeScript-compatible.
This function should do exactly the same thing like PHP's empty() function (see RETURN VALUES)
Considers undefined, null, false, 0, 0.0, "0" {}, [] as empty.
"0.0", NaN, " ", true are considered non-empty.
The shortest and easiest:
if(!EmpName ){
// DO SOMETHING
}
this will evaluate true if EmpName is:
null
undefined
NaN
empty
string ("")
0
false
If the variable you want to check is a global, do
if (window.yourVarName) {
// Your code here
}
This way to check will not throw an error even if the yourVarName variable doesn't exist.
Example: I want to know if my browser supports History API
if (window.history) {
history.back();
}
How this works:
window is an object which holds all global variables as its properties, and in JavaScript it is legal to try to access a non-existing object property. If history doesn't exist then window.history returns undefined. undefined is falsey, so code in an if(undefined){} block won't run.
In JavaScript, as per my knowledge, we can check an undefined, null or empty variable like below.
if (variable === undefined){
}
if (variable === null){
}
if (variable === ''){
}
Check all conditions:
if(variable === undefined || variable === null || variable === ''){
}
Since you are using jQuery, you can determine whether a variable is undefined or its value is null by using a single function.
var s; // undefined
jQuery.isEmptyObject(s); // will return true;
s = null; // defined as null
jQuery.isEmptyObject(s); // will return true;
// usage
if(jQuery.isEmptyObject(s)){
alert('Either variable: s is undefined or its value is null');
}else{
alert('variable: s has value ' + s);
}
s = 'something'; // defined with some value
jQuery.isEmptyObject(s); // will return false;
I've just had this problem i.e. checking if an object is null.
I simply use this:
if (object) {
// Your code
}
For example:
if (document.getElementById("enterJob")) {
document.getElementById("enterJob").className += ' current';
}
You can simply use the following (I know there are shorter ways to do this, but this may make it easier to visually observe, at least for others looking at the code).
if (x === null || x === undefined) {
// Add your response code here, etc.
}
source: https://www.growthsnippets.com/how-can-i-determine-if-a-variable-is-undefined-or-null/
jQuery check element not null:
var dvElement = $('#dvElement');
if (dvElement.length > 0) {
// Do something
}
else{
// Else do something else
}
With the newest javascript changes, you can use the new logical operator ??= to check if the left operand is null or undefined and if so assign the value of right operand.
SO,
if(EmpName == null){ // if Variable EmpName null or undefined
EmpName = 'some value';
};
Is equivalent to:
EmpName ??= 'some value';
The easiest way to check is:
if(!variable) {
// If the variable is null or undefined then execution of code will enter here.
}
I run this test in the Chrome console. Using (void 0) you can check undefined:
var c;
undefined
if (c === void 0) alert();
// output = undefined
var c = 1;
// output = undefined
if (c === void 0) alert();
// output = undefined
// check c value c
// output = 1
if (c === void 0) alert();
// output = undefined
c = undefined;
// output = undefined
if (c === void 0) alert();
// output = undefined
With the solution below:
const getType = (val) => typeof val === 'undefined' || !val ? null : typeof val;
const isDeepEqual = (a, b) => getType(a) === getType(b);
console.log(isDeepEqual(1, 1)); // true
console.log(isDeepEqual(null, null)); // true
console.log(isDeepEqual([], [])); // true
console.log(isDeepEqual(1, "1")); // false
etc...
I'm able to check for the following:
null
undefined
NaN
empty
string ("")
0
false
To test if a variable is null or undefined I use the below code.
if(typeof sVal === 'undefined' || sVal === null || sVal === ''){
console.log('variable is undefined or null');
}
if you create a function to check it:
export function isEmpty (v) {
if (typeof v === "undefined") {
return true;
}
if (v === null) {
return true;
}
if (typeof v === "object" && Object.keys(v).length === 0) {
return true;
}
if (Array.isArray(v) && v.length === 0) {
return true;
}
if (typeof v === "string" && v.trim().length === 0) {
return true;
}
return false;
}
(null == undefined) // true
(null === undefined) // false
Because === checks for both the type and value. Type of both are different but value is the same.
Let's look at this,
let apple; // Only declare the variable as apple
alert(apple); // undefined
In the above, the variable is only declared as apple. In this case, if we call method alert it will display undefined.
let apple = null; /* Declare the variable as apple and initialized but the value is null */
alert(apple); // null
In the second one it displays null, because variable of apple value is null.
So you can check whether a value is undefined or null.
if(apple !== undefined || apple !== null) {
// Can use variable without any error
}
The foo == null check should do the trick and resolve the "undefined OR null" case in the shortest manner. (Not considering "foo is not declared" case.) But people who are used to have 3 equals (as the best practice) might not accept it. Just look at eqeqeq or triple-equals rules in eslint and tslint...
The explicit approach, when we are checking if a variable is undefined or null separately, should be applied in this case, and my contribution to the topic (27 non-negative answers for now!) is to use void 0 as both short and safe way to perform check for undefined.
Using foo === undefined is not safe because undefined is not a reserved word and can be shadowed (MDN). Using typeof === 'undefined' check is safe, but if we are not going to care about foo-is-undeclared case the following approach can be used:
if (foo === void 0 || foo === null) { ... }
You can do something like this, I think its more efficient for multiple value check on the same variable in one condition
const x = undefined;
const y = null;
const z = 'test';
if ([undefined, null].includes(x)) {
// Will return true
}
if ([undefined, null].includes(y)) {
// Will return true
}
if ([undefined, null].includes(z)) {
// Will return false
}
No one seems to have to posted this yet, so here we go:
a?.valueOf() === undefined works reliably for either null or undefined.
The following works pretty much like a == null or a == undefined, but it could be more attractive for purists who don't like == 😎
function check(a) {
const value = a?.valueOf();
if (value === undefined) {
console.log("a is null or undefined");
}
else {
console.log(value);
}
}
check(null);
check(undefined);
check(0);
check("");
check({});
check([]);
On a side note, a?.constructor works too:
function check(a) {
if (a?.constructor === undefined) {
console.log("a is null or undefined");
}
}
check(null);
check(undefined);
check(0);
check("");
check({});
check([]);
Calling typeof null returns a value of “object”, as the special value null is considered to be an empty object reference. Safari through version 5 and Chrome through version 7 have a quirk where calling typeof on a regular expression returns “function” while all other browsers return “object”.
var x;
if (x === undefined) {
alert ("only declared, but not defined.")
};
if (typeof y === "undefined") {
alert ("not even declared.")
};
You can only use second one: as it will check for both definition and declaration
var i;
if (i === null || typeof i === 'undefined') {
console.log(i, 'i is undefined or null')
}
else {
console.log(i, 'i has some value')
}
I still think the best/safe way to test these two conditions is to cast the value to a string:
var EmpName = $("div#esd-names div#name").attr('class');
// Undefined check
if (Object.prototype.toString.call(EmpName) === '[object Undefined]'){
// Do something with your code
}
// Nullcheck
if (Object.prototype.toString.call(EmpName) === '[object Null]'){
// Do something with your code
}
Related
AngularJS Selects Empty Option Even Valid Option is Avaliable
I'm using AngularJS ver. 1.2.15 on my project. And, I have a select element on one of my views as per below: <select class="select-white form-control form-select" id="cat2_{{feed.id}}" ng-model="feed.operationstatusid" ng-change="updateCategoryAndStatus(feed, true)"></select> And, I'm feeding this element like this: function SetCategory2(cat1Id, feed) { var feedId = feed.id; var fromRuleOpStatusId = -1; $('#cat2_' + feedId).find('option').remove(); if (cat1Id > -1) { $('#cat2_' + feedId).append($('<option></option>').text(lang.SelectSubCategory).val(0)); $.each($scope.category2, function (index, cat2Item) { $('#cat2_' + feedId).append($('<option></option>').text(cat2Item.statusdescription).val(cat2Item.id)); }); var isselected = false; $.each($scope.category2, function (index, cat2Item) { if (feed.operationstatusid == cat2Item.id) { $('#cat2_' + feedId).val(cat2Item.id); fromRuleOpStatusId = -1; isselected = true; } else { var feedStr = ""; if (feed.title != undefined && feed.title != null) { feedStr = feed.title.toLowerCase(); } if ($scope.catTitleRulesTwo) { $.each($scope.catTitleRulesTwo, function (r_index, r_item) { if (cat2Item.id == r_item.titleCode && !isselected) { if (feedStr != undefined && feedStr != null && r_item != undefined && r_item != null) { String.prototype.contains = function (str) { return this.toLowerCase().indexOf(str) !== -1; }; var text = feedStr; if (eval(r_item.ruleexpression)) { $('#cat2_' + feedId).val(cat2Item.id); fromRuleOpStatusId = cat2Item.id; isselected = true; } } } }); } } }); if (fromRuleOpStatusId != -1) { feed.operationstatusid = fromRuleOpStatusId; } } else { $('#cat2_' + feedId).append($('<option></option>').text(lang.SelectSubCategory).val(0)); } } I am aware of the facts about eval function, but the project I'm working on is quite old, so does the code. Anyway, this is about business logic and quite irrelevant with the thing I'm going to ask (or so I was thinking). As you can see I'm appending all the options before I set the value of the selectbox with using .val(...). I have also checked that values do match along with the data types. But, when I observe this function step by step, I saw that selected value does show up without flaw. After the code finish with my above mentioned function (SetCategory2), code goes through on of the function located on AngularJS file, named xhr.onreadystatechange. It's not a long function, so I'm sharing it also on below. xhr.onreadystatechange = function() { if (xhr && xhr.readyState == 4) { var responseHeaders = null, response = null; if(status !== ABORTED) { responseHeaders = xhr.getAllResponseHeaders(); response = ('response' in xhr) ? xhr.response : xhr.responseText; } completeRequest(callback, status || xhr.status, response, responseHeaders); } }; After the code released from this function, respective selectbox's value is pointed at the empty option. I have run into topics which talks about this behaviour might due to invalid option-value match, but as I described above, I append all my options before deciding the value. So, I can't figure out what I'm missing. Thank you in advance.
Q. Check if 2 arrays have the same values in the same order
I have looked at other posts that supposedly have solved this issue, but this method still doesn't work for me. I had run into an error in a larger program I was writing but I narrowed the error to this method. I set a cell to =isMatch( {1,2,3} , {1,2,3} ) to verify my method works. The cell computes to False, and I don't know why or how to fix it. Before I checked stackoverflow, I had originally written code identical to the answer of this post. Here is the code I currently have. function isMatch(arr1,arr2){//Returns True if same Array values in same location if(arr1.length !== arr2.length) return false; for(var i =0; i<arr1.length; i++) { if(arr1[i] !== arr2[i]) return false; } return true; }
You're comparing a 2D array. {1,2,3} === [[1,2,3]] and not [1,2,3]. To compare a n dimensional array, you can recurse: function isMatch(arr1, arr2) { if (typeof arr1 !== typeof arr2) throw new TypeError('Arrays or elements not of same type!'); if (Array.isArray(arr1)) return ( arr1.length === arr2.length && arr1.every((e, i) => isMatch(e, arr2[i])) ); return arr1 === arr2; } console.info(isMatch([[1], [2]], [[1], [2]])); console.info(isMatch([[1, 2]], [[1, 2]])); console.info(isMatch([[1, 2]], 1)); 2D array Array#every
Is there a way in Postman to change xml2Json conversion rules?
I have some instances where I need to convert my XML response, and in multiple cases I have nodes that appear once or more times. If it looks like this bbb will be just another element: <aaa> <bbb>ccc</bbb> </aaa> and to get ccc value I have to access json like this: pm.response.json().aaa.bbb But if XML looks something like this bbb will become array: <aaa> <bbb>ccc</bbb> <bbb>ddd</bbb> </aaa> and to get ccc value I have to access json like this: pm.response.json().aaa.bbb[0] Later on, when I use JSON data I need to always check whether I have one or multiple elements returned from provider. What I want is to avoid checking Array.isArray... all the time and force it to be array. Is there is such option?
I have created this group of functions, I hope they can help you (I think that transformIntoSingleElement is what you are looking for): function stackTrace() { var err = new Error(); console.log(err.stack); return err; } function transformIntoArray(element, mandatory){ mandatory = (mandatory!==undefined)?mandatory:true; if(element === undefined && mandatory){ stackTrace(); return element; } if(element === undefined) return []; if(element.constructor.name != "Array") return [element]; return element; } function transformIntoSingleElement(element, index, mandatory){ mandatory = (mandatory!==undefined)?mandatory:true; if(element === undefined && mandatory){ stackTrace(); return element; } if(element === undefined){ return element; } if(element.constructor.name == "Array"){ if(element.length >= index) return element[index]; return element[0]; } return element; } function getElementValue(element, dflt, mandatory){ dflt = dflt || ""; mandatory = (mandatory!==undefined)?mandatory:true; if(element === undefined && mandatory){ stackTrace(); return element; } if(element === undefined) return dflt; if(element._ === undefined) return element; return element._; } function getAttributeValue(element, attribute, dflt, mandatory){ dflt = dflt || ""; mandatory = (mandatory!==undefined)?mandatory:true; if(element === undefined && mandatory){ stackTrace(); return element; } if(element.$ !== undefined && element.$[attribute] !== undefined && (element.$[attribute] !== "" || (element.$[attribute] === "" && mandatory))){ return element.$[attribute]; } return dflt; }
An empty array shows up non-empty when returned from function [duplicate]
This question already has answers here: How do I test for an empty JavaScript object? (48 answers) Closed 5 years ago. What is the fastest way to check if an object is empty or not? Is there a faster and better way than this: function count_obj(obj){ var i = 0; for(var key in obj){ ++i; } return i; }
For ECMAScript5 (not supported in all browsers yet though), you can use: Object.keys(obj).length === 0
I'm assuming that by empty you mean "has no properties of its own". // Speed up calls to hasOwnProperty var hasOwnProperty = Object.prototype.hasOwnProperty; function isEmpty(obj) { // null and undefined are "empty" if (obj == null) return true; // Assume if it has a length property with a non-zero value // that that property is correct. if (obj.length > 0) return false; if (obj.length === 0) return true; // If it isn't an object at this point // it is empty, but it can't be anything *but* empty // Is it empty? Depends on your application. if (typeof obj !== "object") return true; // Otherwise, does it have any properties of its own? // Note that this doesn't handle // toString and valueOf enumeration bugs in IE < 9 for (var key in obj) { if (hasOwnProperty.call(obj, key)) return false; } return true; } Examples: isEmpty(""), // true isEmpty(33), // true (arguably could be a TypeError) isEmpty([]), // true isEmpty({}), // true isEmpty({length: 0, custom_property: []}), // true isEmpty("Hello"), // false isEmpty([1,2,3]), // false isEmpty({test: 1}), // false isEmpty({length: 3, custom_property: [1,2,3]}) // false If you only need to handle ECMAScript5 browsers, you can use Object.getOwnPropertyNames instead of the hasOwnProperty loop: if (Object.getOwnPropertyNames(obj).length > 0) return false; This will ensure that even if the object only has non-enumerable properties isEmpty will still give you the correct results.
EDIT: Note that you should probably use ES5 solution instead of this since ES5 support is widespread these days. It still works for jQuery though. Easy and cross-browser way is by using jQuery.isEmptyObject: if ($.isEmptyObject(obj)) { // do something } More: http://api.jquery.com/jQuery.isEmptyObject/ You need jquery though.
Underscore and lodash each have a convenient isEmpty() function, if you don't mind adding an extra library. _.isEmpty({});
Lets put this baby to bed; tested under Node, Chrome, Firefox and IE 9, it becomes evident that for most use cases: (for...in...) is the fastest option to use! Object.keys(obj).length is 10 times slower for empty objects JSON.stringify(obj).length is always the slowest (not surprising) Object.getOwnPropertyNames(obj).length takes longer than Object.keys(obj).length can be much longer on some systems. Bottom line performance wise, use: function isEmpty(obj) { for (var x in obj) { return false; } return true; } or function isEmpty(obj) { for (var x in obj) { if (obj.hasOwnProperty(x)) return false; } return true; } Results under Node: first result: return (Object.keys(obj).length === 0) second result: for (var x in obj) { return false; }... third result: for (var x in obj) { if (obj.hasOwnProperty(x)) return false; }... forth result: return ('{}' === JSON.stringify(obj)) Testing for Object with 0 keys 0.00018 0.000015 0.000015 0.000324 Testing for Object with 1 keys 0.000346 0.000458 0.000577 0.000657 Testing for Object with 2 keys 0.000375 0.00046 0.000565 0.000773 Testing for Object with 3 keys 0.000406 0.000476 0.000577 0.000904 Testing for Object with 4 keys 0.000435 0.000487 0.000589 0.001031 Testing for Object with 5 keys 0.000465 0.000501 0.000604 0.001148 Testing for Object with 6 keys 0.000492 0.000511 0.000618 0.001269 Testing for Object with 7 keys 0.000528 0.000527 0.000637 0.00138 Testing for Object with 8 keys 0.000565 0.000538 0.000647 0.00159 Testing for Object with 100 keys 0.003718 0.00243 0.002535 0.01381 Testing for Object with 1000 keys 0.0337 0.0193 0.0194 0.1337 Note that if your typical use case tests a non empty object with few keys, and rarely do you get to test empty objects or objects with 10 or more keys, consider the Object.keys(obj).length option. - otherwise go with the more generic (for... in...) implementation. Note that Firefox seem to have a faster support for Object.keys(obj).length and Object.getOwnPropertyNames(obj).length, making it a better choice for any non empty Object, but still when it comes to empty objects, the (for...in...) is simply 10 times faster. My 2 cents is that Object.keys(obj).length is a poor idea since it creates an object of keys just to count how many keys are inside, than destroys it! In order to create that object he needs to loop overt the keys... so why use it and not the (for... in...) option :) var a = {}; function timeit(func,count) { if (!count) count = 100000; var start = Date.now(); for (i=0;i<count;i++) func(); var end = Date.now(); var duration = end - start; console.log(duration/count) } function isEmpty1() { return (Object.keys(a).length === 0) } function isEmpty2() { for (x in a) { return false; } return true; } function isEmpty3() { for (x in a) { if (a.hasOwnProperty(x)) return false; } return true; } function isEmpty4() { return ('{}' === JSON.stringify(a)) } for (var j=0;j<10;j++) { a = {} for (var i=0;i<j;i++) a[i] = i; console.log('Testing for Object with '+Object.keys(a).length+' keys') timeit(isEmpty1); timeit(isEmpty2); timeit(isEmpty3); timeit(isEmpty4); } a = {} for (var i=0;i<100;i++) a[i] = i; console.log('Testing for Object with '+Object.keys(a).length+' keys') timeit(isEmpty1); timeit(isEmpty2); timeit(isEmpty3); timeit(isEmpty4, 10000); a = {} for (var i=0;i<1000;i++) a[i] = i; console.log('Testing for Object with '+Object.keys(a).length+' keys') timeit(isEmpty1,10000); timeit(isEmpty2,10000); timeit(isEmpty3,10000); timeit(isEmpty4,10000);
Elegant way - use keys var myEmptyObj = {}; var myFullObj = {"key":"value"}; console.log(Object.keys(myEmptyObj).length); //0 console.log(Object.keys(myFullObj).length); //1 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
function isEmpty( o ) { for ( var p in o ) { if ( o.hasOwnProperty( p ) ) { return false; } } return true; }
var x= {} var y= {x:'hi'} console.log(Object.keys(x).length===0) console.log(Object.keys(y).length===0) true false http://jsfiddle.net/j7ona6hz/1/
Surprised to see so many weak answers on such a basic JS question... The top answer is no good too for these reasons: it generates a global variable returns true on undefined uses for...in which is extremely slow by itself function inside for...in is useless - return false without hasOwnProperty magic will work fine In fact there's a simpler solution: function isEmpty(value) { return Boolean(value && typeof value === 'object') && !Object.keys(value).length; }
https://lodash.com/docs#isEmpty comes in pretty handy: _.isEmpty({}) // true _.isEmpty() // true _.isEmpty(null) // true _.isEmpty("") // true
How bad is this? function(obj){ for(var key in obj){ return false; // not empty } return true; // empty }
No need for a library. function(){ //must be within a function var obj = {}; //the object to test for(var isNotEmpty in obj) //will loop through once if there is a property of some sort, then return alert('not empty')//what ever you are trying to do once return alert('empty'); //nope obj was empty do this instead; }
It might be a bit hacky. You can try this. if (JSON.stringify(data).length === 2) { // Do something } Not sure if there is any disadvantage of this method.
fast onliner for 'dictionary'-objects: function isEmptyDict(d){for (var k in d) return false; return true}
You can write a fallback if Array.isArray and Object.getOwnPropertyNames is not available XX.isEmpty = function(a){ if(Array.isArray(a)){ return (a.length==0); } if(!a){ return true; } if(a instanceof Object){ if(a instanceof Date){ return false; } if(Object.getOwnPropertyNames(a).length == 0){ return true; } } return false; }
Imagine you have the objects below: var obj1= {}; var obj2= {test: "test"}; Don't forget we can NOT use === sign for testing an object equality as they get inheritance, so If you using ECMA 5 and upper version of javascript, the answer is easy, you can use the function below: function isEmpty(obj) { //check if it's an Obj first var isObj = obj !== null && typeof obj === 'object' && Object.prototype.toString.call(obj) === '[object Object]'; if (isObj) { for (var o in obj) { if (obj.hasOwnProperty(o)) { return false; break; } } return true; } else { console.error("isEmpty function only accept an Object"); } } so the result as below: isEmpty(obj1); //this returns true isEmpty(obj2); //this returns false isEmpty([]); // log in console: isEmpty function only accept an Object
funtion isEmpty(o,i) { for(i in o) { return!1 } return!0 }
here's a good way to do it function isEmpty(obj) { if (Array.isArray(obj)) { return obj.length === 0; } else if (typeof obj === 'object') { for (var i in obj) { return false; } return true; } else { return !obj; } }
var hasOwnProperty = Object.prototype.hasOwnProperty; function isArray(a) { return Object.prototype.toString.call(a) === '[object Array]' } function isObject(a) { return Object.prototype.toString.call(a) === '[object Object]' } function isEmpty(a) { if (null == a || "" == a)return!0; if ("number" == typeof a || "string" == typeof a)return!1; var b = !0; if (isArray(a)) { if (!a.length)return!0; for (var c = 0; c < a.length; c++)isEmpty(a[c]) || (b = !1); return b } if (isObject(a)) { for (var d in a)hasOwnProperty.call(a, d) && (isEmpty(a[d]) || (b = !1)); return b } return!0 }
May be you can use this decision: var isEmpty = function(obj) { for (var key in obj) if(obj.hasOwnProperty(key)) return false; return true; }
I modified Sean Vieira's code to suit my needs. null and undefined don't count as object at all, and numbers, boolean values and empty strings return false. 'use strict'; // Speed up calls to hasOwnProperty var hasOwnProperty = Object.prototype.hasOwnProperty; var isObjectEmpty = function(obj) { // null and undefined are not empty if (obj == null) return false; if(obj === false) return false; if(obj === true) return false; if(obj === "") return false; if(typeof obj === "number") { return false; } // Assume if it has a length property with a non-zero value // that that property is correct. if (obj.length > 0) return false; if (obj.length === 0) return true; // Otherwise, does it have any properties of its own? // Note that this doesn't handle // toString and valueOf enumeration bugs in IE < 9 for (var key in obj) { if (hasOwnProperty.call(obj, key)) return false; } return true; }; exports.isObjectEmpty = isObjectEmpty;
here my solution function isEmpty(value) { if(Object.prototype.toString.call(value) === '[object Array]') { return value.length == 0; } else if(value != null && typeof value === 'object') { return Object.getOwnPropertyNames(value).length == 0; } else { return !(value || (value === 0)); } } Chears
if (Object.getOwnPropertyNames(obj1).length > 0) { alert('obj1 is empty!'); }
if localStorage key value doesn't exist
I am trying to hide my div if there is no a localStorage key value. With the line below I achieved only to hide a div when localStorage key completely deleted but need to do the same if localStorage key hasn't got any value at all just []. window.localStorage.getItem('items') === null How would it be performed?
You can add required conditions using the OR operator || var items = window.localStorage.getItem('items') if (items === null || items.length === 0) { // items is null, [] or '' (empty string) } If you have to check for undefined somewhere as well you can change === null to == null or expand with an extra condition like this if (items === undefined || items === null || items.length === 0) { // items is undefined, null, [] or '' (empty string) } EDIT: Here is what you can do to get the array directly var items = JSON.parse(window.localStorage.getItem('items')) if (items === null || items.length === 0) { // items is null or [] }
How about simply: if (!localStorage.nameOfYourLocalStorage) { // do the following if the localStorage.nameOfYourLocalStorage does not exist } An example of how it could be useful: if (!localStorage.nameOfYourLocalStorage) { localStorage.nameOfYourLocalStorage = defaultValue; } Here the script will check if the localStorage name does not exist, and if it doesn't, it will create it with the default value. And if you want it to act when it does exist, you can add an else after the if block to continue, or remove the '!' from the if block condition.
Storage.prototype.has = function(key) { return (key in this && !(key in Storage.prototype)); };