What does JavaScript do with Number(undefined)? [duplicate] - javascript

This question already has answers here:
Why is NaN === NaN false? [duplicate]
(3 answers)
Closed 7 years ago.
I recently thought about defining a function to copy the functionality of isNaN out of boredom when I found out, that Number(undefined) equals NaN</code>, but if you doNumber(undefined) === NaNyou getfalse`.
I even tried (typeof Number(undefined)) === (typeof NaN) which returns true.
What is JavaScript doing here?

The constant NaN is never equal to anything, including NaN. The value of typeof NaN is "number", because NaN is a number constant.
The value of Number(undefined) is NaN. You can use isNaN() to verify that, or simply:
var x = Number(undefined);
if (x !== x) alert("It's NaN!");

Related

Why typeof undefined + 1 is a 'number'? [duplicate]

This question already has answers here:
Why does typeof NaN return 'number'?
(21 answers)
Closed 9 months ago.
Im trying to pass the test of my lesson but answer of a test dont mach result im getting from JS Bin.
My task is:
let unknown = undefined;
let sum = unknown + 1;
let typeOfSum = typeof sum;
console.log(typeOfSum);
Answer of test should be 'NaN'
BUT!
When I'm trying this code in JS Bin, I get result 'number'
Please, help me to understand, why I'm not getting NaN if it should be NaN?
JavaScript works this way. There's no typeof output that's NaN. The answer is here:
Yes, you're right. undefined + 1 is NaN.
But typeof NaN is a 'number' always.

.includes() on an empty string logic [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
Closed 2 years ago.
I'm trying to debug someone`s code. The "e" variable line is checking whether the user has "save-data" enabled or if he's on a slow (2g) connection.
e = navigator.connection && (navigator.connection.saveData || (navigator.connection.effectiveType || "").includes("2g"));
if (!e && d)...
The part I'm confused about is the empty brackets in the last OR statement. Is it just a redundant code or am I missing something?
What's the point for checking whether an empty string has "2g" in it?
(navigator.connection.effectiveType || "") will resolve to an empty string if effectiveType is null, undefined or some other falsy non-string value.
If you didn't do that you'd attempt to call null.includes() which would throw an exception.

Why strict comparison(===) when checking typeof equality? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
What's the reason to use === instead of == with typeof in Javascript?
(5 answers)
Closed 6 years ago.
Say I wanted a function that checked whether or not a variable is a string, the interwebs advised me as follows:
function is_string(s) { return typeof s === 'string'; }
but I can't think of any scenario where "typeof s" could return "string" without s actually being a string.
Is there any reason for that === operator instead of ==?
Reason being, I want to check types in a switch statement, AFAIK, the switch statement uses the loose comparison operator. Since I'm only looking for known types and don't need to check for undefined, it should be fine, right?

JavaScript === vs == for type checking [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 6 years ago.
Which of the following lines is correct?...
if (typeof value == 'boolean') { return value; }
... or ...
if (typeof value === 'boolean') { return value; }
I thought the double equal sign was a type of "soft compare" so the value variable could either be a string or formal type. Is this not so? I wonder because JSHint complained about the first version. I've changed it but now I'm worried that typeof won't return a string.
== is a soft compare, but typeof always returns a string.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

What is the best way to check a given variable is NaN or not? [duplicate]

This question already has answers here:
How do you check that a number is NaN in JavaScript?
(33 answers)
Closed 7 years ago.
What is the best way to check a given variable is NaN or not?
(only using pure Javascript without any libraries such as Underscore)
JavaScript has function isNaN for that purpose. As described in w3schools:
The isNaN() function determines whether a value is an illegal number
(Not-a-Number).
This function returns true if the value is NaN, and false if not.
For example:
isNaN(123) // returns false;
isNaN("Hello") //returns true
You can use isNaN(x) which returns a boolean value (true if x is NaN, false otherwise).

Categories