Can someone explain to me why this returns an empty string ("") instead of a boolean (false)?
var x = "";
alert(x && x.length > 0);
...While this works as expected, returning true:
var y = "abc";
alert(y && y.length > 0);
I am basically just trying to do a simple shorthand check to see if a value exists in a variable (ensuring it's not undefined, null, or empty string).
I know I can do each test individually (x == null, typeof x == 'undefined', x == '') - I'm just trying to understand why Javascript returns a string on what looks to be a boolean test.
Can someone explain to me why this returns an empty string ("") instead of a boolean (false)?
var x = "";
alert(x && x.length > 0);
...While this works as expected, returning true:
var y = "abc";
alert(y && y.length > 0);
I am basically just trying to do a simple shorthand check to see if a value exists in a variable (ensuring it's not undefined, null, or empty string).
I know I can do each test individually (x == null, typeof x == 'undefined', x == '') - I'm just trying to understand why Javascript returns a string on what looks to be a boolean test.
Share Improve this question edited Mar 28, 2012 at 3:17 alex 491k204 gold badges889 silver badges991 bronze badges asked Mar 28, 2012 at 3:09 kmankman 2,2673 gold badges25 silver badges49 bronze badges 2-
4
note that you can force returning a
boolean
by using!!(x)
– ajax333221 Commented Mar 28, 2012 at 3:20 -
you should be able to shorten with something more like
alert(typeof x != undefined && x.length > 0)
because those both return bools, altho, if ever x is undefined, it may throw error (most likly) because 1st argument might error, would be better to split them up in mini func or inline – SpYk3HH Commented Mar 28, 2012 at 3:57
3 Answers
Reset to default 7When a conditional operator in JavaScript is satisfied, it returns the last value evaluated.
var x = "";
alert(x && x.length > 0);
An empty string is falsey, so when you use just x
in a condition, it will be false. Because you are using &&
, if the LHS is false, then there is no reason to bother checking the RHS. This is short circuit evaluation. Therefore, the last evaluated part, the empty string, is returned to alert()
.
var y = "abc";
alert(y && y.length > 0);
A non empty string is truthy. So the LHS is true, and because it's an &&
, the RHS is evaluated (it needs to be to know if the entire condition is true). The return value of y.length > 0
is true
, so that is passed to your alert()
.
It is returning and empty string because x is already defined, just empty.
This causes the first part of your expression alert(x)
to show an empty string.
If you need to check for a null/empty string, try something like this.
String.isNullOrWhiteSpace = function (str) {
if (typeof str === "string") {
var isNullOrWhiteSpace = false;
// Check for null string
if (str == null || typeof str === "undefined") isNullOrWhiteSpace = true;
// Check for string with whitespace
if (str.replace(/\s/g, '').length < 1) isNullOrWhiteSpace = true;
return isNullOrWhiteSpace;
}
if (typeof str === "undefined" || str == null) {
return true;
}
};
The conditional operations using the &&
(AND operator) will stop when:
- One of the conditions evaluated to
false
- It successfully made it to the end by evaluating everything to
true
The result of the conditional operations will be the last evaluated before stopping (not necessarily a boolean
)
To force returning a real boolean
, you can wrap everything around !!(...)
, example:
alert(typeof !!(...) === "boolean"); //will always be true no matter what conditions are inside
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745325837a4622652.html
评论列表(0条)