Issue working with comparison operator [duplicate] - javascript

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?

Related

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

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

JavaScript array.includes weird behavior with objects [duplicate]

This question already has answers here:
Comparing objects in JavaScript
(10 answers)
Closed 2 years ago.
Can somebody explain this weird behavior of javascript on comparing the existence of an object in an array
Equality checks work different for objects than for strings or numbers:
console.log('hello' === 'hello');
console.log(2 === 2);
console.log({x:2} === {x:2});

What is the purpose of || in a JavaScript variable? [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
How does javascript logical assignment work?
(6 answers)
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 4 years ago.
I just saw a line of code like this one below and I was intrigued by the use of ||
const myCars = ['BMW','Audi','VW']
const foo = myCars.length || [];
Does this mean if myCars.length was to ever be undefined that foo would equal an empty array instead of undefined?
Yes, how it should be read is:
if 'myCars' doesn't have a length (e.g. no values), the constant foo should be set to [].
Note: https://www.w3schools.com/jsref/jsref_length_array.asp, specifically the return value of the .length: "A Number, representing the number of elements in the array object".
this is Short-circuit evaluation in Javascript. its Unique in JS to USE || operator because other languages use this operator in conditional statements only. please read this
https://en.wikipedia.org/wiki/Short-circuit_evaluation

Why === and == giving false for following? [duplicate]

This question already has answers here:
Why doesn't equality check work with arrays [duplicate]
(6 answers)
Closed 4 years ago.
I know it is very much stupid to ask but can anyone tell me
Why === and == giving false for following.
x=[[1,2]];
console.log(x[0]===[1,2]);
console.log(x[0]==[1,2]);
Here typeof(x[0]) and typeof([1,2]) is also same, then why it is giving false?
Because they are different values in memory.
x=[[1,2]];
console.log(x[0]===[1,2]); // Here you're creating a new array in memory
console.log(x[0]==[1,2]); // Here you're creating a new array in memory
var y = x[0]; //Same value in memory
console.log(x[0]===y);
console.log(x[0]==y);
Equality comparisons and sameness

Why strict comparison(===) when checking typeof equality? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
What's the reason to use === instead of == with typeof in Javascript?
(5 answers)
Closed 6 years ago.
Say I wanted a function that checked whether or not a variable is a string, the interwebs advised me as follows:
function is_string(s) { return typeof s === 'string'; }
but I can't think of any scenario where "typeof s" could return "string" without s actually being a string.
Is there any reason for that === operator instead of ==?
Reason being, I want to check types in a switch statement, AFAIK, the switch statement uses the loose comparison operator. Since I'm only looking for known types and don't need to check for undefined, it should be fine, right?

Categories