Javascript: Is my variable NaN or is it a Number? [duplicate] - javascript

This question already has answers here:
Why does typeof NaN return 'number'?
(21 answers)
Closed 5 years ago.
I'm not able to establish if my javascript variable is a number or not.
Here's what I have:
alert('variable startDateB: ' + startDateB);
Results:
variable startDateB: NaN
In the very next line I have:
alert('typeof startDateB: ' + typeof(startDateB));
Results:
typeof startDateB: number
My end goal is to compare this date with other dates, but I don't know whether a conversion is necessary since I appear to be getting mixed information about the variable's data type.
Any help is greatly appreciated!
Thanks!

By definition, NaN is the return value from operations which have an undefined numerical result.
Aside from being part of the global object, it is also part of the Number object: Number.NaN.
This is why you are seeing the behavior you describe. NaN is part of the Number object.
It is still a numeric data type, but it is undefined as a real number.

Related

Why do arrays coerce in a specific way? [duplicate]

This question already exists:
how does the js coercion work in some cases [duplicate]
Closed 1 year ago.
I'm struggling to understand why this examples below end up with this kind of results.
String([,,,]); // ',,' why?
Number([8,8]); // NaN why?
please explain with details if possible
Note that String calls the toString method on the object, which, for arrays, is equivalent to calling Array#join.
String([,,,]);
[,,,] is an array with 3 empty elements. [,,,].join() will then put two commas as separators between the empty elements, resulting in ",,".
Number([8,8]);
[8,8].join() returns "8,8", which cannot be parsed as a Number, so it produces NaN.

Javascript string update doesn't change the string itself [duplicate]

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 1 year ago.
I have following simple javascript code:
s="hello"
s[0]="X"
console.log(s[0])
The output is h, but I have updated it as X, I would ask why I got such result, thanks.
Strings in JavaScript are primitives, which means they are immutable, ie. you are not able to change them. If you were running your code in strict mode then this would throw an error.
If you wanted to modify this string you would need to effectively create a copy of it, and then assign that to the s variable eg.
s = "X" + s.substring(1)

What is the difference between Null, NaN and undefined in JavaScript? [duplicate]

This question already has answers here:
What is the difference between null and undefined in JavaScript?
(38 answers)
Why does typeof NaN return 'number'?
(21 answers)
Closed 4 years ago.
What is the difference between Null, NaN and undefined in JavaScript?
I have come across all three values, and have understood them to be “there isn’t anything here” in the context I found them- but I was hoping for a more detailed explanation as to why they occur, as well as what they mean in different contexts (for example- against an array, vs a class or variable).
NaN: Not a number: As the name implies, it is used to denote that the value of an object is not a number. There are many ways that you can generate this error, one being invalid math opertaions such as 0/0 or sqrt(-1)
undefined: It means that the object doesn't have any value, therefore undefined. This occurs when you create a variable and don't assign a value to it.
null: It means that the object is empty and isn't pointing to any memory address.

I think I discovered a bug with isNaN() [duplicate]

This question already has answers here:
isNaN(1) and isNaN("1") returns false
(2 answers)
Closed 6 years ago.
I'm learning Javascript mechanics and I believe I just stumbled across a bug with isNaN().
Here is the test code.
var x = "1000";
Answer = isNaN(x);
console.log(Answer);
The console log returns "false" which indicates that Javascript looks at "1000" as a number. I thought that anything inside " " was considered a string. Evidently not always. If I'm wrong maybe somebody has some insight that can set me straight.
Apparently, it's a feature, not a bug.
When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

Javascript idiom: What does if (x === +x) do? [duplicate]

This question already has answers here:
+ operator before expression in javascript: what does it do?
(4 answers)
Closed 10 years ago.
Reading through the source code of underscore.js I stumbled upon the following line:
... if (obj.length === +obj.length) { ...
That's a bit confusing for me. What is actually being compared here? I believe it has something to do about detecting native arrays, but cannot figure out what's actually going on. What does the + do? Why use === instead of ==? And what are the performance benefits of this style?
The + coerces the value to an Number (much like !! coerces it to a boolean).
if (x === +x)
...can be used to confirm that x itself contains an integer value. In this case it may be to make sure that the length property of obj is an integer and has not been overwritten by a string value, as that can screw up iteration if obj is treated as an array.
It is a silly (IMO) way of checking if obj.length is a Number. This is better:
typeof obj.length == "number"
The + coheres what is on the right side to be a number.
In this case if length was not a property on the object undefined would be returned. + undefined will yield Nan and this evalutation be false.
If the string can be coheres-ed into a number then it will be.. e.g + '1' will yield 1 as a Number this is especially important when dealing with hex values in string form e.g. +'0x7070' yields 28784

Categories