Adding 'undefined' and a 'number' , the result should be 'NaN' , but this simple code shows different [duplicate] - javascript

This question already has answers here:
Is NaN equal to NaN?
(8 answers)
What is the rationale for all comparisons returning false for IEEE754 NaN values?
(12 answers)
Closed yesterday.
Hi I am solving the JS coding test.
It is to know what is the output .
const number = undefined + 11;
if (number === NaN) {
document.write("NaN");
} else if (number === 11) {
document.write("11");
} else {
document.write("other");
}
I thought the output is the 'NaN'
But the output is 'other'
Is anyone knows why the output is 'other'?
I checked with console.log(number) and it showed NaN .. I dont know why if statement shows 'other'
Thank you so much !

Related

Can anyone explain what is happening here object[val] = (object[val] || 0) + 1 [duplicate]

This question already has answers here:
How to increment a value in a JavaScript object?
(9 answers)
Closed 3 months ago.
I am practicing js and i encounter this line of code in a for each loop
function test(arr1){
let fq1 = {}
for(val of arr1){
fq1[val] = (fq1[val] || 0) + 1
}
}
test([1,2,3,3]);
i have provided the code what i encounter can anyone please exaplian what is happening in
fq1[val] = (fq1[val] || 0) + 1
i tried and i have seen the object key is storing the number of value are provided in array. But i am not clear about the line of code i have mentioned above.
(fq1[val] || 0)
this means evaluate to the value on the left if it is truthy (if converted to a boolean, it would be true), otherwise evaluate to the value on the right.
In this specific case, this is saying "evaluate to the value in object fq1 at key val, however if it's undefined (not in the object yet) then use 0 as a default value"

New to coding, What Am I missing? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
working on JS, What am I missing? Thank you
Modify the function below to greet only those with an even number of letters in their name
function helloYou(name)
numbers.filter (n => n % 2 =i= 1);{
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)
To access the number of letters in the string, you can use the attribute .length.
Then to check if this number is even a modulus 2 should return 0, that's what we need to check. This condition goes in an if statement.
Finally, if this condition is met, return Hello concatenated with name.
Otherwise nothing is returned, so it's undefined (there is no need to explicitly write return undefined).
function helloYou(name) {
if (name.length % 2 === 0) {
return "Hello, " + name;
}
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)
The variable numbers is actually undefined in this case.
Also, filter is not useful in this situation. Filter is mainly used to get the elements that match a condition from an array.
Your should use an if statement to check for even length. Better yet, you can use the ternary operator. Here is an example:
function helloYou(name) {
return name.length % 2 === 0 ? 'Hello, ' + name : undefined;
}
console.log(helloYou('Bob'));
console.log(helloYou('Anna'));

Is possible to add NUMBER value return in Js? [duplicate]

This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 3 years ago.
I am using Javascript. In success function, I am getting some results and storing in var say "baldet" in JS-CODE.
example:
success: function (result) {
baldet = result;
console.log(baldet[0].cleavebalance);
if (openmon == 1){
$("#clvbalance").val(baldet[0].cleavebalance);
}
}
from baldet[0].cleavebalance I am getting 2. My condition is if openmon == 1 then I want to add number 1 in it.
NOTE: add means SUM, not CONCAT()
So that i could get, 2 (from "baldet[0].cleavebalance" + 1 (number i want to add)) i.e. 2 + 1 = 3.
and show in $("#clvbalance").val("3"); // value 3 is after getting SUM RESULT
Currently, I am getting "21" instead of "3"
I hope I made my query clear. Any help will be appreciated.
You need to parse from string to number like below
console.log("2" + 1); // dose not parse
console.log(parseInt("2") + 1)

if runs with low-number >= high-number? [duplicate]

This question already has answers here:
Why is string "11" less than string "3"? [duplicate]
(6 answers)
Closed 3 years ago.
I am making a mistake or something strange is going on but I am not seeing it.
localStorage value = 9
if (localStorage.getItem('TotalProducts') >= '10') {
alert('total products'+localStorage.getItem('TotalProducts'));
}
Why am I receiving the alert?
Alert content is total products9
Thanks everyone for helping.
For others read the comments below there is some really useful information in there.
if (+localStorage.getItem('TotalProducts') >= 10) {
alert('total products'+localStorage.getItem('TotalProducts'));
}
Convert your TotalProducts value of localStorage to a number and then compare it to 10.

String to Boolean [duplicate]

This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 6 years ago.
I've read numerous examples converting from String to Boolean.
For example,
myString = (myString == "true");
But I'm unable to apply the logic to my code. Can someone help?
CommonConfig.getFeatureFlags['analytics.demo'] returns "true" (Note
the "").(That's how backend returns it)
var FeatureFlag = _.extend(CommonConfig, {
demoFeature: String == CommonConfig.getFeatureFlags['analytics.demo'], //either "true" or "false" but I want to make it to true or false
});
Question: I want to convert from String "true" to boolean. And pass true or false based upon!
Can someone help?
Isn't it:
var FeatureFlag = _.extend(CommonConfig, {
demoFeature: "true" == CommonConfig.getFeatureFlags['analytics.demo'],
});

Categories

Resources