Comparing two empty array not working in javascript [duplicate] - javascript

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
[] is not identical to [] [duplicate]
(4 answers)
Closed 4 years ago.
In my current project I am taking a request parameter with a value of either array or a string. But if I get an array it would be an empty array. So what I did is I checked the type first and then I worked with the value. But then I did something like this
const reqParam = []
if (reqParam === []) {
console.log('empty array')
} else {
console.log('string')
}
But reqParam despite of being an empty array is giving me false while comparing with []. Why it is behaving like this? Thanks in advance.

Your code creates two different arrays, that's why the comparison is returning false. === will only compare their references and not their content.

Related

Index of array? [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Why does this index of a two dimensional array return -1 [duplicate]
(2 answers)
Closed 3 years ago.
I have simple nested array, like:
var arr = [[75.0], [65.0]] ;
and when I do:
arr.indexOf( [75.0] );
I expect 0, but I get -1, what gives?
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf :
indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).
There is the problem. In your example, you have an array of arrays. Comparing with === operator means that for it to evaluate to true, it has to be the same array object. Clearly it is a different object so it is not found from the array.
You need to use Array.find() instead where you can provide the testing function. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Palindrome array comparison always returns false [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Why doesn't equality check work with arrays [duplicate]
(6 answers)
Closed 5 years ago.
I'm learning JS and had to come up with a function to check if a certain string is a palindrome. I managed to get it right with:
function isPalindrome(word) {
return word == word.split('').reverse().join('');
}
However, my first attempt was:
function isPalindrome(word) {
return word.split("") === (word.split("").reverse());
}
But this does not work. What this second function does is get the string and make an array out of it, then compare that to the string as an array but reversed. If I console.log() both sides I get the same array (in the case of a palindrome like "level") so why does this always evaluate to false?

In javascript, [] === [] and [] == [] both returns false [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 7 years ago.
why is that? I assumed it's some implicit type conversion at first, but [] == [] is also false.
Arrays in javascript are Objects. Objects are compared by identity. So no two objects created by different literals (or by other means) are going to be equal (either strictly (===) or loosely (==)).

Checking if an array is empty with Coffeescript [duplicate]

This question already has answers here:
Checking if an array has some items in coffeescript
(3 answers)
Closed 8 years ago.
I am trying to figure out if an array is empty. I tried this and it does not work.
a = []
if a == []
alert "empty"
I know I can check the length of the array but am curious why this does not work.
There is no direct empty check for arrays. You could do something like this though:
a = []
alert("empty") unless a.length

test for a propertie in JSON project to be empty? [duplicate]

This question already has answers here:
Testing for an empty array object in JSON with jQuery
(5 answers)
Closed 8 years ago.
I have the following JSON object that is passed to one of my functions …
{"action":"setProjectAll", "application":{"proId":"new","zoomPosition":35,"scrollerPosition":0,"language":"en","timelinePosition":0}, "clips":[]}
How can I test this object for the propertie "clip" to be "[]" (empty)?
Right now in the example above it is empty, however since the object is dynamic I need to test for that property if it contains values or not.
How can that be done?
thank you
How about
if (x.clips && x.clips.length === 0)

Categories

Resources