This question already has answers here:
Why if([]) is validated while [] == false in javascript?
(3 answers)
Closed 6 years ago.
I think in every language I know
if(a)
is the same thing as
if(a == true)
Turns out in JavaScript it isn't true, because:
if([])
Seems to act as if the condition is fulfilled, but:
if([] == true)
Does the opposite thing.
I can't really find any possible explanation, especially that this problem doesn't occur with empty string for example (which is == true, but isn't === true, same as empty array). Is this a bug in JavaScript or what?
In JavaScript, there is a concept of truthy and falsey values. if statements test the truthiness or falsiness of a given value rather than strict equality to true or false.
true is obviously truthy. false is obviously falsey. The rest can be a little tricky. MDN has possibly the clearest documentation about which values evaluate to falsey: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
In this case [] is a truthy value so the condition passes and the code is executed.
Related
This question already has answers here:
How does the single equal sign work in the if statement in javascript
(6 answers)
Closed 3 years ago.
This is my general code (in JavaScript):
let x = Math.floor(Math.random()×6)+1);
if (x=1){
do this
} else if (x=2){
do that
} else if.... and so on.
Whenever I test this code in a browser, only actions that could happen in the {do this} section happen. x is being locked as 1, even though it is meant to be a fair 6 sided die. Any ideas why this is happening, and how I can fix it?
The = operator in JavaScript is for assignment. x=1 will always return true because that is an assignment that will never fail. To test for equality use == (equality with conversion) or === (strict equality).
You have your if (x=1) assigning the value of 1 to x, rather than checking if it's equal to it. This results in it always returning TRUE. Using a pair of == (or ===) will solve this.
This question already has answers here:
Falsey values in JavaScript
(6 answers)
Convert truthy or falsy to an explicit boolean, i.e. to True or False
(2 answers)
Closed 3 years ago.
While programming with javascript, I come in some situations that I have a condition an wonder what values will pass that condition (if it will be truthy).
The solution I came up with is open the console in chrome and type if(foo) {true} and if the foo is truthy, it returns true, otherwise false.
An example of that is when I have some expression (foo) that won't return only true/false values. Inside the if it can return alot of things depending on the input (sometimes string, or number or maybe NaN).
But I fell that writing a if for that is too much.
Is there a easiest way of checking the truthy of a value in javascript?
Edit:
I'm looking for the easiest way of checking the truthy of a value, not just how to check and as I said in my question, I already do if(foo) {true} to check, but I'm looking for a easiest way
I would use a ternary operator to shorthand the if-statement.
The result is something like this:
const foo = true;
console.log(foo ? 'truthy' : 'falsy'); // returns truthy
This question already has answers here:
Why do empty JavaScript arrays evaluate to true in conditional structures?
(7 answers)
Closed 5 years ago.
I am going through JavaScript: The Definitive Guide. In it it says
Boolean([]) // => true
But I don't understand the logic behind this. Why is the boolean of an empty array true?
The ECMAScript specification defines how values are cast to booleans, per the abstract ToBoolean operation: https://www.ecma-international.org/ecma-262/6.0/#sec-toboolean
That operations includes a single entry for object input:
Object: Return true.
Thus, when you supply any object to Boolean, including an array (even an empty one), you'll get a true value back,
Array is considered as an object, even if it's empty. That's why the Boolean has a value, means it's true.
Only false, null or undefined are values which will return false.
JavaScript (and other languages) have a concept of 'truthy' and 'falsey' values.
You said you're from a C++ background, so we can make it analogous to something like this in C++:
if (ptr) { }
which is falsey if ptr is null, and truthy otherwise.
It just so happens that in JavaScript, arrays - even empty ones, among many other things - are considered to be truthy.
This question already has answers here:
return !1 in javascript
(3 answers)
Closed 9 years ago.
I've seen a explanation for something similar to b = !b. But I'm not understanding it well enough to translate over to this usage.
What does
var a = !1;
do?
a = !1 is a shorthand way of writing a = false. This is normally used when trying to compress (minify) JavaScript because it saves three bytes.
If you're seeing this in ordinary un-minified JS, then someone is probably being either lazy or obfuscatory.
Run this in chrome dev tools and see what you get.
a evaluates to false because 1 is a truthy value in javascript and therefore negating it produces false
Maybe read this http://james.padolsey.com/javascript/truthy-falsey/ . It's quite interesting :)
In general the ! will invert the boolean value of its operand.
So !a will be true if a is false or it will be false if a is true.
Hope that helps :)
! is a not operator. Therefore ! true is equal to false. It's result will be either true or false
All values in JavaScript are either "truthy" or "falsy". This describes their interpretation in contexts where a boolean (true or false) is expected.
Examples of "truthy" values: true, 1, [], {}, "text"
Examples of "falsy" values: false, 0, ""
!1 is a negation of a truthy value, which will evalute to false. b = !b is a toggler, it will change the value from a truthy to a falsy, and vice versa.
The ! operator is known as the Logical NOT operator.
In short, it returns false if the following value is 'truthy', and true otherwise.
Since 1 is 'truthy', your example, !1 reads NOT 1, which will return false.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the !! operator in JavaScript?
What is the difference between these two operators? Does !! have special meaning, or does it simply mean you are doing two '!' operations. I know there are "Truth" and "Truthy" concepts in Javascript, but I'm not sure if !! is meant for "Truth"
!! is just double !
!true // -> false
!!true // -> true
!! is a common way to cast something to boolean value
!!{} // -> true
!!null // -> false
Writing !! is a common way of converting a "truthy" or "falsey" variable into a genuine boolean value.
For example:
var foo = null;
if (!!foo === true) {
// Code if foo was "truthy"
}
After the first ! is applied to foo, the value returned is true. Notting that value again makes it false, meaning the code inside the if block is not entered.