Why does `0 in [1]` evaluate to `true`? [duplicate] - javascript

This question already has answers here:
JavaScript 'in' operator for `undefined` elements in Arrays
(3 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 8 years ago.
Apparently 0 in [1] is not the right way to find out whether the array [1] contains the item 0. Is there a JS compatible way to do this without iterating the collection?

You'd think 0 in [3,1] would evaluate to false. But it evaluates to true. I think this is because null, false and zero are sometimes treated as equivalent.
No, you have a misunderstanding of what in does. It tests whether the object has a property with that name. Arrays are just objects with numeric properties:
> console.dir([3,1]);
Array[2]
0: 3
1: 1
length: 2
The array has an element at index 0, hence the test is true. 'length' in [3,1] returns true as well.
null, false and zero are sometimes treated as equivalent
null, false and 0 are all "falsy" values, i.e. they become to false when converted to a Boolean. This is consistent with other dynamically typed languages, such as PHP or Python.
JavaScript can have surprising behavior, especially when it comes to type conversion, but it is deterministic. It's recommended to simply avoid type conversion if possible.

Related

Searching for a specific array in an array of arrays [duplicate]

This question already has answers here:
Javascript indexOf for an array of arrays not finding array
(6 answers)
Closed 1 year ago.
const a = [[1,1], [2,1], [3,1]];
console.log(a);
console.log(a.includes([1,1]));
> false
Can someone explain to me why the above outputs false? For what its worth, this occurs in any of the search functions (.find, .indexof, etc), as well as if I try [1,1] in a. I'm clearly missing something about how multidimensional array searching works in javascript.
This is basically how equal to operator works in JavaScript.
If we try to compare two arrays
[1]=== [1] // false
equality operator can compare primitive values(number,string Boolean). But in case of arrays and objects it compares for reference. So here we are comparing two different references so it is coming as false.
For same reason you are getting false. You have to write customer logic to compare arrays.

Why do arrays coerce in a specific way? [duplicate]

This question already exists:
how does the js coercion work in some cases [duplicate]
Closed 1 year ago.
I'm struggling to understand why this examples below end up with this kind of results.
String([,,,]); // ',,' why?
Number([8,8]); // NaN why?
please explain with details if possible
Note that String calls the toString method on the object, which, for arrays, is equivalent to calling Array#join.
String([,,,]);
[,,,] is an array with 3 empty elements. [,,,].join() will then put two commas as separators between the empty elements, resulting in ",,".
Number([8,8]);
[8,8].join() returns "8,8", which cannot be parsed as a Number, so it produces NaN.

Easiest way of checking the truthy of a value in javascript? [duplicate]

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

What is the difference between Null, NaN and undefined in JavaScript? [duplicate]

This question already has answers here:
What is the difference between null and undefined in JavaScript?
(38 answers)
Why does typeof NaN return 'number'?
(21 answers)
Closed 4 years ago.
What is the difference between Null, NaN and undefined in JavaScript?
I have come across all three values, and have understood them to be “there isn’t anything here” in the context I found them- but I was hoping for a more detailed explanation as to why they occur, as well as what they mean in different contexts (for example- against an array, vs a class or variable).
NaN: Not a number: As the name implies, it is used to denote that the value of an object is not a number. There are many ways that you can generate this error, one being invalid math opertaions such as 0/0 or sqrt(-1)
undefined: It means that the object doesn't have any value, therefore undefined. This occurs when you create a variable and don't assign a value to it.
null: It means that the object is empty and isn't pointing to any memory address.

Why is Boolean([]) true in JavaScript? [duplicate]

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.

Categories