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

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

Related

Checking value with JavaScript [duplicate]

This question already has answers here:
How does the single equal sign work in the if statement in javascript
(6 answers)
Closed 3 years ago.
This is my general code (in JavaScript):
let x = Math.floor(Math.random()×6)+1);
if (x=1){
do this
} else if (x=2){
do that
} else if.... and so on.
Whenever I test this code in a browser, only actions that could happen in the {do this} section happen. x is being locked as 1, even though it is meant to be a fair 6 sided die. Any ideas why this is happening, and how I can fix it?
The = operator in JavaScript is for assignment. x=1 will always return true because that is an assignment that will never fail. To test for equality use == (equality with conversion) or === (strict equality).
You have your if (x=1) assigning the value of 1 to x, rather than checking if it's equal to it. This results in it always returning TRUE. Using a pair of == (or ===) will solve this.

What is assignment with round brackets used for in JavaScript? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 3 years ago.
In one quiz I came across the following question:
What is the value of val?
var val = (5, 10, 15);
Despite the fact that I'm quite experienced with JavaScript I have never seen such kind of assignment before and failed to find any information about it on the internet.
What are the use cases and how it actually works?
Thanks in advance.
In JS, the comma operator (,) evaluates all operands, and returns the last.
So, in your case, it is equivalent to:
var val=15;
The 5 and 10 are evaluated and then silently dropped.
But consider the following:
var i=0;
var val=(i++,15);
console.log(i) //1
console.log(val) //15
The value of i is increased by 1, and it returns 15, combined to a single expression.

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.

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

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.

Not not (!!) inside if condition [duplicate]

This question already has answers here:
Why is the !! preferable in checking if an object is true? [duplicate]
(3 answers)
Closed 7 years ago.
Note: it's actually a duplicate of What is the difference between if(!!condition) and if(condition)
While I understand what the !! means (double not), for this same reason it doesn't make sense to me its use in the MDN documentation:
if (!!window.Worker) {
...
}
Isn't this exactly the same as this for this situation?
if (window.Worker) {
...
}
The casting to boolean makes no sense for me since the if will only be executed if the window.Worker exists. To say that it's True or Object for an if() conditional (I think) is the same.
So, why is the !! used here? Or, why is the window.Worker casted to boolean inside an if()?
Yes it is exactly the same. Nothing to add.
It might have been used to emphasize that the window.Worker property - expected to be a function - is cast to a boolean for detecting its presence, instead of looking like a forgotten () call. Regardless, it is now gone.

Categories