This question already has answers here:
Empty array does not equal itself in Javascript?
(4 answers)
Closed 1 year ago.
To check if an array is empty , we have many choices like if(array.length===0) or if(array=='') but I'm wondering why if (array==[]) doesn't check if an array is empty or not. Any one has a clear explanation?
Double Equals ( == ) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other. and in the same way [] doesn't have any type and if(arr == []) doesn't work in javascript
Related
This question already has answers here:
Check if an array of strings contains a substring [duplicate]
(5 answers)
How do you search an array for a substring match?
(15 answers)
Closed 4 months ago.
Is there a better way of checking if a word exists in an array of strings without including Punctuation Marks?
what I have tried,
const sArray=['Lorem','Ipsum','typesetting-','industry.','Ipsum?','has' ]
console.log(sArray.toString().replaceAll(".","").includes("industry")) //true
console.log(sArray.includes("industry")) //false
Something like this?
sArray.some(str => str.includes('industry'))
If you do a lot of operations on the array, I suggest creating a new reference of the array after replacing, then dealing with the new array.
const sArray=['Lorem','Ipsum','typesetting-','industry.','Ipsum?','has' ]
const trasformedArray = sArray.map(i => i.replaceAll('.', ''));
console.log(trasformedArray.includes("industry"))
This question already has answers here:
Variable position in comparision in PHP
(4 answers)
Why do some experienced programmers write comparisons with the value before the variable? [duplicate]
(12 answers)
PHP conditional assignment
(1 answer)
Closed 2 years ago.
In many place, I've seen that there is used the expression if (value === expression) rather than if (expression === value).
For example, In php we use-
if (false === strpos('abc', 'a'))
And also I see after minifying JavaScript, the minified file also generated like this.
So my question is, what is the benefit of value === expression over expression === value?
Note: This question may be redundant but I may not get the proper keywords for searching. If it is duplicated then I am ready to close the question.
I don't think there is a difference because the idea of equality operator is that to check if both sides are have the same value, in === also same type.
This question already has answers here:
convert string array to integer array
(4 answers)
Closed 4 years ago.
I have a jQuery array ["3434", "3433"]
And I would like to make it like [3434, 3433]
No need for jQuery. Supposing you're sure they're all numbers (that is, we're not checking for errors), you can map them with Number, which converts them from string to numbers.
["3434", "3433"].map(Number);
Considering that Number returns NaN in case of error, you may then want to filter the result to remove undesired elements.
let nums = ["3434", "3433", "foo"].map(Number).filter(n => !isNaN(n));
console.log(nums);
This question already has answers here:
How does JS type coercion work?
(2 answers)
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 years ago.
I have a question about type conversions. Given the following table from w3schools.com...
Why are the strings "0" and "000" converted to boolean true?
Because when you coerce a value to boolean in JavaScript, any non-blank string is true. Only blank strings are false.
The reason of it is because both "0" and "000" are strings and not numbers.
Any string which is non-empty converted to boolean is going to be true.
This question already has answers here:
Javascript string/integer comparisons
(9 answers)
Closed 7 years ago.
My javascript is behaving very strange!
I have a input field in my html file. This input field is read by javascript in the following way:
var bscore = $("#bscore").val();
Then i want to show an alert if the input is below a certain input
if(bscore<"913"){
document.getElementById("bscorealert").style.display="block";
};
This works fine. So when the number is above 913 it should not show. Only javascript is behaving very strange, when the number in the bscore input field is above 999 (so 1000 and higher) the if statement is triggered and the alert is shown.
How is this possible?
It's possible because you are comparing strings, not integers. String comparison is lexicographical, so "9" > "10" and so on.
You will want to convert before comparing with Number(strValue) or with the unary operator +:
var bscore = +$("#bscore").val(); // + prefix converts to number
if (bscore < 913) ... // no quotes around 913!
You're trying to compare 2 strings, which uses the alphabetical comparison. If you enter a value between 9130 and 9999 in the field, you'll notice that the messagebox doesn't show as well.
You'll have to do a parseInt to compare numerical values.