Javascript Infinity-- (infinity minus minus) yields Infinity - javascript

I am looking at JavaScript's number type system.
I'm using Chrome, When I evaluate 15-- for a number literal I get a ReferenceError since it makes no sense to decrement a constant.
When I evaluate var x=10;x--; as expected everything works.
Expectantly var a=Infinity;a-- evaluates to Infinity, this all makes sense and is in accordance to the javascript language spec.
However to my surprise Infinity-- and Infinity++ evaluate to Infinity unlike other literals.
This also happens for Number.POSITIVE_INFINITY which is the same.
tl;dr :
Why does Infinity-- yield infinity as a result when 15-- and (new Number(15))-- yield a reference error?

Infinity as used in your example is not actually a value but refers to the Infinity property of the global object:
15.1 The Global Object
[...]
15.1.1 Value Properties of the Global Object
[...]
15.1.1.2 Infinity
The value of Infinity is +∞ (see 8.5). This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
So, Infinity-- is the same as window.Infinity-- which is perfectly valid.

Because there is no such thing as a the number infinity, it is a concept, and thus in coding it isn't built as other constants but as an object like null or undefined but with some properties thrown in to make it behave nice with Math methods.

Related

Ecmascript: what is the result of `GetValue([])`?

As I try to understand why this ![] exression in Javascript is false, I've got stuck. I understand that [] is considered truthy, and that !true is false but I'd like to dive deeper.
According to the Spec the unary NOT operator works like this:
Let oldValue be ToBoolean(GetValue(expr)).
If oldValue is true, return false.
Then I tried to understand GetValue([]) and it's all blurry right now. Spec sec-8.7.1
Does GetValue([]) simply returns [].valueOf()?
GetValue called with a plain value (not a Reference) returns just that plain value - in your case the array. Otherwise it would evaluate the reference to an identifier (in a variable scope) or property (in an object), but that's not the case in your example.
On that same page, see 8.7.1 for a description of how GetValue is implemented:
https://www.ecma-international.org/ecma-262/5.1/#sec-8.7.1
(it has nothing to do with valueOf)
Evaluate GetValue([]), then evaluate ToBoolean on the result, per the spec.

JavaScript - (window.addEventListener)

all
In the if statement fragment "if (window.addEventListener)" what does
the window.addEventListener resolve to. I believe it is a boolean but when is it "true" and when is it "false". I have been researching for a week but to no avail.
I am learning JavaScript by doing self-study, so bear with me.
Any if expression like that — that is, one without an explicit comparison — implicitly converts the expression value to a boolean. The rules in JavaScript are that anything other than null, undefined, 0, "", NaN, or false is considered true.
Thus, testing window.addEventListener like that is a way to check whether that property exists (is not undefined) on the window object.
window.addEventListener in all modern browsers refers to a function. In JavaScript, any value can be coerced to boolean. The result is either false (for 0, "", NaN, null, undefined, and of course, false — the "falsy" values) or true (for all other values — the "truthy" values). So if addEventListener exists on window and has a truthy value (a function reference is truthy), the code branches into the body of the if. If not (obsolete versions of Internet Explorer didn't have it, they had the Microsoft precursor to it called attachEvent), looking up the property will result in undefined, which is falsy.
Just for refer, javascript uses 'Falsy' values that is a value that translates to false when evaluated in a Boolean context.
Reference Falsy

Javascript !undefined gives true?

When I try to alert a negation of variable having undefined value , I get the output as true?
alert(undefined);
alert(!undefined);
The first alert gives undefined and second alert gives true.
Is this the expected behavior. If so then why ?Am I missing some concept/theory about undefined in Javascript?
Is this the expected behavior.
Yes.
If so then why ?Am I missing some concept/theory about undefined in Javascript?
JavaScript has the concept of implicit conversion of values (aka coercing values). When you use the negation ("NOT") operator (!), the thing you're negating has to be a boolean, so it converts its argument to boolean if it's not boolean already. The rules for doing that are defined by the specification: Basically, if the value is undefined, null, "", 0, 0n, or NaN (also document.all on browsers¹), it coerces to false; otherwise, it coerces to true.
So !undefined is true because undefined implicitly converts to false, and then ! negates it.
Collectively, those values (and false) are called falsy values. Anything else¹ is called a truthy value. This concept comes into play a lot, not just with !, but with tests in ifs and loops and the handling of the return value of callbacks for certain built-in functions like Array.prototype.filter, etc.
¹ document.all on browsers is falsy, even though it's an object, and all (other) objects are truthy. If you're interested in the...interesting...history around that, check out Chapter 17 of my recent book JavaScript: The New Toys. Basically, it's to avoid sites unnecessarily using non-standard, out of date features.
Yes, it is the expected behavior.
Negation of the following values gives true in javaScript:
false
undefined
null
0 (number zero)
""(empty string)
eg: !undefined = true
Note: The following checks return true when you == compare it with false, but their negations will return false.
" "(space only).
[ ](empty array),
eg: [ ] == false gives true, but ![ ] gives false
Yes, that's right. undefined is a falsy value.
https://developer.mozilla.org/ru/docs/Glossary/Falsy
https://developer.mozilla.org/ru/docs/Glossary/Truthy
If you need strict checks, you should avoid the non-strict operators:
$ node -p 'undefined == null'
true
and use the strict operators instead:
$ node -p 'undefined === null'
false
Unfortunately a strict negation does not exist.
But you can negate everything twice to get a true boolean equivalent:
$ node -p '!!undefined'
false

JavaScript: what is NaN, Object or primitive?

what is NaN, Object or primitive?
NaN - Not a Number
It's a primitive. You can check in a number of ways:
typeof NaN gives "number," not "object."
Add a property, it disappears. NaN.foo = "hi"; console.log(NaN.foo) // undefined
NaN instanceof Number gives false (but we know it's a number, so it must be a primitive).
It wouldn't really make sense for NaN to be an object, because expressions like 0 / 0 need to result in NaN, and math operations always result in primitives. Having NaN as an object would also mean it could not act as a falsey value, which it does in some cases.
NaN is a primitive Number value. Just like 1, 2, etc.
NaN is a property of the global object.
The initial value of NaN is Not-A-Number — the same as the value of
Number.NaN. In modern browsers, NaN is a non-configurable,
non-writable property. Even when this is not the case, avoid
overriding it.
It is rather rare to use NaN in a program. It is the returned value
when Math functions fail (Math.sqrt(-1)) or when a function trying to
parse a number fails (parseInt("blabla")).
Reference

Why 'NaN' and 'Undefined' are not reserved keywords in JavaScript?

We can do:
NaN = 'foo'
as well as
undefined = 'foo'
Why are they not reserved keywords?
I think it should be implemented in order to be sure that when we are looking for a number, it is a number :)
If we should use IsNaN() or typeof, why are NaN or undefined needed?
I cannot tell you why, but undefined and NaN are actually properties of the global object:
15.1.1 Value Properties of the Global Object
15.1.1.1 NaN
The value of NaN is NaN (see 8.5). This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
(...)
15.1.1.3 undefined
The value of undefined is undefined (see 8.1). This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
There is a difference between the value undefined (NaN) and the corresponding property.
You might notice the [[Writable]]: false. I'm not sure whether this new in ES5 (and might not be adapted by all browsers), but in newer browsers (tested in Firefox 6), assigning a new value to undefined has no effect:
[12:28:15.090] < undefined = "foo"
[12:28:15.093] > "foo"
[12:28:19.882] < undefined
[12:28:19.883] > undefined
So although it seems you can assign a new value, you actually cannot.
Why they are not reserved keywords?
Not sure if there was a specific reason to not make them reserved keywords, but it was decided against it. The language still works. You cannot override these values, so it's fine.
The only way to test whether a number is NaN, is to use isNaN() anyway.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/NaN
NaN is a property of the global object.
The initial value of NaN is Not-A-Number — the same as the value of
Number.NaN. In modern browsers, NaN is a non-configurable,
non-writable property. Even when this is not the case, avoid
overriding it.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined
undefined is a property of the global object, i.e. it is a variable in global scope.
The initial value of undefined is the primitive value undefined.
I'm speculating now, but the reason why I think NaN and undefined are not keywords is because you generally don't assign these values to variables.
var x = undefined; // doesn't make sense, use null!
var y = NaN; // You can't do much with this variable!
undefined basically means uninitialized, and if you want to make it clear that the value is unknown you use null. So undefined usually means not-initialized or the result of JavaScript code gone wrong.
NaN Is rarely assigned manually, simply because you can't do much with this value. It is usually the result of a calculation gone wrong. The makers of JavaScript probably didn't want to give this value the visibility of primitive values.
Also, NaN is also present in other languages and it isn't used as a keyword there either. For example: In C# NaN is represented by Double.NaN, since you don't make a distinction between floating point and integer values in JavaScript, I'm guessing that's why they put NaN with the Global Identifiers!
I hope this clears things up!
Both NaN and undefined are values in JavaScript.
NaN is of number type. (it denotes "not a valid number").
undefined is of undefined type. (when there is nothing assigned to a variable, JavaScript assigned undefined particular in var declaration).
And also both are read-only values(property) of global window object.
So that's why JavaScript cannot make values as a reserved word.
NaN is not a keyword, but it is rather a built-in property of the global object, and as such may be replaced (like undefined, but unlike the keyword this or the literals true, false, and null).
You can test if a value is NaN with the isNaN() function. Moreover NaN is defined to be unequal to everything, including itself.
Or in a nutshell you can say that:
NaN is the value returned when you try to treat something that is not a number as a number. For instance, the results of 7 times "abc" is not a number. The old form of it is Number.NaN. You can test for not-a-number values with the isNaN() function.
One reason is that it is possible to use those words as object keys:
messages = {
NaN: 'that was not a number',
undefined: 'that was not initialized',
default: 'that was something' // not in the original question but is valid syntax
};
console.log (messages.NaN);
While it would be possible to use { 'NaN': 'that was not a number' } and console.log(messages['NaN']), leaving as many words as possible unreserved could make your code easier to read.

Categories

Resources