What is wrong with my javascript functions logic? [duplicate] - javascript

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 6 years ago.
I'm having a problem with my Javascript function, I'm not understanding something, just looking for some clarity.
I have a function:
function Test (array) {
if (array === []) {
return "the array is empty";
} else {
return array;
}
When I pass this function an empty array, it returns the empty array, completely skipping the first part of my if statement (this is the part I'm not understanding, why is it skipping that part? My understanding is that it would return my string statement at that point since the array I pass it, is in fact empty. If I remove the else statement, it returns "undefined".
NOTE! : I am aware that the solution to this problem is to set my "if" statement to compare the length of the array I pass it.
ex:
function Test (array) {
if (array.length === 0) {
return "the array is empty";
} else {
return array;
}
I'm just still not understanding why the first one doesn't work, and would really appreciate an explanation.

When you compare two objects in JavaScript, the comparison is asking "Are these objects the same object?", not "Are these objects identical?".
You are comparing two different empty arrays.

Related

Is there a way to splice out elements of an array and return the spliced array in one line? [duplicate]

This question already has answers here:
Js remove element from array without change the original
(4 answers)
Closed 13 days ago.
If I have an array a = [1,2,3,4] and I want to return it with the 3 removed, there are two ways I can do it:
let b = [...a] // or a.slice()
b.splice(2,1)
return b
or
return [...a.slice(0,2), ...a.slice(3,4)]
Advantage of the second is that it's one line. Disadvantage is that it's verbose. I thought of writing a helper function that contains the logic of the first approach, so that I'd be able to call it in one line elsewhere.
Is there an alternative? Something like splice but that returns that spliced array rather than mutating it and returning the spliced-out elements.
Since you know the indicies you want to remove, you can use the Array.prototype.filter method.
const a = [1,2,3,4];
const b = a.filter((_, i) => i !== 2);
console.log(b);
If you need to remove a range, you can just do something like 2 < i || i > 3.
.filter makes a copy of the array, copying the values where the callback function evaluates truthy and ignores the values where the callback function evaluates falsy.

In JS, when using or (||) in if statement, when first is true, does it evaluated second? JavaScript behavior [duplicate]

This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
In javascript, if (true || false) results in true, but the question is whether the compiler will evaluate the second statement if the first is true.
In my case, I want to know if an array has changed, so I believe I have two options: compare the lengths of the arrays (what was and what is now) and compare if the array values are different.
I think the first option requires less work.
If (first || second) { give me deleted values, and give me added values }
No, it does not
first() || second()
function first() {
console.log('first')
return true
}
function second() {
console.log('second')
return false
}

Comparing two empty array not working in javascript [duplicate]

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.

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?

Return statement not stopping function [duplicate]

This question already has answers here:
Javascript function fails to return element
(2 answers)
Closed 8 years ago.
I'm sure this is a noob issue but I'm a little confused. My understanding is that when a return statement is reached it should stop the function, however in the example below it doesn't seem to. Would someone please explain what's going on?
Fiddle
var hasTerm = function(obj,term){
$.each(jsonObject,function(key, value){
if (value == term){
console.log("conditional");
return true;
console.log("conditional after return");
};
if(typeof value == (Object || array)){
hasTerm(value,term);
}
});
return false;
}
The $.each is looping through all of the object collections passed in "jsonObject". Likely it is stopping on an object and then restarting on the next object. Does the console window ever show "conditional after return"?
Use "return false" to stop the iteration.

Categories

Resources