This question already has answers here:
JavaScript numeric self equality [duplicate]
(2 answers)
What is the difference between (NaN != NaN) and (NaN !== NaN)?
(5 answers)
Closed 2 years ago.
In auditing the Javascript version of 'verb', (a NURB library,) I happened across this method:
HxOverrides.cca = function(s,index) {
var x = s.charCodeAt(index);
if(x != x) return undefined;
return x;
};
I'm puzzled by the condition,
if(x != x)
When is this ever True?
In further reading, I discovered the Javascript method, "s.charCodeAt(index)" returns the Unicode value of the (index)th character in string 's'. Specifically:
If index is out of range, charCodeAt() returns NaN.
In the console, I tested:
NaN == NaN
I found this to be false. Therefore, as to the question of:
"When does x not equal x?"
the answer (at least in Javascript,) is:
"x != x when x is NaN (not a number)".
Related
This question already has answers here:
Logical operator || in javascript, 0 stands for Boolean false?
(2 answers)
What does this symbol mean in JavaScript?
(1 answer)
What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?
(8 answers)
Closed 5 years ago.
let x = process.argv[2] || "abc";
I am new to javascript.
I don't think || is a boolean OR operator here.
What does this line mean? Why using OR on some strings?
Does this mean if process.argv[2] is null, then assign "abc" to x?
This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 5 years ago.
I know the rules that 0 is equal to empty string '' in javascript. So how to make if statement to differ this? I mean, I have field where I insert amount value. For empty string I need one message to show but for 0 value another. Currently, I always catch 0 == ''.
if (amount == '') {
$("#validation-msg3").addClass("opux-is-visible");
return false;
}
else if (amount < 5) {
$("#validation-msg1").addClass("opux-is-visible");
return false;
}
EDIT. when I use === I do not get condition equals true when field is empty. What does it mean? My amount value is different type than empty string? I parse it like this:
var amount = parseInput($("#amount").val());
Just use strict compare === that will check value AND type of operands.
Use three equation signs. It checks the type too and 0 will be different from ''.
if (amount === '') {
$("#validation-msg3").addClass("opux-is-visible");
return false;
}
This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Logical AND (&&) and OR (||) operators
(1 answer)
Closed 6 years ago.
I have something like the following:
var val = "string";
var testVal = val && val.length;
I would expect testVal to be either true or false but it is the length of the string. Not sure why this is?
The logical && operator doesn't perform a type casting. The expression it forms is just evaluated to the left-hand operand if this operand is falsy, otherwise to the right-hand operand.
You have to explicitly convert to boolean:
var testVal = !!(val && val.length);
This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 6 years ago.
I am not getting what or operator does with inters. I have following code
-1||4 // output -1
4||-1 //output 4
Does it converts integers in bytes and performs or operation.
It first checks wheter the number is truthy or falsey and returns the first truthy one. All numbers are truthy except for 0.
0 || 4; // 4
2 || 3; // 2 (picks the first one, because both true)
-3 || 0; // -3
0 || -2; // -2
Does it converts integers in bytes and performs or operation?
No. The || operator is logical and, not bitwise and.
This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 7 years ago.
I am trying to understand equality in JavaScript. Here is the code.
var x = prompt("What is 10 + 10");
if (x === 10) {
document.write("Correct")
}
else {
document.write("Incorrect")
}
Why wouldn't I make the equals sign like "===". So if "10" is equal("===") to "x"(user answer) then it should be correct right?
I searched on both Stack Overflow and W3Schools, but couldn't find what I was looking for. I guess I'm just nor getting this "true or false" thing. I mean this seems like a very simple equation. Help would be great thanks guys!
=== is strict type equality which compares by both value and type
== is non-strict type equality, which compares only by value.
In other words, == performs type conversion and then compares values for equality. Here are some examples
"3" == 3
=> true
Explanation: The string 3 is converted to the number 3, which is equal to 3.
"3" === 3
=> false
Explanation: The string is not converted to a number. Thus the string 3 does not equal the number 3.
In your example, incorrect would be written to the document. That is because the result of prompt returns a string, and you are performing strict equality with a number.
In your case, the interpreter sees it like this
if ("10" === 10) {
// does the string "10" equal the number 10? If so
document.write("Correct")
}
else {
// Hey, wait a minute. It doesn't equal the number. I should write "Incorrect" instead.
document.write("Incorrect")
}
In Javascript,
== means: is equivalent to
=== means: is identical to
When the value of x is "10", x is equivalent to 10.
But it isn't identical to 10.