Why is this simple JSON.parse JavaScript code failing? [duplicate] - javascript

This question already has answers here:
Why [] == [] is false in JavaScript?
(6 answers)
Weird behavior of comparison operator JavaScript when using empty array [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to understand why this simple code is saying false instead of true.
let test1 = JSON.parse("[]");
let test2 = JSON.parse("[]");
console.log(test1 == test2);
Surely this code should be true but I always get it saying false. Whats the issue?
Thanks

Related

what is the logic behind this logical expression [duplicate]

This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 8 months ago.
function expect(argument) {
this.eat(argument) || this.undefined();
}
Can someone please help me explain how this code works... the question is focused on this.eat(argument) || this.undefined();. I am guessing that one of those methods would run? and if it is, how do I tell which would run
Or Condition || will return the first true value.
if this.eat(argument) return true or any value which can be converted to true value, So the this.undefined() function will not execute.
If not it will execute.

Javascript "in" give wrong results [duplicate]

This question already has answers here:
Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?
(6 answers)
What is the difference between "in operator" and "includes()" for JavaScript arrays
(4 answers)
Closed 2 years ago.
I have the following code in Javascript:
var myArray = {"a":21600904, "b":21100999, "c":21602019, "d":21704354, "e":21602271, "f":21500123};
console.log(Object.values(myArray));
console.log(21600904 in Object.values(myArray));
And I get the following output:
[ 21600904, 21100999, 21602019, 21704354, 21602271, 21500123 ]
false
But this was not what I expected. What I understand is, 21600904 is inside Object.values(myArray) but 21600904 in Object.values(myArray) returns false instead of true.
What am I missing here? Why this code does not print true?

Issue working with comparison operator [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Triple equal signs return false for arrays in javascript. why?
(4 answers)
Closed 4 years ago.
Console logging this.byPassViewState returns ["01"]
if i do this.byPassViewState === ['01'] it returns false
typeof(this.byPassViewState) retuns object
My question is why this.byPassViewState returns false ? it suppose to be true right ? please tell me what i'm doing wrong here
You cannot compare 2 array with just using == or === operators.
The easiest way to compare array is using JSON.stringify().
let byPassViewState = ["01"];
let compare = (JSON.stringify(byPassViewState) == JSON.stringify(["01"]) );
console.log(compare);
Please Reference : How to compare arrays in JavaScript?

what does the ! mean in Angular? i.e $scope.selected = !$scope.selected; [duplicate]

This question already has answers here:
What is an exclamation point in JavaScript?
(2 answers)
Closed 7 years ago.
I was wondering what does the ! actually mean in this method
$scope.toggleSelected = function () {
$scope.selected = !$scope.selected;
};
I understand it's allowing me to set a selected item and it won't work without it but what exactly is the ! for?
The ! is the normal negation operator.
Inside of that function, it's used to flip/toggle the value each time it's called. For example, from true to false and vice-versa.

how does javascript or condition work [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
Closed 9 years ago.
i have written my code something like this
this._users = users || [];
just wanted to know what does this mean to this._users?
Thanks for the help.
This basically is evaluating users to true or false. If it evaluation is true, than return users otherwise, assigns an empty array to this._users .

Categories